- Home /
Force AddForce AddForceAtPosition Moving Objects According To Rotation
var MoveForce:float = 10;
var xSensitivity : float;
var ySensitivity : float;
var maxHeight : float;
var minHeight : float;
private var xPos : float;
private var yPos : float;
var myTrans : Transform;
var camTrans : Transform;
function Update () {
yPos = Mathf.Clamp(yPos - Input.GetAxis ("Mouse Y") * ySensitivity, minHeight, maxHeight);
xPos = xPos + Input.GetAxis("Mouse X") * xSensitivity;
myTrans.rotation = Quaternion.AngleAxis(xPos, Vector3.up);
camTrans.localRotation = Quaternion.AngleAxis(yPos, Vector3.right);
rigidbody.SetMaxAngularVelocity(10);
var inputVector : Vector3 = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
rigidbody.AddForce(inputVector * MoveForce, ForceMode.Force);
}
As you can see I use a simple rotation method. Then when you see the var inputVector, that is what stumps me. I will not move according to the direction. Only north south, you get the idea. I do not know whats going on and i have been looking in the reference. I want it to move on the angle by force on the rigidbody. I have x,y,z locked. Is that whats going on? Because I unlocked it too. I'm not using a character controller.
Answer by Flynn · May 04, 2012 at 11:08 PM
Use
tranform.InverseTransformDirection(dir);
To convert local directions to global coordinates.
For example:
rigidbody.AddForce(tranform.TransformDirection(inputVector * MoveForce), ForceMode.Force);
Instead of the last line.
It's the opposite: TransformDirection converts local to world, and InverseTransformDirection converts world to local!
Don't $$anonymous$$d, almost nobody (including me) remembers who's who!
Answer by aldonaletto · May 04, 2012 at 11:14 PM
AddForce uses world space, thus the rigidbody will only move in the world axes. You should transform the inputVector to world space with transform.TransformDirection:
var inputVector: Vector3 = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
inputVector = transform.TransformDirection(inputVector); // transform
rigidbody.AddForce(inputVector * MoveForce); // ForceMode.Force is the default mode
But be aware that you're applying a force to the rigidbody: the force produces an acceleration while it's being applied, thus removing the force (when you release the controls) will not stop the rigidbody - it will continue moving at its last velocity. You can set a big rigidbody.drag value to make it stop quickly, but this will also affect the vertical direction (it will fall in slow motion).
A possible solution is to implement your own horizontal drag function in FixedUpdate:
var horizontalDrag: float = 0.06; // 0 = no drag, 1.0 = infinite drag
function FixedUpdate(){
var vel = rigidbody.velocity; // get the current velocity...
vel *= 1.0 - horizontalDrag; // apply the drag friction...
vel.y = rigidbody.velocity.y; // but keep the original y velocity
rigidbody.velocity = vel; // then apply it to rigidbody.velocity
}
NOTE: Keep rotations freezed in all axes, or your rigidbody will start spinning after any collision!
Your answer
![](https://koobas.hobune.stream/wayback/20220613071107im_/https://answers.unity.com/themes/thub/images/avi.jpg)