- Home /
Switch force from horizontal to vertical
Hello everyone, i am attempting to make a VTOL(vertical take off and landing) aircraft, kind of like the CoD MW2 Osprey, and i want the thrust force to be able to apply on the angle of the engine (not only 0 and 90 but any angle in between). here is the way i am trying to do it:
public class Flight_Script : MonoBehaviour {
public float acceleration;
public float thrust;
public float thrustDir;
public Vector3 vectorThrust;
void Update () {
//thrust
vectorThrust = new Vector3 (thrust * Mathf.Cos(thrustDir), thrust * Mathf.Sin(thrustDir), 0);
thrustDir -= Input.GetAxis ("Mouse ScrollWheel") * 10;
thrustDir = Mathf.Clamp (thrustDir, 0, 90);
thrust += Input.GetAxis ("Vertical") * acceleration;
}
btw i deleted a lot from this script that has nothing to do with the thryst, if i have a missing bracket i probably deleted it by mistake. anyway, the problem i have is that when the engine rot is 90 degrees the x is something like a 45% of the thrust, and the y force is something like an 89% of the thrust. i have recently seen 2D vectors in physics and if i recall corectly that is how its done. thank you all
Answer by robertbu · Mar 28, 2014 at 11:38 PM
One issue is that Mathf.Cos() and Mathf.Sin() takes radians for input, not degrees, so this line should be:
vectorThrust = new Vector3 (thrust * Mathf.Cos(thrustDir * Mathf.Deg2Rad), thrust * Mathf.Sin(thrustDir * Mathf * Deg2Rad), 0);
Note as an alternative, you could do use an Quaternion.AngleAxis() for the rotation. Something like:
vectorThrust = Quaternion.AngleAxis(thrustDir, Vector3.forward) * Vector3.right;
Based on your code above, I'm assuming this is a 2D game. I don't know your specific setup. You may want to use another base vector (Vector3.left, Vector3.down), and you may need Vector3.back instead of Vector3.forward. But the concept is the same. You are just picking some base vector and rotating it around the word 'z' axis by angle.
thanks a lot, it is actualy a 3d game but i dont need the plane engines to rotate left and right, at lest for now. but thanks for noting that, since its a 3d game, i should probably take in account that when i use the actual engine rotation it should use localRotation.
Your answer
Follow this Question
Related Questions
rigidbody problem 1 Answer
Why the object being moved is faster than the rest? 1 Answer
Get/set vector 3 causing problems 0 Answers
How to make a Vector with the same angle as a certain object? 1 Answer
Rotational Force 0 Answers