Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CoalCzar · Nov 07, 2013 at 09:45 PM · rotationoffsettransform.lookat

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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;
Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tomer-Barkan · Nov 07, 2013 at 10:34 PM 0
Share

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.

avatar image CoalCzar · Nov 07, 2013 at 10:43 PM 0
Share

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.

avatar image Huacanacha · Nov 07, 2013 at 11:03 PM 0
Share

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.

avatar image CoalCzar · Nov 07, 2013 at 11:07 PM 0
Share

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.

avatar image Tomer-Barkan · Nov 07, 2013 at 11:10 PM 0
Share

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;

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mimic other object's rotation on one axis (with offset) 0 Answers

Creating a homing missile on 2D, problem with transform.lookat() 1 Answer

Limit Rotation of Camera around a player using keyboard 1 Answer

Rotate object with different timing...? 1 Answer

Turning a vector into a local offset? Math help 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges