Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 AlejandroGorgal · Nov 29, 2013 at 10:35 PM · rotationvectorquaternion.lookrotation

Look rotation viewing vector is zero

Hi, Im using the Unity Stealth game tutorial movement system for my own game, for some reason when I first launch the game on the editor it gives me this error and the character starts walking forward, if I stop it and launch it again the error is gone and everything works perfectly, here's the code related to character movement:

 function MovementManagement (horizontal : float, vertical : float)
 {
     // If there is some axis input...
     if(horizontal != 0f || vertical != 0f)
     {
         // ... set the players rotation and set the speed parameter to 1.7f.
         Rotating(horizontal, vertical);
         anim.SetFloat(hash.speedFloat, 1.7f, speedDampTime, Time.deltaTime);
     }
     else
         // Otherwise set the speed parameter to 0.
         anim.SetFloat(hash.speedFloat, 0);
 }
 
 
 function Rotating (horizontal : float, vertical : float)
 {
     // Player can only be rotate while he's alive
     if (health >= 1)
     {
         // Create a new vector of the horizontal and vertical inputs.
         var targetDirection : Vector3 = new Vector3(horizontal, 0f, vertical);
         
         // Create a rotation based on this new vector assuming that up is the global y axis.
         var targetRotation : Quaternion = Quaternion.LookRotation(targetDirection, Vector3.up);
         
         // Create a rotation that is an increment closer to the target rotation from the player's rotation.
         var newRotation : Quaternion = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
         
         // Change the players rotation to this new rotation.
         rigidbody.MoveRotation(newRotation);
     }
 }

The line actually giving me the error is this one:

 // Create a rotation based on this new vector assuming that up is the global y axis.
 var targetRotation : Quaternion = Quaternion.LookRotation(targetDirection, Vector3.up);

Im kind of a noob, here, any ideas what's causing it?

Comment
Add comment · Show 13
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 JeanSimonet · Nov 29, 2013 at 10:43 PM 0
Share

Is it possible it will give you this error if the direction and the up vector are collinear?

avatar image AlejandroGorgal · Nov 29, 2013 at 10:46 PM 0
Share

I think that may be the cause, though Im not sure how to fix it.

I tried changing the Vector.up line to Vector.zero but that didnt work.

avatar image robertbu · Nov 29, 2013 at 11:24 PM 1
Share

If horizontal and vertical are 0.0, then you will get this error. You can try modifying line 19 to:

 if (health >= 1 && (horizontal != 0.0 || vertical != 0.0))
avatar image JeanSimonet · Nov 29, 2013 at 11:28 PM 1
Share

@robertbu He's already testing for both of them being 0 in $$anonymous$$ovement$$anonymous$$anagement().

Alejandro, try this ins$$anonymous$$d: change line 19 to: if (health >= 1 && horizontal != 0f)

avatar image robertbu · Nov 29, 2013 at 11:55 PM 1
Share

Here is a theory. When we compare a Vector3 to Vector3.zero, under the hood, Unity is comparing to see if the distance between the two vectors is below some threshold...it probably compares the square of the distance against $$anonymous$$athf.Epsilon squared. So it is possible to build a vector out of floats that pass the 'x != 0.0' test that would evaluate to Vector3.zero. This may be happening here. If so, you can solve the problem by putting this check on line 23 so that the rotation will not occur:

 if (targetDirection != Vector3.zero) {

If this works, you may also be able to fix this problem by using $$anonymous$$athf.Approximately() for your checks on line 4.

Show more comments

1 Reply

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

Answer by AlejandroGorgal · Nov 30, 2013 at 05:45 AM

Im going to post the answer here so that anyone with the same issue will be able to see it:

The error itself is not important, in fact, even using the Unity's Stealth game tutorial script unchanged it pops up once in a while without affecting gameplay.

I managed to track down the issue and it appears that it was related to the joystick. Im using Unity's Standard Asset (mobile) Single Joystick and for some reason the first time I launched the game it failed to initialize at the 0,0 coordinates and got screwed up. I managed to fix this odd behavior by simply adding a small deadzone, so when the game boots up it'll be at 0,0 no matter what.

Anyway, I've given some thumbs up to both JeanSimonet and robertbu for helping me out, you guys worked really hard on this and I really appreciate the assistance!

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

18 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

Related Questions

making an object below this object follow rotation on y axis 0 Answers

How can I instantiate a gameobject facing another gameobject 2D? 0 Answers

Object not rotating when the associated trigger is triggered 1 Answer

Look rotation makes sudden changes 1 Answer

Mimic rotation from a joystick 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