Hey, I m currently making a endless runner game but I bumped into a problem, I dont know how to fix. the problem is that the character goes crazy bounces,bumps into objects… anything is helpful… thanks.
script:
using UnityEngine;
using System.Collections;
public class PlayerMotor : MonoBehaviour
{
private CharacterController controller;
private Vector3 moveVector;
private float speed = 5.0f;
private float verticalVelocity = 0.0f;
private float gravity = 5.0f;
private float animationDuration = 3.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) {
verticalVelocity = -0.5f;
} else {
verticalVelocity -= gravity * Time.deltaTime;
}
// X - Left and Right
moveVector.x = Input.GetAxisRaw ("Horizontal") * speed;
// Y - Up and Down
moveVector.y = verticalVelocity;
// Z - Forward and Backward
moveVector.z = speed;
controller.Move (moveVector * Time.deltaTime);
}
public void SetSpeed(float modifier)
{
speed = 5.0f + modifier;
}
// It is beign called every time our capsule hits something
private void OnControllerColliderHit (ControllerColliderHit hit)
{
if (hit.point.z > transform.position.z + controller.radius)
Death ();
}
private void Death ()
{
isDead = true;
GetComponent<Score> ().OnDeath ();
}
}