有我的代码: 我在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);
}
}
答案 0 :(得分:0)
我按照教程这是我的代码的样子。您错过了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;
}
}