Problem with AddForceAtPosition

Hey, i am new to unity, yet i am already trying to make a game that is heavy on physics… actualy more of an space ship simulator…

but i have some problems when i want to use AddForceAtPosition for thrusters, because i dont know hot to get the engine to add force with local vectors and not with global ones…

actualy to be more specific, when i use 2 thrusters (1 5m infront of the rigidbody center, and 1 5m back from the center) the spining of the ship works ok, but when i tilt the ship down, the ship spins around the global y axis and not around the local y axis of the ship…

i want it to always spin around the local axis…

my code:

var shipBody_GameObject : GameObject;
var engine_GameObject : GameObject;
var cockpit_GameObject : GameObject;

var secEngFR_GameObject : GameObject;
var secEngFL_GameObject : GameObject;
var secEngBL_GameObject : GameObject;
var secEngBR_GameObject : GameObject;

var mainEng_Force : float = 100000.0;

var secEng_Force : float = 50000.0;


function FixedUpdate () 
{											);													
	if(Input.GetKey("w"))
	{
		rigidbody.AddRelativeForce(Vector3.forward * mainEng_Force);
	}
	if(Input.GetKey("s"))
	{
		rigidbody.AddRelativeForce(-Vector3.forward * mainEng_Force);
	}
	if(Input.GetKey("a"))
	{
		rigidbody.AddRelativeForce(Vector3.left * mainEng_Force);
	}
	if(Input.GetKey("d"))
	{
		rigidbody.AddRelativeForce(Vector3.right * mainEng_Force);
	}
	
	//****SPIN LEFT****
	if(Input.GetKey("q"))
	{
		//trenutnaPoz means currentPosition
	    trenutnaPoz = secEngFR_GameObject.transform.localPosition;
		rigidbody.AddForceAtPosition(Vector3.left * secEng_Force, trenutnaPoz);
		
		trenutnaPoz2 = secEngBL_GameObject.transform.localPosition;
		rigidbody.AddForceAtPosition(Vector3.right * secEng_Force, trenutnaPoz2);
	}
	//****SPIN RIGHT***
	if(Input.GetKey("e"))
	{
	    trenutnaPoz = secEngBR_GameObject.transform.localPosition;
		rigidbody.AddForceAtPosition(Vector3.left * secEng_Force, trenutnaPoz);
		
		trenutnaPoz2 = secEngFL_GameObject.transform.localPosition;
		rigidbody.AddForceAtPosition(Vector3.right * secEng_Force, trenutnaPoz2);
	}
	
	//****TILT DOWN****
	if(Input.GetKey("r"))
	{
		trenutnaPoz = secEngFR_GameObject.transform.localPosition;
		rigidbody.AddForceAtPosition(Vector3(0, -1, 0) * secEng_Force, trenutnaPoz);
		
		trenutnaPoz2 = secEngFL_GameObject.transform.localPosition;
		rigidbody.AddForceAtPosition(Vector3(0, -1, 0) * secEng_Force, trenutnaPoz2);
		 
		trenutnaPoz3 = secEngBR_GameObject.transform.localPosition;
		rigidbody.AddForceAtPosition(Vector3.up * secEng_Force, trenutnaPoz3);
		
		trenutnaPoz4 = secEngBL_GameObject.transform.localPosition;
		rigidbody.AddForceAtPosition(Vector3.up * secEng_Force, trenutnaPoz4);
	}
}

thank you for your help in advance!

You may want to try this: Unity - Scripting API: Rigidbody.AddRelativeForce

thak you… as you may see in my code, i allerady used AddRelativeForce for moving WSAD, would you please explain how could i use it for spining.

Ok, sorry I read your question a bit quickly there. Try using your thrusters’ transform.up, right and forward vectors (these are in world space) instead of using absolute vectors such as Vector3.left for the AddForceAtPosition calls.

It’s not important for testing, but you may want to cache your rigidbody and transform references in local variables at some point, since you are using them a lot.