The best way to make motion relative to participant rotation utilizing Rigidbody.AddForce() in 3D Unity?


I am making a 3D recreation and I used to be attempting to make motion relative to participant rotation for a number of hours, however I failed. Here is the code, possibly I made a mistake there. Additionally, participant would not rotate.

public class PlayerScript : MonoBehaviour
{
    personal Quaternion rotat; //rotation of the digital camera
    public Remodel cam; //digital camera
    bool IsOnGround = true;
    public float pace = 5;
    personal Rigidbody rb;
    void Begin()
    {
        rb = GetComponent();
    }

    void FixedUpdate()
    {
        
        rotat = Quaternion.Euler(0, cam.rotation.y, 0);
        IsGrounded();
        if (Enter.GetKey(KeyCode.W))
        {
            rb.AddForce(rotat * Vector3.ahead * pace, ForceMode.Impulse);
        }
        if (Enter.GetKey(KeyCode.A))
        {
            rb.AddForce(rotat * Vector3.left * pace, ForceMode.Impulse);
        }
        if (Enter.GetKey(KeyCode.S))
        {
            rb.AddForce(rotat * Vector3.again * pace, ForceMode.Impulse);
        }
        if (Enter.GetKey(KeyCode.D))
        {
            rb.AddForce(rotat * Vector3.proper * pace, ForceMode.Impulse);
        }
        if (Enter.GetKey(KeyCode.House) && IsOnGround == true)
        {
            IsOnGround = false;
            rb.AddForce(Vector3.up * (pace / 10), ForceMode.Impulse);
        }
    }
    public bool IsGrounded() {
        RaycastHit hit;
        float rayLength = 1.12f; 
        if (Physics.Raycast(remodel.place, Vector3.down, out hit, rayLength)) {
            IsOnGround = true;
            return true;
        }
        IsOnGround = false;
        return false;
    }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles