Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 behemothdan · Aug 09, 2017 at 05:25 PM · movementnavmeshnavmeshagent

NavMesh Agent rotates on X axis when moving and snaps back to 0.

I've been successful in creating a NavMesh at the run time and I am able to get my player unit to move around on the NavMesh. All is well there.

The weird part is that the player unity rotates every so slightly on the X axis but upon reaching the intended destination snaps back to 0 rotation again. My movement is a pretty basic click-to-move system that doesn't involve any rotation other than using the transform.LookAt before moving.

I haven't been able to figure out what is causing this weird rotation or how to stop it from happening yet. Any ideas on what might be causing it would be greatly appreciated.

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
1

Answer by unidad2pete · Aug 09, 2017 at 06:14 PM

Please, share your code to find the problem, and tell us if you are using animator.

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 behemothdan · Aug 09, 2017 at 06:49 PM

I am not using animator. My player movement code looks like this:

 public class PlayerMovement : MonoBehaviour {
     public float speed = 3;
     private Vector3 targetPosition;    
     private bool isMoving;
     const int LEFT_MOUSE_BUTTON = 0;
     private float yAxis; 
     private float distance;
     
 
     void Start() {
         targetPosition = transform.position;
         isMoving = false;
         yAxis = gameObject.transform.position.y;
     }
         
     void Update () {
         if(Input.GetMouseButtonDown(LEFT_MOUSE_BUTTON)) {            
             SetTargetPosition();
         }
         if(isMoving) {            
             MovePlayer();
         }        
     }
 
     void MovePlayer() {        
         gameObject.transform.LookAt(targetPosition);        
         distance = Vector3.Distance(targetPosition, gameObject.transform.position);
         if(distance >= .5){
             gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, targetPosition, speed * Time.deltaTime);
         }                       
         if(distance < .5) {
             isMoving = false;
         }
 
         Debug.DrawLine(transform.position, targetPosition, Color.red);
     }
 
     void SetTargetPosition() {
         RaycastHit hit;        
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);        
 
         if(Physics.Raycast(ray, out hit, 100)) {
             targetPosition = hit.point;
             targetPosition.y = yAxis;
             isMoving = true;                    
         }        
     }    
 }

The player object does move properly to the clicked location within the navmesh too. Just the weird X rotation happening is the weird part.

Comment
Add comment · Show 6 · 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 unidad2pete · Aug 09, 2017 at 07:36 PM 1
Share

The code its fine. But, you dont need use a nav mesh component , if your player object has a Nav$$anonymous$$esh Agent, remove it, you dont need it, the problem its solved on my test proyect if I dont use Nav$$anonymous$$eshAgent

avatar image behemothdan unidad2pete · Aug 09, 2017 at 07:40 PM 0
Share

Ultimately the reason I was using Nav$$anonymous$$eshComponent and Agent was that my map is a non-square that is dynamically generated. Using the Nav$$anonymous$$esh pieces was the best way to keep the player object from moving through walls.

avatar image unidad2pete behemothdan · Aug 09, 2017 at 07:57 PM 1
Share

Then,if you are using a Nav$$anonymous$$eshAgent, move your player with them, you dont need use lookAt() , Nav$$anonymous$$eshAgent Looks to de destination automaticaly.

 using UnityEngine.AI;
 
 public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour
 {
     public float speed = 3;
     private Vector3 targetPosition;
     private bool is$$anonymous$$oving;
     const int LEFT_$$anonymous$$OUSE_BUTTON = 0;
     private float yAxis;
 
     private float distance;
 
     public Nav$$anonymous$$eshAgent agent; 
 
 
     void Start()
     {
         targetPosition = transform.position;
         is$$anonymous$$oving = false;
         yAxis = gameObject.transform.position.y;
 
 
         agent = GetComponent<Nav$$anonymous$$eshAgent>();
     }
 
     void Update()
     {
         if (Input.Get$$anonymous$$ouseButtonDown(LEFT_$$anonymous$$OUSE_BUTTON))
         {
             SetTargetPosition();
         }
         if (is$$anonymous$$oving)
         {
             $$anonymous$$ovePlayer();
         }
     }
 
     void $$anonymous$$ovePlayer()
     {
         //gameObject.transform.LookAt(targetPosition);
         distance = Vector3.Distance(targetPosition, gameObject.transform.position);
         if (distance >= .5)
         {
             //gameObject.transform.position = Vector3.$$anonymous$$oveTowards(gameObject.transform.position, targetPosition, speed * Time.deltaTime);
 
             agent.SetDestination(targetPosition);
         }
         if (distance < .5)
         {
             is$$anonymous$$oving = false;
         }
 
         Debug.DrawLine(transform.position, targetPosition, Color.red);
     }
 
     void SetTargetPosition()
     {
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (Physics.Raycast(ray, out hit, 100))
         {
             targetPosition = hit.point;
             targetPosition.y = yAxis;
             is$$anonymous$$oving = true;
         }
     }
 }
Show more comments

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

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

Related Questions

Can't get player to walk to object using NavMeshAgent 1 Answer

Navmesh Agent Path Not Updating 1 Answer

Change agent type stops movement. 0 Answers

Navmesh Agent is moving alone if changed position 1 Answer

Gaps in the Nav mesh 0 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