Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by imp903 · Oct 12, 2015 at 01:00 AM · rotationanimatorvector3worldspacelocalspace

converting local space vector into world space?

So, I have a movement script for my character based entirely in the animator controller. The character has a reference vector in front of him to monitor which area is straight ahead of him, then the character's mouse X rotates a second vector for look direction around the character. The angle difference between the reference vector and the look direction vector is the offset angle. Then depending on this offset angle, the blend tree blends between different turn speed animations to turn the character.

For example, if I move my mouse to the right suddenly, the offset angle would increase, the character would rotate towards that, thus closing the distance between the reference vector and the look direction vector, and the character would stop rotating when that offset angle dropped below threshold. But, currently, I have it so the angle between the vectors increases, but when the character rotates, both the reference vector and the look direction vector rotate, so the offset angle never changes and the character continues to rotate until I manually move my mouse to close the gap, because the look direction vector is in local space rather than world space. I'm not quite sure how to convert the vector into world space though to achieve this effect. Here's what I have:

     private Animator playerAnim;
     public Vector3 refDir;
     public Vector3 lookDir;
     public int lookSens;
     public float yRotation;
     public float offsetAngle;
 
     void Start()
     {
         playerAnim = GetComponent<Animator>();
         lookDir = new Vector3(0.0f, 0.0f, 1.0f);
     }
 
     // Update is called once per frame
     void Update()
     {
         float mouseX = Input.GetAxis("Mouse X") * Time.deltaTime * lookSens;
         refDir = new Vector3(0, 0, 1);
         lookDir = Quaternion.Euler(0.0f, mouseX, 0.0f) * lookDir;
         offsetAngle = Vector3.Angle(refDir, lookDir);
         playerAnim.SetFloat("HorAimAngle", offsetAngle);
     }

Also, as a smaller note I might as well throw out there. My offset angle is never negative right now because the vector3.Angle seems to only take the absolute value of that offset, so my character only wants to turn right for now. Thanks in advance

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
0
Best Answer

Answer by dkjunior · Oct 12, 2015 at 07:17 PM

Your lookDir is actually in world space. It never changes with character rotation, it only changes in response to mouse movement.

I think the problem is that your refDir vector is constant (always equal to (0,0,1)) while you want it to represent the current character rotation. What does your animation do? Does it rotate the object around Y axis? If so, setting refDir to transform.forward instead of (0,0,1) may help:

 refDir = transform.forward;
Comment
Add comment · 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

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

33 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 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

Unity2d worldspace / local space vector 3 help needed 0 Answers

Global direction (e.g. Vector3.forward, Vector3.right) from Mouse drag 0 Answers

Why does my GameObject move to the side when applying force on the Y axis (in world space) and then rotating it (also in world space)? 0 Answers

Using quaternions to align game objects. 1 Answer

how to convert a Vector3 to a float value? 1 Answer


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