I have a 2d game, where GameObject (GO) moves to the right side on the floor. I want to make it jump.
But when I add Character Controller to my GO and start simulation, it just fast move on the left side. Can't understand, what's wrong.
GO also contain Rigidbody and Box Collider
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
void Start ()
{
player = (GameObject)this.gameObject;
}
void Update ()
{
// Move
player.transform.position +=
player.transform.right * speed * Time.deltaTime;
// Jump
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if(Input.GetKey(KeyCode.Space))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move (moveDirection * Time.deltaTime);
}