- Home /
Rigidbody.AddForce strange with L/R movement?
In my present project, the player is a script-controlled rigidbody with full movement in space (inputs for up, down, left, right, forward, and back. I would like her to accelerate very quickly in a given direction when the button is initially pressed, then continue to accelerate slowly as long as it is held down.
With the following script, this works as intended for all directions except left and right. Is there something about rigidbodies I'm overlooking? Thanks.
#pragma strict
var upInput = false;
var downInput = false;
var leftInput = false;
var rightInput = false;
var forwardInput = false;
var backInput = false;
private var upBoost = false;
private var downBoost = false;
private var leftBoost = false;
private var rightBoost = false;
private var forwardBoost = false;
private var backBoost = false;
var forceSpeed = 1;
var forceBoost = 10;
@script RequireComponent(Rigidbody, CapsuleCollider)
function Awake ()
{
rigidbody.useGravity = false;
}
function Update(){
upInput = Input.GetButton ("Up");
upBoost = Input.GetButtonDown ("Up");
downInput = Input.GetButton ("Down");
downBoost = Input.GetButtonDown ("Down");
rightInput = Input.GetButton ("Right");
rightBoost = Input.GetButtonDown ("Right");
leftInput = Input.GetButton ("Left");
leftBoost = Input.GetButtonDown ("Right");
forwardInput = Input.GetButton ("Forward");
forwardBoost = Input.GetButtonDown ("Forward");
backInput = Input.GetButton ("Back");
backBoost = Input.GetButtonDown ("Back");
}
function FixedUpdate (){
if (rightInput){
rigidbody.AddRelativeForce (Vector3.right*forceSpeed);
}
if (rightBoost){
rigidbody.AddRelativeForce (Vector3.right*forceBoost);
}
if (leftInput){
rigidbody.AddRelativeForce (Vector3.left*forceSpeed);
}
if (leftBoost){
rigidbody.AddRelativeForce (Vector3.left*forceBoost);
}
if (forwardInput){
rigidbody.AddRelativeForce (Vector3.forward*forceSpeed);
}
if (forwardBoost){
rigidbody.AddRelativeForce (Vector3.forward*forceBoost);
}
if (backInput){
rigidbody.AddRelativeForce (Vector3.back*forceSpeed);
}
if (backBoost){
rigidbody.AddRelativeForce (Vector3.back*forceBoost);
}
if (upInput){
rigidbody.AddForce (Vector3.up*forceSpeed);
}
if (upBoost){
rigidbody.AddForce (Vector3.up*forceBoost);
}
if (downInput){
rigidbody.AddForce (Vector3.down*forceSpeed);
}
if (downBoost){
rigidbody.AddForce (Vector3.down*forceBoost);
}
}
Comment
Best Answer
Answer by robertbu · Sep 30, 2013 at 05:46 AM
You have an error on line 34. You are getting the "Right" button for 'leftBoost'. It should be:
leftBoost = Input.GetButtonDown ("Left");
Your answer
![](https://koobas.hobune.stream/wayback/20220613115037im_/https://answers.unity.com/themes/thub/images/avi.jpg)