- Home /
Rotating on X and Z axis only
How would I make this -
var rotation = Quaternion.LookRotation(player.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damp);
only rotate the object on the X and Z axis? Thanks!
Answer by rutter · Apr 02, 2012 at 09:50 PM
The simplest thing would be if your two objects are on the same plane.
Barring that, you can at least pretend they are by zeroing out the depth axis before calculating your goal rotation:
var displacement = player.position - transform.position;
displacement.z = player.position.z; //pretend objects are at same height
var rotation = Quaternion.LookRotation(displacement); //ed: thanks to Bunny83
By some clever vector multiplication, you could even make that axis-agnostic, but that's probably better left for another day.
If you need more control than that, I suppose you could try something like this:
Get a rotation from
Slerp()
Convert that rotation to its vector form using its `eulerAngles` property
Zero out one or more axes
Convert that back into a rotation using `Quaternion.Euler()`
Also remembers that if you're using rigidbodies, you can set rotation and position constraints locking them on a particular axis, which is sometimes very handy for 2D simulation.
Doesn't seem to work. Either ways. Can't I just see if the player and enemy are on the same Y axis? and it they aren't then not to move the enemy. I just don't know how to go about doing that.
I guess you meant Quaternion.LookRotation(displacement);
Look rotation takes a direction vector, not a position.
Beside that this question has been asked several times before.
Hmm... Has there been a solution? I'm thinking of making a system that checks to see if they are both on the same Y axis if not a bit off from each other. If they aren't then the enemy will follow a set path until the player goes within it's position. Then it checks to make sure they are on the same Y axis and if they are it goes to attack. I just don't see if that would be such a great idea. How would I make an if statement that gets the player.position.y and tranform.position.y then checks to see if they are 3 - 4 meters within the same y axis?
Answer by DracoScorpius · Apr 02, 2012 at 09:46 PM
A solution, if the object this applies to has a Rigidbody, is to freeze the rotation on the Y axis under constraints.
Your answer
Follow this Question
Related Questions
Collisions not working when player stands still 1 Answer
Waypoint System help 1 Answer