- Home /
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?
Is it possible it will give you this error if the direction and the up vector are collinear?
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.
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))
@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)
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.
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!