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 Americus · Dec 07, 2012 at 06:24 AM · errorenemylookatsmoothwaypoint

Smooth Look causing Vector is Zero error...

Hey fellow Unity scripters!

I'm fairly new to the game, but hopefully this isn't an eye-rolling question. My script is actually working fairly well, but since it's all new ground for me, I'm not able to shake this annoying console error. And I just like it when there are no errors. ;)

This script is a basic Enemy AI using a Waypoint array. If the player gets close enough, then the enemy behaves differently (charges and stomps). It has a few animations thrown in there but the problem is coming out of the smooth look function. I spent some time trying to figure out a better method of looking at things outside of:

 transform.LookAt();

which gives very sharp turns toward waypoint/player. Eventually I found a nice little snippet that smooths this rotation out, which is:

 Quaternion rotation = Quaternion.LookRotation(player.position - _myTransform.position);
 _myTransform.rotation = Quaternion.Slerp(_myTransform.rotation, rotation, Time.deltaTime * damping);

This works fine, but when the game begins, it passes out a console error stating: "Look Rotation Viewing Vector is Zero", and a little more detail states: "UnityEngine.Quarternion:LookRotation(Vector3)"

I thought maybe it was trying to get a position before one was loaded in, so I put a little if target !=null at the start, but it didn't help. I've spent a good hour looking into the error and the discussions are a little too advanced for my current C# understanding. So here I am! ^_^

The entire script:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyMovement : MonoBehaviour {
     
     //Waypoint control. 
     public Transform[] waypoint;
     int currentWaypoint;
     
     //Speed ints. 
     public int chargeSpeed;
     public int patrolSpeed;
     
     //Loops waypoints. 
     bool loop=true;
     
     //Plug ins.
     public Transform player;
     public GameObject demonModel;
     
     //Smooth Looking Control.
     public float damping = 6.0f;
     private Transform _myTransform;
     public bool smooth = true;
     
     //Awake. 
     void Awake()
     {
         waypoint[0].position = transform.position;
     }
     
     // Update is called once per frame
     void Update () 
     {
         //Set the transform. 
         _myTransform = transform;
         
         //Temp term to streamline code. 
         Vector3 velocity = rigidbody.velocity;
         
         //If the waypoint is less than the entire length of the waypoint array. 
         if (currentWaypoint < waypoint.Length)
                 {
                     //Temp. variables to streamline code. 
                     Vector3 target = waypoint[currentWaypoint].position;
                     Vector3 moveDirection = target-transform.position;    
                     Vector3 distFromPlayer = player.position - transform.position;
                     
                     //Not working. Meant to set the y to 0 so he wont rotate towards waypoint/player. 
                     moveDirection.y = 0;
                     
                         //Look at target. 
                         if(smooth && distFromPlayer.magnitude>10)
                             {
                                 Quaternion rotation = Quaternion.LookRotation(target - _myTransform.position);
                                 _myTransform.rotation = Quaternion.Slerp(_myTransform.rotation, rotation, Time.deltaTime * damping);
                             }
                         else if (smooth && distFromPlayer.magnitude<10 && !AnxietyController.Hiding)
                             {
                                 Quaternion rotation = Quaternion.LookRotation(player.position - _myTransform.position);
                                 _myTransform.rotation = Quaternion.Slerp(_myTransform.rotation, rotation, Time.deltaTime * damping);                        
                             }
                             
                         //If the distance between current position and end position is less than 1. 
                         if (moveDirection.magnitude<1)
                             {
                                 //LK - Crossfade to idle, will be increased when enemy stops. 
                                 demonModel.animation.CrossFade("Idle", 0.4F);
                 
                                 //LK - Switch to next waypoint. 
                                 currentWaypoint++;    
                             }
             
                         //LK - if distance from player is less than 10 and they're not hiding. 
                         if (distFromPlayer.magnitude<10 && !AnxietyController.Hiding)
                             {            
                                 //Set velocity to 0. 
                                 velocity = Vector3.zero;
                                 moveDirection.y = 0;
                 
                                 //LK - Look at player. 
                                 transform.LookAt(player.position);
                                 
                                 //LK - Set velocity to patrol speed. 
                                 velocity = (player.position - transform.position).normalized * chargeSpeed;
                                 
                                 //LK - Is distance from player is less than 3, then stop moving and crossfade roar animation. 
                                 if (distFromPlayer.magnitude < 5)
                                     {            
                                         velocity=Vector3.zero;
                                         demonModel.animation.CrossFade("Roar", 0.4F);                
                                     }
                 
                                 //if (distFromPlayer.magnitude > 5 && distFromPlayer.magnitude <6)
                                 //    {            
                                 //        velocity=Vector3.zero;
                                 //        demonModel.animation.CrossFade("hpaunch", 0.4F);                
                                 //    }
                 
                                 if (distFromPlayer.magnitude > 6 && distFromPlayer.magnitude <10)
                                     {            
                                         velocity = (player.position - transform.position).normalized * chargeSpeed;
                                         demonModel.animation.CrossFade("Walk", 0.4F);            
                                     }
                             }
                         //Else, play walk and move around as normal. 
                         else
                             {
                                 demonModel.animation.CrossFade("Walk", 0.4F);
                                 velocity = moveDirection.normalized * patrolSpeed;
                             }
                 }
             //If waypoint is the same or higher than length of waypoint. 
             else 
                 {
                     //And loop is slected. 
                     if(loop)
                         {
                             //Reset to first waypoint. 
                             currentWaypoint=0;
                         }
                     //If loop is off. 
                     else
                         {
                             //Just stop. 
                             velocity=Vector3.zero;
                         }
                 }
         
         //Set back the velocity to reset the frame. 
         rigidbody.velocity= velocity;
         
     }    
     
     
 }
 
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
2
Best Answer

