• 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 /
  • Help Room /
avatar image
Question by StephanT · Jan 27, 2016 at 04:11 AM · anglemovetowardstorquerotatetowards

Rotate towards target in 2D space using AddTorque

Hi everybody,

I'm working on a project where pacman-like creatures chase and eat and also flee from each other. The rotation maths give me headaches, though. I'm stuck for days, out of google keywords, found no answer which suits my problem. Though, in theory, it's pretty simple I guess.

The situation:

2D-space, using the XY-plane. The forward direction of my pacmans is their transform.right. (it would be nice to use transform.forward instead of .right, but that would make them move away from the camera, in z-direction, leaving their x-y-plane.)

They have sensors looking for smaller pacmans to eat. Once a sensor locks on a target we can access the target's transform, especially it's transform.position.

So I know the position of A and the look-direction of A. I know the position of B (its rotation does not matter). I want A to move towards B. But only if the angle from faceDirection to targetDirection is small. If it's too big, A shall not move but rotate so that this angle decreases.

I'd like to use the Rigidbody2D of A with .AddTorque(force) to accomplish that. In the hope of a smooth rotation without overshooting I intend to use big forces for big angles and small forces for small angles.


So bottom line I need: - the angle to determine if A can actually move towards B or if it has to rotate first - the proper force (positive or negative) to turn A in the right direction.

I can move and rotate my colorful fellows, I simply can't do it right. They turn in pointless directions and run into nothingness, so I guess my math was wrong, or I fail to understand Quaternions. Can someone do it right?


In the picture below you can see a green and pink pacman who have locked on the target in the middle, indicated by the arrow gizmos. The facing direction is where the mouth is, so the green one is supposed to turn downwards while the pink one should turn up a little before moving in.

alt text

hungry.png (18.3 kB)
Comment
jarbleswestlington_unity

People who like this

1 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by StephanT · Jan 30, 2016 at 01:43 AM

I think I solved it. I created a test scene with some debug output:

alt text

As you can see, the angle is properly calculated, including the sign.

After a few seconds, it looks like this (pacmans on the left side are still turning):

alt text

And here is the code:

     void Update () {
         // rotate if angle is bigger than that to decrease it
         float maxAngle = 10f;
 
         // where is the target?
         Vector2 targetDirection = Small.transform.position - Big.transform.position;
         // where are we looking?
         Vector2 lookDirection = Big.transform.right;
 
         // to indicate the sign of the (otherwise positive 0 .. 180 deg) angle
         Vector3 cross = Vector3.Cross(targetDirection, lookDirection);
         // actually get the sign (either 1 or -1)
         float sign = Mathf.Sign(cross.z);
 
         // the angle, ranging from 0 to 180 degrees
         float angle = Vector2.Angle(targetDirection, lookDirection);
 
         // apply the sign to get angles ranging from -180 to 0 to +180 degrees
         angle *= sign;
         // debug output
         Text.text = angle.ToString();
 
         // apply torque in the opposite direction to decrease angle
         if (Mathf.Abs(angle) > maxAngle) Big.GetComponent<Rigidbody2D>().AddTorque(-sign);   
     }

It's properly working even if the blue targets are randomly moving around.


start.png (46.4 kB)
later.png (52.6 kB)
Comment
jarbleswestlington_unity
GibsSCake
CakeMuffinCo

People who like this

3 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 jarbleswestlington_unity · Aug 25, 2020 at 12:57 AM 0
Share

WELL DONE! I don't know why it was so hard to find somebody who's attempted this, but this is a great help.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

39 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

Related Questions

How to make a character controller slide down when the angle of a slope is over 45 degrees 1 Answer

Why angle between these two velocities is 45? 2 Answers

Feild of view only wokring in specific areas of the scene 0 Answers

Is it possible to restrict the viewing angle for a while in GOOGLEVR? 0 Answers

Constant Car Speed using wheel colliders. 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