Delayed LookAt, but only on one axis
Hey, after too many hours of trying multiple solutions, using Slerp, Lerp, LookAt, LookRotation and reading almost every answer on the forums I'm just really confused right now.
Basically what I want to achieve is an object that changes its rotation to look at another object over time. (with "over time" i mean that it should be rotation towards the endrotation and not just look at it every frame) But this should only be happening on one axis (y-axis in this case).
I would very much appreciate your help.
Thank you in advance :)
Patrick
Answer by Statement · Oct 25, 2015 at 08:34 PM
Get the direction to the target, but ignore Y. From that direction, get a rotation quaternion. Rotate towards that rotation.
using UnityEngine;
public class LookAtXZ : MonoBehaviour
{
public Transform lookAt;
public float degreesPerSecond = 90;
void LateUpdate()
{
if (!lookAt)
return;
Quaternion targetRotation = Quaternion.LookRotation(DirectionXZ());
transform.rotation = Quaternion.RotateTowards(transform.rotation,
targetRotation, degreesPerSecond * Time.deltaTime);
}
Vector3 DirectionXZ()
{
Vector3 direction = lookAt.position - transform.position;
direction.y = 0; // Ignore Y
return direction;
}
}
Your answer
Follow this Question
Related Questions
Problem with Quaternion and Euler Angles 0 Answers
How to use Quaternion.slerp and Quaternion.LookRotation with a child gameobject 0 Answers
Mobile Gyroscope, make Camera always rotating towards zero point using Quaternion 2 Answers
Smooth FPS rotation with right/left arrow key 1 Answer
Nonlinear movement from A to B 0 Answers