hi everyone, I’m quite new to Unity. So I hope someone can help me. I’m writing a movement script. Moving and jumping is okay but the character will move forward itself.
Here’s my script:
public class NewMovement : MonoBehaviour {
public float speed = 6F;
public float sprintSpeed = 10F;
public float jumpSpeed = 8F;
public float gravity = 20F;
private Vector3 moveDirection = Vector3.zero;
CharacterController cc;
Animator anim;
// Use this for initialization
void Start () {
cc = GetComponent<CharacterController> ();
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
if (cc.isGrounded)
{
moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
moveDirection = transform.TransformDirection (moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
}
if (Input.GetKey(KeyCode.LeftShift))
{
moveDirection *= sprintSpeed;
}
Debug.Log("grounded");
}
moveDirection.y -= gravity * Time.deltaTime;
cc.Move (moveDirection * Time.deltaTime);
anim.SetFloat ("Speed", moveDirection.magnitude);
}
void FixedUpdate () {
}
}
I really dun know what the problem is. So plz help!