# Writing a custom LookAt() function

 0 I need to use the "LookAt()" function to rotate an object towards a waypoint, but since I am always moving my object in the X axis using Vector3.right, I need a version of LookAt() which will point the object towards the target on the X axis instead of the Z axis.Can anyone tell me how the LookAt() function works internally, so that I can write my own version using a different axis?Thx for your time! Stephane more ▼ asked Apr 26 '12 at 05:52 PM ronronmx 806 ● 84 ● 101 ● 122 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 You don't need to write your own code to achieve the effect you are looking for.I'm not sure which way to interpret what you're asking, but I'll my answer for each:1) You're trying to rotate the object so that it doesn't pivot around the Y axis. ``````transform.LookAt(target, newAxis); // Change "newAxis" to the axis you want. ``````2) You want to make it so that it is not necessarily the forward axis of an object that looks at another object. You might want the side to be angled towards the target. ``````transform.LookAt(target); transform.Rotate(eulerAngleOffset, Space.Self); ``````eulerAngleOffset is the rotation in local space applied AFTER the transform is rotated to look at the target. For example, to orient left side of the transform towards the target instead of the forward side, eulerAngleOffset would be: ``````eulerAngleOffset = new Vector3(90, 0, 0); ``````Top would be: ``````eulerAngleOffset = new Vector3(0, 90, 0); ``````To roll the camera to the side: ``````eulerAngleOffset = new Vector3(0, 0, 90); ``````These functions can be broken down into operations with Quaternions. When you can wrap your head around them, they are extremely useful. I'm not sure of what the source code is for the quaternion functions, but here is an example of code that "points" an arbitrary local direction of an object towards an arbitrary global direction, taking the shortest way around possible: ``````Vector3 fromGlobalDir = transform.rotation * fromLocalDir; // Same as Transform.TransformDirection(). transform.rotation = Quaternion.FromToRotation(fromGlobalDir, toGlobalDir) * transform.rotation; `````` more ▼ answered Apr 27 '12 at 12:57 AM Zergling103 201 ● 6 ● 8 ● 11 Thanks for the help! Basically your second solution is what I was looking for. Thing is I'm working with a vehicle with a rigidbody so I need to use smooth rotations with Lerps, and I'm also trying to make the vehicle rotate depending on the speed, but keep the rotation the same at any speed, just make it rotate slower/faster, kinda like if the vehicle was locked on a spline. I have been using Quaternion.LookRotation() and Quaternion.Slerp() instead of LookAt(), which works fine, but I cannot get the rotation to finish at exactly the same vector regardless of speed. Btw, thanks a lot for your examples above, they cleared up a lot of things :) Apr 27 '12 at 06:39 AM ronronmx add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x5095
x2173
x442
x137

asked: Apr 26 '12 at 05:52 PM

Seen: 976 times

Last Updated: Apr 27 '12 at 06:39 AM