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 Rodlaiz · Apr 13, 2016 at 06:27 PM · 2drotationdirection

Why is my GameObject rotation snapping to a certain angle when he reaches its destination?

I'm trying to make my NCP to rotate and look at the direction he's heading. That's not a problem. Problems is when he reaches its destination the GO rotates snapping to 90, 180, 270, etc degrees. Can't figure why he's doing that... I want the NCP to keep looking at the direction he was looking just before he stops moving, any ideas?

 void Update () {
 
         dir = transform.position - prevLoc;
 
         float heading = Mathf.Atan2(dir.x, -dir.y);
         transform.rotation = Quaternion.Euler(0f, 0f, heading * Mathf.Rad2Deg + 90);
 
         prevLoc = transform.position;
     }

A more visual example: Animated GIF

Comment
Add comment · Show 2
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 Eno-Khaon · Apr 13, 2016 at 06:40 PM 1
Share

Could you add a detail regarding how your character moves from one location to the next?

If I were to guess, your character's reaching the destination and the "next direction to move" is Vector3.zero, so Atan2() is getting stuck dealing with zeroes and the resulting rotation is being thrown arbitrary values.

However, a guess is a guess and is fairly irresponsible.

So, why did I guess? If I'm wrong about how it works, then it's a demonstration of why more information is necessary to deter$$anonymous$$e what the problem is.

With that in $$anonymous$$d, if you can provide some of your code regarding movement, then it would be easier to tell whether the character's facing based on that movement is doing something expected or unusual.

avatar image Rodlaiz · Apr 13, 2016 at 07:49 PM 0
Share

Hello Eno, Thank you for your answer. Since I'm a noob with code I'm using Behavior Designer and PolyNav2D packages to create my AI

Behavior Designer

Poly Nav 2D

The NPC is moving with a combination of those packages, I assume is actually PolyNav who is doing the movement in the "Seek" State.

Taking a look at the package code I presume the actions happens here:

 //move the agent
         position += velocity * Time.deltaTime;

In any case the movement is occurring without a Rigidbody I'm sorry that I can't give more information about how the movement is happening

1 Reply

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

Answer by Kazuva · Apr 14, 2016 at 06:34 AM

it's because when it stops

dir = transform.position - prevLoc = Vector3(0,0,0);

so you need to check if it's stopped

\/ like this \/

 void Update () {
         if (transform.position != prevLoc) {
 
             dir = transform.position - prevLoc;
         
             float heading = Mathf.Atan2 (dir.x, dir.y * -1);
             transform.rotation = Quaternion.Euler (0f, 0f, heading * Mathf.Rad2Deg + 90);
         
             prevLoc = transform.position;
         }
     
     }
Comment
Add comment · Show 1 · 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 Rodlaiz · Apr 14, 2016 at 07:05 AM 0
Share

That did the trick! Thank you!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Point an arrow at the mouse position, 2.5D 1 Answer

Rotate an arrow object based on addedForce 1 Answer

rotation LookAt of 2D gameObject in 3D space 1 Answer

how to build a 2d character which can faced 2 directions(left and right), with high performance 1 Answer

Rotation to go from one direction to another? 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