I have recently made a very simple space ship with space flight controls whereby the ship moves nicely with the camera following it and turns and rotates quite well.
The problem that i am having is that even though the ship has a rigidbody on it, it is just passing through physics objects. I want it so that when the ship collides with say planets or walls it is stopped from doing this. i have tried adding box colliders etc to my ships base model but for some unknown reason it stops my flight system from working I.E it still moves forward but it will no longer apply torque to make it turn.
I have also tried adding a character controller script to it but that allows the ship to cotinue turning but it will not move forward. I am a bit confused could anyone help with this please?
The code i am using for my main ship that has a rigidbody but when i add colliders like box etc it wont move other than forward is this:
function Update () {
var controllerInput = new Vector3(Input.GetAxis("Vertical"), 0, - Input.GetAxis("Horizontal"));
var inputPitch = controllerInput.x;
var inputRoll = controllerInput.z;
var power : float = 10000.0;
rigidbody.AddRelativeTorque(Vector3(inputPitch * 1,0,inputRoll * 1));
rigidbody.AddTorque(Vector3(0,inputRoll * -1,0));
if(Input.GetButton("Jump")){
Debug.Log("We Have Hit the Space Bar!");
rigidbody.AddRelativeForce(Vector3(0,0,power));
}
I had the same problem and finally found the simple solution. If your rigidbody has a collider attached to it, Unity computes a inertiaTensor for it. The inertiaTensor is basically for rotations what mass is for linear movements. You probably just need much larger magnitude for your torque. In my case I had to multiply it with 30000, so get a similar movement. Since the inertiaTensor is computed based on the dimensions of the collider and the mass of the rigidbody, your required torque is probably in a similar magnitude as the force you have to apply for linear movements.
To get the same behavior as without the collider you can also just reset the inertiaTensor to one. However, that will get you unrealistic rotation behavior on collisions. You just did not notice it up to now, because you had no collisions
rigidbody.inertiaTensor = Vector3.one;
the code i have used so far is a bit of a mish mash of basic tutorial file code but it does work, just not the physics interaction. here it is:
function Update () {
var controllerInput = new Vector3(Input.GetAxis("Vertical"), 0, - Input.GetAxis("Horizontal"));
var inputPitch = controllerInput.x;
var inputRoll = controllerInput.z;
var power : float = 10000.0;
rigidbody.AddRelativeTorque(Vector3(inputPitch * 1,0,inputRoll * 1));
rigidbody.AddTorque(Vector3(0,inputRoll * -1,0));
if(Input.GetButton("Jump")){
Debug.Log("We Have Hit the Space Bar!");
rigidbody.AddRelativeForce(Vector3(0,0,power));
}
the code i used was from several basic tutorial files and is as follows:
function Update () {
var controllerInput = new Vector3(Input.GetAxis("Vertical"), 0, - Input.GetAxis("Horizontal"));
var inputPitch = controllerInput.x;
var inputRoll = controllerInput.z;
var power : float = 10000.0;
rigidbody.AddRelativeTorque(Vector3(inputPitch * 1,0,inputRoll * 1));
rigidbody.AddTorque(Vector3(0,inputRoll * -1,0));
if(Input.GetButton("Jump")){
Debug.Log("We Have Hit the Space Bar!");
rigidbody.AddRelativeForce(Vector3(0,0,power));
}