- Home /
How to lock Y Rotation
Hello, I'm trying to make an ai code that follows and rotates towards the player. The code I'm using is.
var rotation = Quaternion.LookRotation(player.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damp);
Is there anyway to lock the Y axis or make sure it doesn't rotate on the Y axis? thanks!
Answer by OzzyRawrz · Apr 10, 2012 at 04:02 AM
Make a new empty Object with a box collider and set the size of the collider for the distance you want to start the script on colliding with the Player. place the empty game object in the Enemy you made as a children and try this on the EmptyObject it will work.
var Myself : Transform;
var WalkSpeed : float;
var Player : Transform;
var PlayerAlert : int;
function Start () {
}
function Update () {
if(PlayerAlert == 1){
Myself.rotation = Quaternion.LookRotation(Vector3(Player.position.x, Myself.position.y, Player.position.z) - Myself.position);
Myself.transform.position += Myself.transform.forward * WalkSpeed * Time.deltaTime;
}
function OnTriggerEnter(other : Collider){
if(other.gameObject.CompareTag("Player")){
PlayerAlert = 1;
}
}
it will follow the player....set the myself to the main enemy object. and put a tag "player" on the object you want this to follow.
This honestly has nothing to do with my question. This is just a peace of code, I already have my AI system done. What I'm asking is how to make sure the AI doesn't not rotate on the Y Axis...
Use this code...
$$anonymous$$yself.rotation = Quaternion.LookRotation(Vector3(Player.position.x, $$anonymous$$yself.position.y, Player.position.z) - $$anonymous$$yself.position);
Answer by BiG · Apr 10, 2012 at 09:46 PM
If you want to lock the y-axis rotation for all the duration of the game, look at the Inspector of your object on which that script is attached. Go under Rigidbody->Constraints->Freeze Rotation. At that point, just check the "Y" box.
Answer by by0log1c · Apr 10, 2012 at 10:36 PM
Well I had written thislast night and deleted because it felt out-of-place. Its still not clear if its what you're looking but... here. What I often do is create a helper function that returns any position with my own Y, so my actors keep their feet on the ground, JS example:
function CleanPosition(position:Vector3):Vector3
{
position.y = transform.position.y;
return position;
}
Then anytime you LookAt() or Move(), you should send the vector3 through this method. It might not blend so well into your already written AI, but perhaps it will. Its simple and worked well for me.
Your answer
Follow this Question
Related Questions
Cube Rotation Problem 0 Answers
How to Rotate on one axis? 4 Answers
Problem changing 3rd Person controller rotation 1 Answer
Character rotation on foot? 1 Answer
Need to change direction of transform? 2 Answers