• 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 DMCH · Aug 21, 2013 at 03:25 PM · 2drotationmovementjerky

2D Top Down Shooter: AI Ally twitching

Hi, and thanks for reading,

I've encountered a problem with an AI ally (armed with a gun) twitching (rotating rapidly side to side on the Y axis) when enemies get close. I think it's either caused by the ally and enemy rigidbodies bumping off one another, or the ally trying to keep its weapon pointed at an up close enemy.

I tried changing some of the properties of the rigidbody (mass, drag, angular) to no avail. Also commented out the code which rotates the ally toward the enemy. This prevents the twitching, but the ally isn't much use if it doesn't aim at enemies.

Update 1: Increasing the radius of the ally's capsule collider from 0.3 to 0.6 seems to have solved the problem. The enemies can't get so close now. This might suggest that the targetting/rotation code for the ally doesn't handle close up calculations very well, rather than it being a problem with collisions between rigidbodies. Using a oversize collider isn't a very good solution though, so an alternate one would be nice.

I've included the code the ally uses to rotate/track targets. If you need any more info, please ask. Thanks!

 public void RotateToTarget()
     {
         // Predict time it will take a bullet to reach enemy
         float secsFlightTime = (targetPosition - centreBulletSpawn.transform.position).magnitude / bulletSpeed;
         
         // Get current velocity of target
         Vector3 targetMovePerSec = target.rigidbody.velocity; 
         targetMovePerSec.y=0; 
         
         // Predict the future position of the target
         Vector3 estHitPos = targetPosition + targetMovePerSec * secsFlightTime;    
         
         // Get the rotation needed to hit the enemy
         Vector3 aimDirection = estHitPos - centreBulletSpawn.transform.position;
         
         // Rotate turret to face that position
         Quaternion neededRotation = Quaternion.LookRotation(aimDirection);
         transform.rotation = Quaternion.Slerp(transform.rotation, neededRotation, rotationSpeed * Time.deltaTime);    
         
         //transform.rotation = neededRotation;
     }

 
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 DMCH · Aug 22, 2013 at 12:41 PM

Sorry to answer my own question, but I just figured it out. Maybe it might be useful to the next guy. I think the problem had to do with estimating the future position of a target that is really close. Solved by adding a distance check, and only predicting future position if the target was over 2 units away. Code below:

 // Estimates the target's position when the bullet will hit, and rotates toward it
     public void RotateToTarget()
     {
         // Do a distance check to see how far away the enemy is
         float distToEnemy = Vector3.Distance(position, targetPosition);
         
         // If the enemy is more than 2 units away
         if(distToEnemy > 2)
         {
             // Predict time it will take a bullet to reach enemy
             float secsFlightTime = (targetPosition - centreBulletSpawn.transform.position).magnitude / bulletSpeed;
             
             // Get current velocity of target
             Vector3 targetMovePerSec = target.rigidbody.velocity; 
             targetMovePerSec.y=0; 
             
             // Predict the future position of the target
             Vector3 estHitPos = targetPosition + targetMovePerSec * secsFlightTime;    
             
             // Get the rotation needed to hit the enemy
             Vector3 aimDirection = (estHitPos - centreBulletSpawn.transform.position).normalized;
             
             // Rotate turret to face that position
             Quaternion neededRotation = Quaternion.LookRotation(aimDirection);
             transform.rotation = Quaternion.Slerp(transform.rotation, neededRotation, rotationSpeed * Time.deltaTime);
         }
         
         // Else, the enemy is less than 2 units away
         else
         {
             // Get the rotation needed to hit the enemy
             Vector3 aimDirection = (targetPosition - position).normalized;
             
             // Rotate turret to face that position
             Quaternion neededRotation = Quaternion.LookRotation(aimDirection);
             transform.rotation = Quaternion.Slerp(transform.rotation, neededRotation, rotationSpeed * Time.deltaTime);
         }
     }
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 AlucardJay · Aug 22, 2013 at 01:54 PM 1
Share

Answering your own question (especially the way you have) is totally fine, as this is a knowledge base (all answers are meant to help future readers as well as the OP). upvoted =]

avatar image
0

Answer by reppiz01 · Aug 22, 2013 at 09:50 AM

Vector3 aimDirection = estHitPos - centreBulletSpawn.transform.position;

try:

Vector3 aimDirection = (estHitPos - centreBulletSpawn.transform.position).normalized;

instead.

Comment
Add comment · Show 2 · 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 DMCH · Aug 22, 2013 at 12:13 PM 0
Share

Thanks, but the character is still twitching. Almost certain it has to do with enemies being very close, as opposed to rigidbodies bumping into one another.

avatar image reppiz01 · Aug 22, 2013 at 01:59 PM 1
Share

i thought of that, so my idea was to normalize, to "make" the distance bigger.

but nice that you solved it on your own.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

17 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

Related Questions

Having Issues Rotating 2d Sprites to face another 2d Object 3 Answers

How do i rotate my player in moving direction in 2d? 0 Answers

How do I make a 2D object face the direction it is moving in top down space shooter 1 Answer

Rotating my character only while moving in a 2D plane 3 Answers

Joystick 2D Sprite Rotation and Movement 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