- Home /
Adding force to rigidbody
Hi! I'm trying to make a game which involves flying a rocket. The code I have for the force is:
function Update () {
rigidbody.AddForce(new Vector3(Input.GetAxis("Horizontal") * 2, 0, 0));
rigidbody.AddForce(new Vector3(Input.GetAxis("Vertical") * 5, 0, 0));
}
The horizontal works, because when the rocket is on the ground I can make it lean with left arrow and right arrow. The vertical, however, doesn't work as intended. The rocket slides at an angle, and stays on the ground. Help??
Just re-wrote the above code, and now all it does (horizontal or vertical) is slide on an angle.
It probably doesn't help that you are putting both the horizontal and vertical forces into basically the same thing. Try using something more like
var horizontalForce = Input.GetAxis("Horizontal");
var verticalForce = Input.GetAxis("Vertical");
rigidbody.AddForce(new Vector3(horizontalForce, verticalForce, 0);
However, if you want the rocket to rotate, this is still probably not what you want.
Answer by syclamoth · Oct 21, 2011 at 05:16 AM
Instead of using rigidbody.AddForce, try using rigidbody.AddTorque. AddForce adds a force to the centre of gravity of the rigidbody- which doesn't change the angle of the object! Use AddTorque to rotate an object the way you want to.
Also, try using transform.TransfomPoint(inputVector) to transform from local coordinates to global coordinates! That way, your inputs will always be the same relative to the position and rotation of the rocket.
Thanks! Perhaps I should explain more...
I want the rocket to be launched by pressing space, apart from that the player will have no control (no it's not a rocket launcher weapon) Also, the force should stop after a variable amount of time depending on which "engine" the player has selected.
This should be pretty simple, but I can't seem to get it to work.
Well, if the player isn't supposed to have any control over the rocket after it's been launched, why are you reading off the input axes?
Try doing it with some kind of timer-
var thrustTime = 10;
var power = 1000;
function Update()
{
if(thrustTime >= 0)
{
rigidbody.AddForce(-transform.up * power);
thrustTime -= Time.deltaTime;
}
}
When the rocket is on the ground, allow the player to rotate it with the horizontal axis, and make sure its rigidbody is kinematic. Then, when the time comes to launch it, set thrustTime to the amount of time your current engine allows, and set power to the amount of power your engine should have, set rigidbody.is$$anonymous$$inematic to false, and watch your space program fly!
Hmmm... I must be doing something stupid, because no matter how high I set the power, the rocket jumps slightly, then falls slooooooowly back to the terrain.
I suggest some changes in the @syclamoth code:
1- move it to FixedUpdate to have frame rate independent results;
2- declare thrustTime as a float;
3- change -transform.up to transform.up;
var thrustTime: float = 10; var power: float = 1000;
function FixedUpdate(){ if (thrustTime >= 0){ rigidbody.AddForce(transform.up * power); thrustTime -= Time.deltaTime; } }
FixedUpdate doesn't help with being frame-rate independent, exactly, but @aldonaletto is correct in saying that rigidbody physics should be put there anyway, because FixedUpdate only gets called once per physics step, ins$$anonymous$$d of every single frame. I think there should be a Time.deltaTime in there somewhere, too- just to make things more generally sane (and consistent between different physics step times)