- Home /
transform.Translate but Transform has Rotation!=0
Hi there!
I was testing enemies movements in my wannabe game, and I've noticed a behaviour that I really don't like.
Basically, I got a simple translation function that should move the enemies to a given target Vector 3. Skipping useless details, here's the function itself:
//Your regular Translation function
void Move(Vector3 _target){
//DEBUG
if(Commander.allStandStill){
//Do nuffin!
}
else{
//First we define the direction - target is assumed as specified.
direction= _target - transform.position;
//We normalize it
direction = direction.normalized;
//We set up a Vector used to move you
moveVector = direction * speed * Time.deltaTime;
//We scale this so that speed 1 is the minimum value to move you
moveVector *= 12f;
//And then we move you
//Space.World helps moving decently even with a 25° X rotation
transform.Translate(moveVector.x,0f,moveVector.z, Space.World);
}
}
Odd Things ( number 2 is the focus of this question):
As you can see I have to multiply moveVector by 12f. I don't know why, but if I set the speed <= 11 the enemy doesn't move, while values of 12 or above works. My solution was to multiply the vector by 12f so that a speed of 1 could work ( thus the scale starts at 1 now);
The enemy translates but it doesn't follow a linear direction even though it's supposed to do so. My game is 2.5d so little imprecisions of floats for x/z axes are fine, but these curves are obviously more than just a rushed rounding.
I've managed to track down a bit what I believe is the source of these curves: the object's rotation.
All the object's in my game have 25° of rotation on the X axis. That's my 2 cents in tweaking the perspective a little bit so the game seems deeper ( right now the camera is not ortho, and these 25 degrees really show off).
As a matter of fact, I've already tried the posted code on an enemy with default rotation ( 0) and everything went as expected: no curves or other oddities.
Question is: What would I need to fix those curves and make translation straight with a rotated object?
If you need to know more about my current set up, please let me know!
P.S.: If you think .Translate() is not the best method to use, feel free to point me toward other methods! Thanks for your time
Answer by d2 · Mar 12, 2015 at 09:31 PM
you could change move with MoveToowards instead of translate, something like this:
transform.position = Vector3.MoveTowards(transform.position, _target, speed * Time.deltaTime);
Hi d2! Thanks for you suggestion, I've tried but the result is the same; it seems that Unity handles the .$$anonymous$$oveTowards() under the hood, so you only have one line of code on screen, but does the same things I was doing with my code. Still, it's more pleasant to the eye, so thanks for pointing that out! ( is it also faster?)
Your answer
Follow this Question
Related Questions
Rotatating a character 2 Answers
Moving multiple transforms from an array in a single script 0 Answers
How do I get GameObjects to look at me? 2 Answers
Moving an object 1 Answer
Player movement adding sprint, issues with rotation 0 Answers