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

You are watching: Unity look rotation viewing vector is zero

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);

*

Basic arithmetic..

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

(A + B) - A = B

So if B is a zero size vector (no velocity), climate the duty LookRotation will certainly be mad at you.


*
Owen-Reynolds · Feb 11, 2013 at 10:29 pm 3

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

You want to say "if not moving, face the method I to be before," and also doing nothing perfect that.


*
Stardog · Feb 12, 2013 at 02:52 to be 1

Why doesn"t it prefer it being zero? Shouldn"t it simply make the rotation (0,0,0)?. It"s not also a appropriate warning/error, just a regular Print/Debug.Log. Anyway, right here is the final piece that code, through a tiny extra to resolve 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); rather transform.rotation = Quaternion.Euler(Vector3.zero);

*
flaviusxvii · Feb 12, 2013 at 03:22 am 4
A zero length vector enables for an infinite number of "correct" rotations, ins$$anonymous$$d of just selecting one arbitrarily, or picking some consistent default, allisonbrookephotography.com walk the only sane choice, i m sorry is to i think you"re no expecting unknown results, and also throwing an error.

See more: Cara Delevingne John Hardy, John Hardy Spring 2015 Featuring Cara Delevingne


*

I got comparable issue and it to be to carry out with i didnt declare what the object tag to be so that dodnt discover the v3.look rotation.... Room u doing similar to find your object to mouse?


For those utilizing Nav$$anonymous$$eshAgent

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


Attachments:Up come 2 attachments (including images) can be offered with a maximum of 524.3 kB each and 1.0 MB total.


Welcome to allisonbrookephotography.com allisonbrookephotography.com

The best place come ask and also answer questions about development through allisonbrookephotography.com.

To aid users navigate the website we have posted a site navigating guide.

If you space a brand-new user to allisonbrookephotography.com allisonbrookephotography.com, check out our FAQ for an ext information.