My Player flys in the direction of movement

Hey, Any idea how i can change this so this player is always grounded?

using UnityEngine;
public class BasicController : MonoBehaviour
{
    public float sensitivity;
    public CharacterController cc;
    Vector3 velocity;
    Vector3 look;

    void Awake()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    void Update()
    {
        look += new Vector3(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0) * sensitivity;
        look.x = Mathf.Clamp(look.x, -90, 90);
        transform.rotation = Quaternion.Euler(look);
        if (cc.isGrounded) velocity.y = -6;
        Vector3 force = ((transform.right * Input.GetAxis("Horizontal")) + (transform.forward * Input.GetAxis("Vertical"))) * 10;
        velocity = new Vector3(force.x, velocity.y + Physics.gravity.y * Time.deltaTime, force.z);
        cc.Move(velocity * Time.deltaTime);
    }
}

Absolute diamond lad! Thank you!

However, it does seem that MouseX is inverted since when looking left it looks right and vice versa; for this i just inverted the controls on the Project settings but is there a way to invert it through the scrip?

(I tried using “!” and “-” in the script but didnt work unfortunately.)

look += new Vector3(-Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0) * sensitivity;
1 Like

Thank you so much dude. I am so glad discussions exist!

1 Like