Answer by Americus · Dec 07, 2012 at 09:45 AM

For anybody having the same problem (noticed a few across the web) I found a fix. It is not a fix to the actual problem, which I still don't fully understand yet, but it gets rid of the weird Viewing Vector error code in the console.

 if(target != _myTransform.position){
             Quaternion rotation = Quaternion.LookRotation(target - _myTransform.position);
             _myTransform.rotation = Quaternion.Slerp(_myTransform.rotation, rotation, Time.deltaTime * damping);
                                 }    


The idea is to check to make sure both positions are not the same (that is the first position in the Quaternion rotation, and the second one being subtracted). This keeps the code fully functional (as it was only occurring momentarily anyhow) and removes the error. Hope that helps anybody else that has trouble with this.

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
avatar image
0

Answer by kevinv321 · Jul 12, 2013 at 03:04 PM

Hi mate,

I just had the same problem, I am really new to Unity game development (started 2 days ago), but I might know the reason why this happens.

I did debug my Vector 3 values and noticed that it only occurred when y z or x is really small, like 0.00000001. My guess is that somewhere in the process of calculating the rotation, Unity rounds this number to 0.0000 (example) and that means the value is zero. With your solution (in the answers you provided) you simply check if Unity rounded this value. Another way of doing this is:

 h = Input.GetAxis("Horizontal");
 v = Input.GetAxis("Vertical");
 
 if ( (h > 0.001f || h < -0.001f ) || (v > 0.001f || v < -0.001f) ) {
     Rotating(h, v);
     Move(h, v);         
 }

But probably both our ways will result in same effect: preventing from a rounded to 0 value to be used in the rotation script.

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

10 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

Related Questions

Enemy Not Facing target direction (waypoint) correctly when moving between waypoints 1 Answer

Enemy detection while pathfinding through the map 1 Answer

Find enemy problem. 1 Answer

enemy detect player then attack - c# 1 Answer

How to make the enemy move back to its waypoint after it's target killed? 2 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