2016-08-15 2 views
0

나는 유니티에 대해 아주 익숙하다. 그래서 내가 어떻게하면 좋을지 모르지만, 캐릭터가 점프 할 때 일관성이 없다.왜 플레이어의 바운스가 일치하지 않습니까?

일부 바운스는 다른 것보다 훨씬 많으며 왜 이런 일이 발생하는지 잘 모르겠습니다.

또한 위쪽 화살표를 누르면 빠르게 움직입니다.하지만 2 초 정도 기다리면 정상처럼 튀어 오릅니다. 대신에 힘을 추가 할 rigidbodys 능력을 활용, 객체를 변환의

using UnityEngine; 
using System.Collections; 

public class MovePlayer : MonoBehaviour 
{ 
    Vector3 endPos; 
    int numBackwards = 0; 
    bool jumping = false; 
    public Rigidbody rigidBody; 
    //public Collider theCollider; 

    void Start() 
    { 
     rigidBody = GetComponent<Rigidbody>(); 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     rigidBody.freezeRotation = true; 

     endPos = gameObject.transform.position; 
     if (!jumping) 
     { 
      if (Input.GetButtonDown("up") && gameObject.transform.position == endPos) { 
       if (numBackwards < 0) 
       { 
        numBackwards++; 
       } 
       else 
       { 
        UpdateScore.score++; 
       } 
       transform.Translate(Vector3.up * 50 * Time.deltaTime, Space.World); 
       transform.Translate(Vector3.forward * 110 * Time.deltaTime, Space.World); 
      } 
      else if (Input.GetButtonDown("down") && gameObject.transform.position == endPos) 
      { 
       transform.Translate(Vector3.up * 50 * Time.deltaTime, Space.World); 
       transform.Translate(-Vector3.forward * 110 * Time.deltaTime, Space.World); 
       numBackwards--; 
      } 
      else if (Input.GetButtonDown("left") && gameObject.transform.position == endPos) 
      { 
       transform.Translate(Vector3.up * 50 * Time.deltaTime, Space.World); 
       transform.Translate(Vector3.left * 110 * Time.deltaTime, Space.World); 
      } 
      else if (Input.GetButtonDown("right") && gameObject.transform.position == endPos) 
      { 
       transform.Translate(Vector3.up * 50 * Time.deltaTime, Space.World); 
       transform.Translate(Vector3.right * 110 * Time.deltaTime, Space.World); 
      } 
     } 
    } 

    void OnCollisionEnter(Collision other) 
    { 
     if (other.gameObject.CompareTag("Ground")) 
      jumping = false; 
    } 

    void OnCollisionExit(Collision other) 
    { 
     if (other.gameObject.CompareTag("Ground")) 
      jumping = true; 
    } 
} 

답변

1

:

여기 내 코드입니다. 나는 또한 충돌 이벤트를보다 간단한 방법으로 대체했다. 객체 중심과지면 사이의 거리를 확인합니다. (당신은 여전히 ​​자신의 길을 분명히 사용할 수 있습니다). FixedUpdate는 코드가 프레임 속도에 의존하지 않고 정기적으로 호출되도록합니다. 당신은 앞으로 또는 측면 속도 (점프인가


public float fspeed 

다시 이동할 수 있습니다 때까지 (그 주제 Here에 기타)

//PUBLIC 
public float distance;  
public float fspeed; 
public float uspeed; 

//PRIVATE 
private Rigidbody rigidBody; 


void Awake() 
{ 
    rigidBody = GetComponent<Rigidbody>(); 
    rigidBody.freezeRotation = true; 
} 

void FixedUpdate() 
{ 
    if (Physics.Raycast(transform.position, -Vector3.up, distance + 0.1F)) 
    { 
     if (Input.GetKeyDown(KeyCode.UpArrow)) 
     { 
      rigidBody.AddForce(new Vector3(0, uspeed, fspeed)); 
     } 

     if (Input.GetKeyDown(KeyCode.DownArrow)) 
     { 
      rigidBody.AddForce(new Vector3(0, uspeed, -fspeed)); 
     } 

     if (Input.GetKeyDown(KeyCode.LeftArrow)) 
     { 
      rigidBody.AddForce(new Vector3(fspeed, uspeed, 0)); 
     } 

     if (Input.GetKeyDown(KeyCode.RightArrow)) 
     { 
      rigidBody.AddForce(new Vector3(-fspeed, uspeed, 0)); 
     } 
    } 
} 

public float distance 

땅에 분 거리입니다 거리)


public float uspeed 

는 (점프 높이)를 위쪽으로 속도가

+0

답변을 neaten하기 위해, 나는 문 if''의 부하를 방지하기 위해, 당신의'FixedUpdate()'방식 내에서'switch' 문을 사용하는 것이 좋습니다 것입니다 –

+0

바운스가 여전히 일치하지 않습니다. 위쪽 화살표를 너무 빨리 올리면 플레이어가 튀어 오르는 시간이 길어지고 결국 앞으로 활공을 시작합니다. –

관련 문제