- Home /
Hi please tell me whats wrong with my player script.
Hi, i am making an endless runner game like temple run and i want my player to move automatically on the z axis when the game starts and to be able able to move to the x and y axis when a key is pressed. But the problem is that my character isnt moving automatically on the z axis as i planned please help me out. i would like to know the problem and you cna please type the right code for me since i dont know how to program and i am using a templete. using UnityEngine; using System.Collections;
public class MoveForward : MonoBehaviour { public float speed = 6.0F; public float jumpSpeed = 8.0F; public float gravity = 20.0F; private Vector3 moveDirection = Vector3.zero;
// Update is called once per frame
void Update () {
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.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
Answer by Megaboy238 · Feb 08, 2021 at 10:20 AM
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 1); Should work