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
7
Question by Stardog · Feb 11, 2013 at 05:26 PM · rotationquaternionlookatlookrotation

"Look rotation viewing vector is zero" error

I have some smooth look code that spams this error. It basically makes an object look in the direction a rigidbody is moving.

 void Update()
 {
     if (target)
     {
         if (bSmooth)
         {
             Quaternion rotation = Quaternion.LookRotation((transform.position + target.rigidbody.velocity) - transform.position);
             transform.rotation = Quaternion.Slerp(transform.rotation, rotation , smoothSpeed * Time.deltaTime);
         }
         else
         {
             transform.LookAt(transform.position + target.rigidbody.velocity);
         }
     }
 }

The error is this line:

 Quaternion rotation = Quaternion.LookRotation((transform.position + target.rigidbody.velocity) - transform.position);
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

2 Replies

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

Answer by flaviusxvii · Feb 11, 2013 at 05:42 PM

Basic arithmetic..

(transform.position + target.rigidbody.velocity) - transform.position

(A + B) - A = B

So if B is a zero length vector (no velocity), then the function LookRotation will be mad at you.

Comment
Add comment · Show 3 · 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 Owen-Reynolds · Feb 11, 2013 at 10:29 PM 3
Share

The common fix is to guard with "if velocity is not zero".

You want to say "if not moving, face the way I was before," and doing nothing accomplishes that.

avatar image Stardog · Feb 12, 2013 at 02:52 AM 1
Share

Why doesn't it like it being zero? Shouldn't it just make the rotation (0,0,0)?. It's not even a proper warning/error, just a normal Print/Debug.Log. Anyway, here is the final piece of code, with a little extra to fix something specific to my game:

 if (target.rigidbody.velocity != Vector3.zero)
 {
     Quaternion rotation = Quaternion.LookRotation((transform.position + target.rigidbody.velocity) - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, smoothSpeed * Time.deltaTime);
 }
 else
 {
     transform.rotation = Quaternion.Euler(Vector3.zero);
 }
avatar image flaviusxvii · Feb 12, 2013 at 03:22 AM 4
Share

A zero length vector allows for an infinite number of "correct" rotations, ins$$anonymous$$d of just choosing one arbitrarily, or choosing some consistent default, Unity does the only sane choice, which is to assume you're not expecting undefined results, and throwing an error.

avatar image
0

Answer by supamigit · Aug 12, 2015 at 04:18 PM

I got similar issue and it was to do with i didnt declare what the OBJECT tag was so it dodnt find the v3.look rotation.... Are u doing similar to find your object to mouse?

Comment
Add comment · Show 2 · 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 jchart7 · Oct 20, 2015 at 08:53 AM 0
Share

For those using Nav$$anonymous$$eshAgent

if (navAgent.desiredVelocity != Vector3.zero) { transform.rotation = Quaternion.LookRotation(navAgent.desiredVelocity); }

avatar image Kerihobo jchart7 · Feb 08, 2016 at 06:15 AM 0
Share

O$$anonymous$$G thank you, you just ended a 4hr headache for me D:

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

14 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

Related Questions

Camera following/looking at aircraft 1 Answer

Transform Z axis rotation from center of screen toward mouse cursor with Quaternion.LookRotation for a top down game. 2 Answers

How can I get the rotation between two objects 1 Answer

to rotate along z axis 1 Answer

Lookat offset angle combined with RotateAround. Basically orbit + rot angle 3 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