- Home /
Player movement backward bouncing
Hi All,
Basically I have set up a move and camera rotate script. This allows the player to move in the direction of where ever the camera is facing. I 1 issue and that is when the player moves backwards they start to bounce. Code below.
public float speed = 10.0f;
private float jumpForce = 8f;
private float gravity = 30f;
private Vector3 moveDir = Vector3.zero;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
CharacterController controller = gameObject.GetComponent<CharacterController>();
if (controller.isGrounded)
{
//Getting input
moveDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
//Telling put to change the transform
moveDir = Camera.main.transform.TransformDirection(moveDir);
//MoveDir is * by the speed to make it go
moveDir *= speed;
//Looking for Jump input then setting moveDir Y to jumpForce
if (Input.GetButtonDown("Jump"))
{
moveDir.y = jumpForce;
}
}
moveDir.y -= gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime);
}
}
Comment