Loui Augst
Loui Augst

Reputation: 1

Point game object to another only on the Local X Axis

I would like to have a Gameobject point to another only on the Local X-axis.

         void FixedUpdate()
         {
             if(started){
                 Vector3 targetPosition = target.position;
                 Vector3 direction = Vector3.ProjectOnPlane(targetPosition - transform.position, transform.right);
                 Quaternion lookRot = Quaternion.LookRotation(direction, transform.right);
                 transform.rotation = Quaternion.RotateTowards(transform.rotation, lookRot, Time.fixedDeltaTime * 45);
             }

On the y-axis it worked with transform.up instead of transform.right, but on the x-axis the Gameobject only rotates permanently around the z-axis.

Upvotes: 0

Views: 644

Answers (1)

zyapguy
zyapguy

Reputation: 99

You could do something like this:

Vector3 beforeRot = transform.eulerAngles;
transform.LookAt(gameObjectToLookAt.transform);
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, beforeRot.y, beforeRot.z);
  1. This will take a record of the position before looking at the object.
  2. Make it look at the object.
  3. Reset rotation back to its original except the X rotation

Upvotes: 1

Related Questions