我正在Unity編寫2D平臺遊戲,我試圖讓玩家留在移動平臺上。我已經完成了一兩天的搜索和修補工作,而且我沒有任何運氣。留在移動平臺上
基本上,我被告知要儘量讓角色在觸摸時隨着平臺移動。首先,如果我使用與OnTriggerEnter()相關的任何東西,玩家就可以通過該平臺。如果我做OnCollisionEnter()(在播放器上有一個CharacterController,在平臺上有一個BoxCollider),根本沒有任何事情發生。這些是我發現的最重要的兩件事。另一個是用平臺來撫養玩家,但這顯然會造成「問題」(經常說,從來沒有解釋過)。
那麼,我該如何讓玩家留在移動平臺上呢?下面是移動平臺的代碼:
public class MovingPlatform : MonoBehaviour
{
private float useSpeed;
public float directionSpeed = 9.0f;
float origY;
public float distance = 10.0f;
// Use this for initialization
void Start()
{
origY = transform.position.y;
useSpeed = -directionSpeed;
}
// Update is called once per frame
void Update()
{
if(origY - transform.position.y > distance)
{
useSpeed = directionSpeed; //flip direction
}
else if(origY - transform.position.y < -distance)
{
useSpeed = -directionSpeed; //flip direction
}
transform.Translate(0,useSpeed*Time.deltaTime,0);
}
,這裏是對玩家的動作(在更新)的代碼:與我怎麼樣,我認爲它可能要做:
CharacterController controller = GetComponent<CharacterController>();
float rotation = Input.GetAxis("Horizontal");
if(controller.isGrounded)
{
moveDirection.Set(rotation, 0, 0); //moveDirection = new Vector3(rotation, 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
//running code
if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) //check if shift is held
{ running = true; }
else
{ running = false; }
moveDirection *= running ? runningSpeed : walkingSpeed; //set speed
//jump code
if(Input.GetButtonDown("Jump"))
{
//moveDirection.y = jumpHeight;
jump();
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
編輯m定義了玩家和平臺,但我嘗試了不同的組合。如果平臺是一個觸發器(在其對撞機上),玩家就會一直貫徹它。如果沒有,我不能使用OnTrigger函數。我有一個固定在播放器和平臺上的剛體,但它似乎沒有影響任何東西。當玩家在某些設置上進入平臺時,他會不停地跳動,而且往往會結束。
我從來沒有使用Unity3D,但有可能像做'moveDirection + = theplatform。moveDirection'當角色在平臺上? – mbeckish 2013-03-26 17:59:05
我試過了,但我似乎遇到了OnTrigger和OnCollision函數的問題,這些函數將負責「玩家在平臺上」時的情況。我認爲這與我如何定義我的對象有關,但我不確定如何定義它們以使其可行。 – muttley91 2013-03-26 18:02:02
此外,你需要確保角色的moveDirection.y永遠不會比你的平臺的moveDirection.y更少,否則你會遇到(除非Unity3D框架自動爲你處理?) – mbeckish 2013-03-26 18:03:10