- Home /
How to make thrusters affect a rigidbody
I am trying to make some maneuvering thrusters apply a force to my rigidbody spacecraft. The main thruster was easy as the force can be applied to the whole craft, but I want the maneuvering thrusters to apply a force to a specific part of the rigidbody to create pitch, roll, and yaw. I have been messing with the AddForceAtPosition function, but I can not figure out how to position where the force is applied.
Im sorry i know this is a dead thread but... You got it working with some tweaks... Can you show what code you got working so I may learn haw to do the same thing. :) PLEASE!!! :)
Answer by RetepTrun · Feb 28, 2013 at 11:54 PM
Maybe if I explain the script refrence http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForceAtPosition.html
firstly at the top it says "function AddForceAtPosition (force : Vector3, position : Vector3, mode : ForceMode = ForceMode.Force) : void"
Which means you use it by by saying
AddforceAtPosition(force vector wich is a Vector3, position vector which is also a Vector3);
They give this example but I wil add more coments:
function ApplyForce (body : Rigidbody) {// a sub function where you pass it a
//rigidbody and it adds force to it
//here they are making a vector3 to put in for 1st parameter
var direction : Vector3 = body.transform.position - transform.position;
//here they call AddForce on the rigidbody
//rigidbodyVariable.function( parameter 1 , parameter2 );
body.AddForceAtPosition(direction.normalized, transform.position);
}
. .
So what I would do it make some cubes, position them in the engines with Z axis forwards.
Make them children of the ship.
Have a script and add a Transform variable for each cube.
say rigidbody.AddForceAtPosition(cube.position , cube.forward * amountOfForceYouWant);
for each engine
Answer by mcline7008 · Mar 01, 2013 at 05:23 AM
Thanks for the reply. This seems to be working, but I am having trouble making the force vector stay relative to the cube and not the global space.
#pragma strict
var bottomNoseThruster : GameObject;
var topNoseThruster : GameObject;
bottomNoseThruster = GameObject.Find("Bottom_Nose_Thruster");
topNoseThruster = GameObject.Find("Top_Nose_Thruster");
function Start ()
{
}
function FixedUpdate ()
{
if(Input.GetAxis("Pitch") > 0)
{
rigidbody.AddForceAtPosition (topNoseThruster.transform.position, ThrustDirection(topNoseThruster.transform.localRotation) * 2);
}
if(Input.GetAxis("Pitch") < 0)
{
rigidbody.AddForceAtPosition( bottomNoseThruster.transform.position, ThrustDirection(bottomNoseThruster.transform.localRotation) * 2);
}
}
function ThrustDirection(rotate : Quaternion) : Vector3
{
return rotate * Vector3.up;
}
This is what I am working with. Right now the nose pitches up to the top then jerks around as the force is still being applied upwards instead of rotating all the way around.
Thanks for the help I really appreciate it.
You should really add this as a comment to the previous answer. Looking at your code, it looks like you're using your thrusters' .position ins$$anonymous$$d of .localPosition
Thanks for the help. I am still trying to get used to the api. And posting etiquette. Worked perfectly after tweaking a few other things.
Your answer
Follow this Question
Related Questions
Added force to rigid body, but wont move same speed as horizontal speed... if rotating 1 Answer
how to limit horizontal input when jumping 1 Answer
Can't use Rigidbody.AddForce to RaycastHit.point 1 Answer
Rigidbody.AddRelativeForce after modifying gameObject's rotation 1 Answer
Ball moving much faster forward when there is horizontal input 0 Answers