- Home /
How exactly do you AddForceAtPosition with in LocalRotation?
Apologizes in advance from a noob :) I'm pretty new to Unity and coding in general and I'm having a few problems.
I have a flat-ish cube and I am trying to AddForceAtPosition in each corners transform.localRotation (Y axis) depending on gamepad input. However, the force is only ever applied in the Y axis of the world space (straight up).
I'm basically trying to make a Quadcopter. I'm sure there are easier ways of faking the same sort of movement but I'm more interested in the process of how the mixing of the motors works in real life.
Here's the code I made.....
public Vector3 frontLeftRotor; //location of Rotors
public Vector3 frontRightRotor;
public Vector3 rearLeftRotor;
public Vector3 rearRightRotor;
public float speed; //Throttle Multiplier
float currentThrottle = 0.0f; //throttle
float currentRotation = 0.0f; //Rotation
float currentForwardBias = 0.0f; //forward lean
void Update()
{
currentThrottle = state.ThumbSticks.Left.Y; // Get Input (XinputDotNet)
currentRotation = state.ThumbSticks.Left.X;
currentForwardBias = state.ThumbSticks.Right.Y;
Vector3 rotorLiftFrontLeft = new Vector3 (0, currentThrottle * speed, 0); //Set Direction and Throttle
Vector3 rotorLiftFrontRight = new Vector3 (0, currentThrottle * speed, 0);
Vector3 rotorLiftRearLeft = new Vector3 (0, currentThrottle * speed, 0);
Vector3 rotorLiftRearRight = new Vector3 (0, currentThrottle * speed, 0);
player1.AddForceAtPosition(rotorLiftFrontLeft,(frontLeftRotor)); //AddForceAtPosition, this much force, at this position
player1.AddForceAtPosition(rotorLiftFrontRight,(frontRightRotor));
player1.AddForceAtPosition(rotorLiftRearLeft,(rearLeftRotor));
player1.AddForceAtPosition(rotorLiftRearRight,(rearRightRotor));
transform.localRotation *= Quaternion.Euler (currentForwardBias, currentRotation, 0); //Only here for testing, adds lean to cube to test AddForceAtPosition direction
}
Any help would be appreciated.
Answer by AlwaysSunny · Apr 26, 2015 at 07:44 PM
When you can, use empty child game objects to help with things like finding a specific point on a model. Place and name the child objects and let the script see them by exposing Transform variables. Your script can apply force at the position of the empties. Using extra helper empties can be very useful.
You should know that creating a good looking, good feeling, stable quad-copter simulation in pure PhysX is really not feasible. Not without extensive Vector Math, Physics, Unity, and PhysX experience. Even then you'd wind up taking such explicit control over what PhysX does, you might as well not use it (except for collision and simple motion). If you had the necessary experience to pull it off, you'd also know you'd be better served faking 90% of its dynamics.
When someone tried to tell me this years ago, I had to prove it to myself to believe it, so by all means, give it a shot. It will be very educational, but curb your expectations.
Heh! Thanks for the heads up on the difficulty setting, it may be more than a little out of my league :) But I shall persevere for now & I shall certainly look into using empty gameobjects to begin with :)