- Home /
Transform.LookAt() when transform.forward is offset
I have a mesh with an elbow that I want to orient towards the player. However, the elbow's z-axis is not pointing towards its tip it points out instead, so when I use transform.LookAt(character), it points the z-axis at the character (like it's supposed to) and ends up in at a weird angle. I want it to align with this transform's z-axis when it rotate towards the player so that the tip of the "nozzle" is pointed at the player.
I've tried various solutions, but I am missing a basic understanding of what I need to do to correct the offset.
Thanks.
Answer by Tomer-Barkan · Nov 07, 2013 at 10:19 PM
The easiest way I can think of, is to make an empty GameObject, that will be the parent of your mesh.
Then, rotate the child (your mesh), so that the part you refer to as "forward", is in-line with the parent's transform transform.forward
(positive z axis).
Once you've done that, you can simply use transform.LookAt(character)
, but using the parent object's transform.
If it's not possible to change the hierarchy, here's an alternative solution:
First, create an empty parent object, and add your mesh as a child. Then rotate the child so that it faces the transform.forward of the parent, in the scene. Now, attach the following script to the child object (the mesh):
public class RotationCalculation : MonoBehaviour {
public static Quaternion relativeRotation;
void Start() {
// relativeRotation should hold a quaternion that can be used to rotate the mesh, so that it points toward it's transform.forward.
relativeRotation = Quaternion.Inverse(transform.localRotation);
// after setting the static variable, destroy the object and it's parent
GameObject.Destroy(transform.parent.gameObject);
}
}
Now you have a static variable that you can use to rotate your mesh so that it faces it's transform.forward. So when you want it to point at something, use the following commands:
transform.LookAt(character);
transform.rotation *= RotationCalculation.relativeRotation;
An even easier way, though, would be to fix the mesh in your 3D software, so that it is pointing in the positive Z direction, then re-import it to unity correctly.
Thanks for your answer. Here are two problems:
1) I'm not the creator of the mesh or a 3d artist. I can see if my artist can do that.
2) I don't know if I can parent it to another object without messing up the animations that are already attached to it.
You don't need your artist to do anything. The child GameObject contains your mesh, and the rotation can be set in the inspector or via script.
Huacanacha - I know I can set the rotation, but I can't change the origin rotation of the submesh without the 3d software.
Because the elbow's z-axis is not in line with the gun's z-axis, I can't transform.LookAt an object and have the tip of the gun point at the object.
I see. Well, try to get your artist to fix it, because that would really be easiest. But if not, you can try the following formula (untested, but the idea should be sound):
First, create an empty parent object, and add your mesh as a child. Then rotate the child so that it faces the transform.forward of the parent, in the scene. Now, attach the following script to the child object (the mesh):
public class RotationCalculation : $$anonymous$$onoBehaviour {
public static Quaternion relativeRotation;
void Start() {
// relativeRotation should hold a quaternion that can be used to rotate the mesh, so that it points toward it's transform.forward.
relativeRotation = Quaternion.Inverse(transform.localRotation);
// after setting the static variable, destroy the object and it's parent
GameObject.Destroy(transform.parent.gameObject);
}
}
Now you have a static variable that you can use to rotate your mesh so that it faces it's transform.forward. So when you want it to point at something, use the following commands:
transform.LookAt(character);
transform.rotation *= RotationCalculation.relativeRotation;
Your answer
Follow this Question
Related Questions
RayCast offset rotation 20 degrees 1 Answer
Offset textures individually into specific world directions 2 Answers
smooth look at with offset 0 Answers
Firing a projectile from orbit 1 Answer