- Home /
Make object move in the direction is facing when rotated
Hi there!
I've this problem where I manage to add the velocity to the game object. However, once I try to change it's direction (using rotate), the object just keeps the previous momentum, without updating to the current direction.
So, how can I make my object change its vector direction every time I rotate it?
Here's the current code:
//<---------------movement------------------------------>
//these are to be replaced through stored variables
var agility : float = 90.0; //Agility of the craft, soon to be controled through stored variables. In degrees per second.
var speed : float = 10.0; //Speed of the craft, soon to be controled through stored variables
//frame dependent vars
var amountToMove : float; //amount of the movement
var agilityFrame : float; //amount of rotation
var frameMulti : float = 20; //controls the movement multiplier (higher values mean the action occurs faster)
//control vars
var curSpeed : float; //this one checks the object current speed
var wheelStatus : float; //this one controls the state of the mouse wheel, between 0 and 1;
//this function controls the ship movement
function movement() {
agilityFrame = agility * Time.deltaTime * frameMulti;
//Movement
//obtain movement variables
var vert = Input.GetAxis("Vertical") * agilityFrame;
var horz = Input.GetAxis("Horizontal") * agilityFrame;
var rot = Input.GetAxis("Rotate") * agilityFrame;
//here we rotate the ship
transform.Rotate(vert,horz,rot);
//obtain speed variables
amountToMove = speed * Time.deltaTime * 20;
if (wheelStatus < 1 && wheelStatus > -1)
{
wheelStatus += Input.GetAxis("ShipSpeed");
}
if(wheelStatus > 1)
{
wheelStatus = 1;
}
if(wheelStatus < -1)
{
wheelStatus = -1;
}
fwd = wheelStatus * amountToMove;
//give speed to the ship
rigidbody.velocity.z = fwd;
//place current velocity into control variable
curSpeed = rigidbody.velocity.magnitude;
//make sure that current speed isn't bigger that maximum speed or smaller than minimum speed
if (curSpeed > speed)
{
rigidbody.velocity.z = speed;
}
if (curSpeed < -speed)
{
rigidbody.velocity.z = -speed;
}
//reassign current velocity into control variable
curSpeed = rigidbody.velocity.z;
}
So, how can I make this work?
I appreciate any answers. JPB18
Answer by ScroodgeM · Jul 28, 2012 at 07:27 PM
rigidbody.velocity is world-based speed
you should use
http://docs.unity3d.com/Documentation/ScriptReference/Transform.InverseTransformDirection.html
this to convert world to local speed. example:
rigidbody.velocity = transform.InverseTransformDirection(Vector3.forward) * speed;
or
rigidbody.velocity = transform.forward * speed;
Thank you for your answer, however I'd like to know one thing: How can I know the speed of the object (independently of the direction its going)? I'm gonna need it for some other parts of the game, like the damaged caused by a collision, p.e..
object's speed in world space:
rigidbody.velocityobject's speed in local space:
transform.InverseTransformDirection(rigidbody.velocity);object's speed in local forward direction:
Vector3.Dot(transform.forward, rigidbody.velocity);
Your answer
Follow this Question
Related Questions
Object doesn't turn at the desired rate 1 Answer
How to make bullets moving proportionally faster than player? 4 Answers
Moving a wheel character 0 Answers