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 fallingbrickwork · Aug 06, 2014 at 11:00 AM · 2drotationpathfinding

Problem with rotation and forward motion 2D Z-axis up

Hi all,

I am currrently working on a top down 2D game where the Z-axis is up. Everything is going great with setting up the grid etc, but I am having a few issues with the AI follow code. The rotation doesn't seem to be working and due to this the forwardDir (see code below which is basically a slightly modified version of the core AIFollow code from the A* Pathfinding Project).

     public void Update () {
            
             if (path == null || pathIndex >= path.Length || pathIndex < 0 || !canMove) {
                 return;
             }
            
             //Change target to the next waypoint if the current one is close enough
             Vector3 currentWaypoint = path[pathIndex];
             currentWaypoint.z = tr.position.z;
             while ((currentWaypoint - tr.position).sqrMagnitude < pickNextWaypointDistance*pickNextWaypointDistance) {
                 pathIndex++;
                 if (pathIndex >= path.Length) {
                     //Use a lower pickNextWaypointDistance for the last point. If it isn't that close, then decrement the pathIndex to the previous value and break the loop
                     if ((currentWaypoint - tr.position).sqrMagnitude < (pickNextWaypointDistance*targetReached)*(pickNextWaypointDistance*targetReached)) {
                         ReachedEndOfPath ();
                         return;
                     } else {
                         pathIndex--;
                         //Break the loop, otherwise it will try to check for the last point in an infinite loop
                         break;
                     }
                 }
                 currentWaypoint = path[pathIndex];
                 currentWaypoint.z = tr.position.z;
             }
      
             Vector3 dir = currentWaypoint - tr.position;
            
             // Rotate towards the target
             tr.rotation = Quaternion.Slerp(tr.rotation, Quaternion.LookRotation(dir), rotationSpeed * Time.deltaTime);
             tr.eulerAngles = new Vector3(0, 0, tr.eulerAngles.z);
      
             //MinusRotatorGO.rotation = Quaternion.identity;
             Vector3 forwardDir = transform.forward;
             forwardDir = forwardDir * speed;
             forwardDir *= Mathf.Clamp01(Vector3.Dot(dir.normalized, tr.forward));
             forwardDir.z = 0;
      
             transform.Translate(forwardDir * Time.deltaTime, Space.World);
      
         }
 

I am guessing the problem is within the tr.rotation = Quaternion.Slerp line, but I can't see how to correct it. I have tried amending the Quaternion.LookRotation(dir) part to an Atan2 call which I found elsewhere on the internet but that didn't fix the issue.

Any pointers on a solution would be greatly received as I'm starting to pull out the hair now (and I don't have too much left as it is!).

Kindest Regards, Matt.

Comment
Add comment · Show 1
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 Jakeman · Aug 06, 2014 at 11:26 AM 0
Share

I have this problem as well

1 Reply

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

Answer by DeiCarnifex · Aug 06, 2014 at 11:59 AM

It's hard to guess, but try to use second parameter in Quaternion.LookRotation to specify upwards direction - http://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html Hope it will help. Best, Andrey

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

23 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

Related Questions

Smoothly Rotate object towards target? 1 Answer

turn fish3d follow mouse 0 Answers

2D head breaks when flipping character 0 Answers

What causes this object to re-rotate itself after it hits it's destination? 1 Answer

how to tell a game object to move between 2 points(A,B) along x axis , right to left, and then move left to right back to its first position ??? 3 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