Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Rigidbody problem

Started by Timothy Sharp Aug 24, 2018 at 6:11 PM 3 replies 1.8k views
Original Post
Timothy Sharp
Timothy Sharp

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);
}
}
}

CrazyCdn
CrazyCdn

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
0r0d
0r0d
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.

Timothy Sharp
Timothy Sharp
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.