我想要一个物体(鸟)在下降时向右旋转

问题描述:

有我的代码: 我在YouTube上观看了一个教程(https://www.youtube.com/watch?v=dWtoB3J1YTg)。 我试图遵循的步骤,但我可能拼错的东西,因为转动的右侧,而下降事情是不是为我工作:( 请帮帮忙!如果您需要了解更多相关信息只是问我!我想要一个物体(鸟)在下降时向右旋转

using UnityEngine; 
using System.Collections; 

public class BirdMovement : MonoBehaviour { 

    Vector3 velocity = Vector3.zero; 
    public Vector3 gravity; 
    public Vector3 flapVelocity; 
    public float maxSpeed = 5f; 
    public float forwardSpeed = 1f; 

    bool didFlap = false; 

    // Do Graphic & Input updates here 
    void Update() { 
     if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) { 
      didFlap = true; 
       } 
      } 

    // Do physics engine updates here 
    void FixedUpdate() { 
     velocity.x = forwardSpeed; 
     velocity += gravity * Time.deltaTime; 

     if (didFlap == true) { 
      didFlap = false; 
      if(velocity.y < 0) 
       velocity.y = 0; 

      velocity += flapVelocity; 
       } 

     velocity = Vector3.ClampMagnitude (velocity, maxSpeed); 

     transform.position += velocity * Time.deltaTime; 

     float angle = 0; 
     if(velocity.y < 0) { 
      angle = Mathf.Lerp (0, -90, velocity.y/maxSpeed); 
     } 

     transform.rotation = Quaternion.Euler (0,0,angle); 


    } 
} 

我跟着教程这是我的代码看起来像你缺少Animator

public class BirdMovement : MonoBehaviour 
{ 
    Vector3 velocity = Vector3.zero; 
    public float flapSpeed = 100f; 
    public float forwardSpeed = 1f; 
    bool didFlap = false; 
    Animator animator; 
    public bool dead = false; 
    float deathCooldown; 
    public bool godMode = false; 

    void Start() 
    { 
     animator = transform.GetComponentInChildren<Animator>(); 

     if(animator == null) 
      Debug.LogError("Failed to find animator!"); 
    } 

    void Update() 
    { 
     if(dead) 
     { 
      deathCooldown -= Time.deltaTime; 

      // This should be an AND but this is how it comes in tutorial. 
      if(deathCooldown <= 0) 
       if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) 
        Application.LoadLevel(Application.loadedLevel); 
     } 

     else if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) 
      didFlap = true; 
    } 


    // Do physics engine updates here 
    void FixedUpdate() 
    { 
     if(dead) 
      return; 

     rigidbody2D.AddForce(Vector2.right * forwardSpeed); 

     if(didFlap) { 
      rigidbody2D.AddForce(Vector2.up * flapSpeed); 
      animator.SetTrigger("DoFlap"); 

      didFlap = false; 
     } 

     if(rigidbody2D.velocity.y > 0) 
      transform.rotation = Quaternion.Euler(0, 0, 0); 

     else 
     { 
      float angle = Mathf.Lerp (0, -90, (-rigidbody2D.velocity.y/3f)); 
      transform.rotation = Quaternion.Euler(0, 0, angle); 
     } 
    } 

    void OnCollisionEnter2D(Collision2D collision) { 
     if(godMode) 
      return; 

     animator.SetTrigger("Death"); 
     dead = true; 
     deathCooldown = 0.5f; 
    } 
}