- Home /
Adjust object forward to force direction
I am pushing a cube around on a plane using AddRelativeForce. Expectedly, the cube does not change it's orientation when doing this. How can I orient the cube so that the positive Z is always facing the direction that the object is moving. In other words, if I created a direction bt taking the position of the last frame and the position of the current frame, how would I point the Z of the cube in that direction?
var xdirection : float; var ydirection : float;
function Update(){ //This in the user calibrated acceleration input code xdirection = Input.GetAxis ("Vertical") ; ydirection = Input.GetAxis ("Horizontal") ; }
function FixedUpdate () {
var relativeForce = Vector3(ydirection, 0, xdirection);
rigidbody.AddRelativeForce (relativeForce);
}
Answer by Mike 3 · May 07, 2010 at 06:38 PM
something along the lines of this
var xdirection : float; var ydirection : float;
function Update(){ //This in the user calibrated acceleration input code xdirection = Input.GetAxis ("Vertical") ; ydirection = Input.GetAxis ("Horizontal") ; }
function FixedUpdate () {
var relativeForce = Vector3(ydirection, 0, xdirection);
rigidbody.AddRelativeForce (relativeForce);
if(relativeForce.magnitude > 0.01)
rigidbody.rotation = Quaternion.LookRotation(relativeForce);
}
That's it $$anonymous$$ike. I just needed to change AddRelativeForce to AddForce so the cube would continue the direction indicated by 'relativeForce'. Guess I should change that variable name!
Your answer
Follow this Question
Related Questions
How can I convert velocity/direction to Force? 3 Answers
AddRelativeForce to another object 1 Answer
Why is force only being added in the same direction? 1 Answer
AddForce makes cube 2 Answers