I’m writing a movement script for my game, and it says I’m missing a right brace. Can someone help? Here’s my code.
using UnityEngine;
public class movingroovin : MonoBehaviour {
public Rigidbody rb;
public float sidewaysForce = 500f;
void FixedUpdate()
{
if( Input.GetKey(“d”))
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
{
if( Input.GetKey(“a”))
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0);
{
}
}
Code tags, indentation, counting brackets. Results in…
using UnityEngine;
public class movingroovin : MonoBehaviour
{
public Rigidbody rb;
public float sidewaysForce = 500f;
void FixedUpdate()
{
if (Input.GetKey("d"))
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
}
if (Input.GetKey("a"))
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0);
}
}
}
1 Like
Also: don’t multiply by Time.deltaTime in the above context: by default AddForce already adds whatever force you specify for just the duration of the FixedUpdate() loop. Doing the above will make your physics simulation unreliable and play differently each time.
If you doubt this you can prove it to yourself: put a Rigidbody in a blank scene and in FixedUpdate() simply call AddForce() with the inverse of Physics.gravity value and you will see the net force on your object is perfectly balanced with gravity. 
using UnityEngine;
public class Lift : MonoBehaviour
{
void FixedUpdate ()
{
GetComponent<Rigidbody>().AddForce( -Physics.gravity);
}
}
1 Like
Thanks guys. Helped a lot
1 Like