How I can make my rigid body turn each time I click A,W,S or D

Question is on the title, ive tried to do this only by myself but Im really new at Unity and C# and I failed all the time, so I decided to ask for help here in forums to make this possible, heres the code of my character movement for now:

public class scr : MonoBehaviour
{
    //Variables

    public float speed = 2f;
    private Rigidbody rb;
    private float midJump = 1;
  

    void Start()
    {
        rb = GetComponent<Rigidbody>();

    }

    //Character Movement
 

    void FixedUpdate()
    {
  
 
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

      
        Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);


        rb.AddForce(movement * speed);


    //Character Jump


        if (midJump == 1 && Input.GetKeyDown(KeyCode.Space))
        {
            GetComponent<Rigidbody>().velocity = new Vector3(0, 45, 0);
            midJump = 2;
        }

        else if (GetComponent<Rigidbody>().velocity.y == 0.0)
            midJump = 1;


    }
}

Hi.
Firstly replace GetComponent() for rb, in FixedUpdate.
Then you should use Input in Update, not in FixedUpdate, if you want detect clicks, rather than holding button, or you may miss Input calls.

Other than that, IF condition with Input KeyCode.A/D for turning is required.
Inside need to set , rb.angularVelocity, or rb.AddRelativeTorque for example.

See RigidBody documentation for more details.

2 Likes

Hi!! Thank you so much for the help, I understanded some new concepts now (I just started Unity and C# yesterday sorry im kinda slow), I made some modifications to my code and this is what I got:

public class scr : MonoBehaviour
{
    //Variables

    public float speed = 2f;
    private Rigidbody rb;
    private float midJump = 1;
  

    //Character Movement
 

    void FixedUpdate()
    {
        rb = GetComponent<Rigidbody>();

        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

      
        Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);


        rb.AddForce(movement * speed);


    //Character Jump


        if (midJump == 1 && Input.GetKeyDown(KeyCode.Space))
        {
            GetComponent<Rigidbody>().velocity = new Vector3(0, 45, 0);
            midJump = 2;
        }

        else if (GetComponent<Rigidbody>().velocity.y == 0.0)
            midJump = 1;


    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.D))
            rb.AddRelativeTorque(Vector3.back);

        if (Input.GetKeyDown(KeyCode.A))
            rb.AddRelativeTorque(Vector3.forward);

    }
}

After I hit play nothing happens when I click A or D besides the normal movement, idk what I did wrong here, everything makes pretty much sense for me :confused: Also my Z axis is frozen, idk it that might be the problem, but I dont want my character to interact with other objects, I just want it to spin a certain angle which time I click A/D/W/S

Can you double confirm that Horizontal and Vertical works correctly?
Can you double confirm also, holding space works correctly?

Your value of Vector3.forward may be very small (0,0,1), in respect to mass of rb.
Try respectively Vector3.forward * 10, Vector3.forward * 100, Vector3.forward * 1000.
Similarly Vector3.back.

It appears, you removed

void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

You should keep

rb = GetComponent<Rigidbody>();

in Start.
But that is not the reason for faulty code, unless you got any errors in console?

On side note, replace

GetComponent<Rigidbody>().velocity

with

rb.velocity
1 Like