🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Rigidbody problem

Started by
3 comments, last by Timothy Sharp 5 years, 10 months ago

Hi there. I am a beginner programmer. I have a problem with Rigidbody.AddForce. I can get my cube to go fowards and backwards, but not sideways. Here is my code.

 



 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour {

    //Refence to our Rigidbody
    public Rigidbody rb;

    //Reference to our ForwardForce
    public float ForwardForce = 500f;

    //Reference to our SidewaysForce
    public float SidewaysForce = 500f;

   void FixedUpdate()
    {
        //When "w" is pressed, we
        //Add force along the Z-Axis
        if (Input.GetKey(KeyCode.W))
        {
            rb.AddForce(0, 0, ForwardForce * Time.deltaTime);
        }

        //When "s" is pressed, we
        //Add force along the -Z-Axis
        if (Input.GetKey(KeyCode.S))
        {
            rb.AddForce(0, 0, -ForwardForce * Time.deltaTime);
        }

        //When "a" is pressed, we
        //Add force along the X-Axis
        if (Input.GetKey(KeyCode.A))
        {
            rb.AddForce(SidewaysForce * Time.deltaTime, 0, 0);
        }

        //When "d" is pressed, we
        //Add force along the -X-Axis
        if (Input.GetKey(KeyCode.A))
        {
            rb.AddForce(-SidewaysForce * Time.deltaTime, 0, 0);
        }
    }
}

Advertisement

What does it do when you press A or D?  What have you done to debug this code?  Typically people wrap their code in the <> coding style feature to make it easier to read also.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

3 hours ago, Timothy Sharp said:

 




 

 

        //When "d" is pressed, we
        //Add force along the -X-Axis
        if (Input.GetKey(KeyCode.A))
        {
            rb.AddForce(-SidewaysForce * Time.deltaTime, 0, 0);
        }
 

Well, there's at least one obvious problem with the code above.  I'll let you figure it out.

1 hour ago, 0r0d said:

Well, there's at least one obvious problem with the code above.  I'll let you figure it out.

Yea someone already pointed that out on the game dev league discord

This topic is closed to new replies.

Advertisement