- Home /
Constrain LookAt to Local Axis
I've been using the following code to constrain LookAt to a specific axis:
Vector3 lookAtPos = target;
lookAtPos.y = obj.transform.position.y; // constrain y axis
obj.LookAt(lookAtPos);
This constrains to the global y axis. What I need to do is constrain to the local y axis that can be viewed with the local rotate handle. How is this possible?
Answer by ScroodgeM · Jul 22, 2012 at 07:16 AM
Vector3 lookAtPos = target; //first get a position relative to object Vector3 lookAtPosLocalised = obj.InverseTransformPoint(target); //offset it lookAtPosLocalised.y = 0f; //replace zero with any offset needed //convert localised position back lookAtPos = obj.TransformPoint(lookAtPosLocalised); obj.LookAt(lookAtPos);
sometimes the localized z point is not in-line with the local z-axis, it bounces all over the place. i will do some more investigating.
to clarify, i need this to work for all axis.
to be in-line with the local z-axis you should reset x and y components of vector.
i lost your first issue that you want to achieve 8)
which vector should have the x and y reset, lookAtPosLocalised?
Answer by samcsss · Oct 28, 2013 at 01:20 AM
Wow, thanks. I've been searching for that answer for hours. ...I need to study math more :(
Your answer
Follow this Question
Related Questions
use world axis when using transform.rotate 3 Answers
Rotating Object on its Local World Axis 4 Answers
LookAt something while rotating on z? 1 Answer
Look At while locked in a position 1 Answer
LookAt() with X axis 1 Answer