Making an object face a target

I have tried many scripts and went through the forum for over two hours searching. I Tried the Turret from the FPS tutorial. I can’t get this working on only one axis.

I want an enemy to face in a target direction. The entire body of the enemy. Not snap though. The problem is that every script I have tried has it changing all axis’s of its rotation instead of just the Y. So. If there is a rock the enemy needs to look at and its up a little higher or lower he should not be rotating his entire body up or down to see it while turning. I just want the enemy “object” as a whole to face the rock without changing its other axis’s Only the Y.

Any idea how to do this?

Well, you could rotate towards it on multiple axis using those scripts and just add these lines in:

transform.eulerAngles.x = 0;
transform.eulerAngles.z = 0;

You’d probably have to change it to stop rotating when the y was close enough instead of all three, too, but that shouldn’t be too difficult…

Thank you! That seems to be working perfectly. The only thing is I am wondering if there is a way to constrain the script to only using one axis in the first place rather than always locking the other ones nonstop since its a tiny bit extra code to execute all the time and also would cause some issues if I did need to have the other axis’s change later on.

There really isn’t if you’re using someone else’s code. If you understand how it works and can modify to only rotate on one axis, that would also solve the problem.

However, that doesn’t appear to be an option or you would’ve done it already.

That is the first question I asked. How do you rotate an object on only one axis. lol

I guess I am not the only one who doesn’t understand this or something.

How about this solution… Create an empty object and place it at localPosition (0,0,0) for the object you want rotated. Then use this code and attach the empty to the looker variable. The target you’re rotating toward should be in targeti. (Please note that this is impromptu code and may contain errors)

var looker : Transform;
var targeti : Transform;

var turnSpeed : float = 90;

function Update()
{
   if (targeti) {
      looker.LookAt(targeti);
      RotateToward();
   }
}

function RotateToward()
{
   if (Mathf.Abs(transform.eulerAngles.y - looker.eulerAngles.y) < .5) {
      transform.eulerAngles.y = looker.eulerAngles.y;
   }
   else {
      if (transform.eulerAngles.y > 180) {
         if (looker.eulerAngles.y > 180) {
            if (looker.eulerAngles.y > transform.eulerAngles.y) {
               transform.Rotate(0, turnSpeed * Time.deltaTime, 0);
            }
            else {
               transform.Rotate(0, -turnSpeed * Time.deltaTime, 0);
            }
         }
         else {
            if (transform.eulerAngles.y - looker.eulerAngles.y < 180) {
               transform.Rotate(0, -turnSpeed * Time.deltaTime, 0);
            }
            else {
               transform.Rotate(0, turnSpeed * Time.deltaTime, 0);
            }
         }
      }
      else {
         if (looker.eulerAngles.y < 180) {
            if (looker.eulerAngles.y > transform.eulerAngles.y) {
               transform.Rotate(0, turnSpeed * Time.deltaTime, 0);
            }
            else {
               transform.Rotate(0, -turnSpeed * Time.deltaTime, 0);
            }
         }
         else {
            if (looker.eulerAngles.y - transform.eulerAngles.y < 180) {
               transform.Rotate(0, turnSpeed * Time.deltaTime, 0);
            }
            else {
               transform.Rotate(0, -turnSpeed * Time.deltaTime, 0);
            }
         }
      }
   }
}

If you find a better technique than my quickie, feel free to replace the RotateToward() function with it. I hope it helps! :smiley: