我使用C#在Unity 2018上制作了一个3D无限赛跑游戏,每次升级时我的角色都会加速。但问题是他并没有逐渐增加,相反,他从正常到非常快,很难控制他。这是我的代码:
public class PlayerController : MonoBehaviour
{
private CharacterController controller;
private float speed = 2.0f;
private Vector3 moveVector;
private float vertivalVelocity = 0.0f;
private float gravity = 12.0f;
private float animationDuration = 2.0f;
private bool isDead = false;
// Use this for initialization
void Start ()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update ()
{
if (isDead)
return;
if(Time.time < animationDuration)
{
controller.Move(Vector3.forward * speed * Time.deltaTime);
return;
}
moveVector = Vector3.zero;
if(controller.isGrounded)
{
vertivalVelocity = -0.5f;
}
else
{
vertivalVelocity -= gravity * Time.deltaTime;
}
//x - Left and Right
moveVector.x = Input.GetAxisRaw("Horizontal") * speed;
//y - Up and Down
//z - Foward and Backward
moveVector.z = speed;
controller.Move((moveVector * speed) * Time.deltaTime);
}
public void SetSpeed(float modifer)
{
speed = 2.0f * modifer;
}
//it is being called everytime our capsule hits something
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.point.z > transform.position.z + 0.1f && hit.gameObject.tag == "Enemy")
Death();
}
private void Death()
{
isDead = true;
GetComponent<Score>().OnDeath ();
}