- Home /
Rotate a GameObject [set front]
I have two GameObjects. one is the player, and one is the enemy. I have a code that rotates the enemy to the player's direction:
Quaternion.LookRotation(Target.position - transform.position);var rotation =
Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping); The problem is that the ememy's front is the back. The enemy turns his back to the player, instead of his front. (not a code problem) How can I fix it? Thank you, and sorry for my Englishtransform.rotation =
Answer by MSpiteri · Jun 05, 2014 at 10:36 AM
You have to make sure that the PARENT gameobject of the enemy has its Z-axis pointing in the enemy's FORWARD direction. This is VERY important when grouping objects.
To fix it, it depends on how the parent-children are in your game.
They both have the same parent. How can I fix it? (it's not a problem to remove them from this parent)
Can you paste a screenshot of the Hierarchy panel so I can see how the enemy is constructed?
http://gyazo.com/a0c4894bcf26c243d452f2945d9a7ecd Please tell me if you need anything else... And thank you!
Sorry for the late reply.
Ok, so you have that object called "Enemy" in the list. If you click on it and look at the "Scene" view, you will see the arrows for the x, y and z direction. The Enemy will move in the direction of the Z-ARROW (blue arrow).
($$anonymous$$ake sure that the settings at the top are "Pivot" and "Local").
So, most probably, your problem is that the Z-ARROW is pointing in the opposite direction. To resolve this, you can do this:
(1) Create a new empty gameobject and call it "test".
(2) $$anonymous$$ove all the children of "Enemy" inside "test". So "Enemy" will not have any children now.
(3) Rotate "Enemy" by 180 degrees around the Y-axis. So now the Z-ARROW of "Enemy" will point in the opposite way.
(4) $$anonymous$$ove back the children from "test" to "Enemy".
(5) Delete "test".
After the above, it should be ok :)
Your answer
Follow this Question
Related Questions
How should I rotate a 2D unit vector by X degrees? 1 Answer
Set rotation based on 2 points problem 1 Answer
Rotate but keep direction 1 Answer