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 pulkit8.mahajan · Jan 13, 2014 at 04:40 AM · move an objectorbitenemyai

How to refine the enemy moving towards me?

I know the answer is really simple, but i just can't seem to put a finger on it. Here is my code:

 void Update () {
         
         float Speed = Random.Range(1.0F,10.0F);
         Vector3 direction_to_player = (Player.transform.position - transform.position);
         rigidbody.AddForce(direction_to_player);
         
         }


So this causes the enemy to move towards me, but the problem is the enemy doesn't head straight at me. Even if the enemy is right in front of me, it moves in a circular orbit around the player, slowly getting closer and closer. And if u try to increase the speed, the orbit just becomes more elliptical. How can i make the enemy come straight at me, instead of circling me?

Comment
Add comment · Show 5
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 getyour411 · Jan 13, 2014 at 04:54 AM 0
Share

This sounds like the enemy is hitting a collider so it can't get to the location it wants; watch it in scene view and see if there's something like that? In your code above, you have "Player" in uppercase, is that right?

avatar image pulkit8.mahajan · Jan 13, 2014 at 05:05 AM 0
Share

The thing is, it moves towards the player, but if it misses once, it can't compensate and it will overshoot, and then come back and overshoot again. How do i make it so that it doesn't overshoot?

avatar image getyour411 · Jan 13, 2014 at 05:09 AM 0
Share

Add some logic to stop adding force when the enemy nears the player; also check out $$anonymous$$oveTowards

avatar image SunnyChow · Jan 13, 2014 at 05:29 AM 0
Share

Do you want to do it in realistic physics? Or you just want to make a simple game?

avatar image pulkit8.mahajan · Jan 13, 2014 at 05:45 AM 0
Share

I want to make it simple, and have some way for it to stop, and then head straight at the player, ins$$anonymous$$d of the enemy remaining in motion and slowly turning around. The enemy should be like a bull, where it stops, and then turns around for another charge.

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by SunnyChow · Jan 13, 2014 at 06:02 AM

rigidbody.rotation = Quaternion.LookRotation(direction_to_player);

rigidbody.velocity = direction_to_player;

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 pulkit8.mahajan · Jan 15, 2014 at 11:18 PM 0
Share

this works, but when an explosion occurs, the enemies aren't pushed back like they originally were. How can i make it so that they will get pushed back from the explosion force?

avatar image morbidcamel · Jan 16, 2014 at 06:36 AM 0
Share

The code I posted I found in the AngryBots enemy AI movement motor. Download and have a look at the code.

avatar image
0

Answer by morbidcamel · Jan 13, 2014 at 05:25 AM

It sounds like you not setting the angular velocity... Here's a typical rigidbody movement motor.. Assuming you have 2 vectors, point a the position of the game object, b the position of the target to move to...

First calculate the force - ...

 Vector3 nextDirection = (b - a).normalized;                       
 Vector3 targetVelocity = nextDirection * speed;
 Vector3 deltaVelocity = targetVelocity - thisRigidbody.velocity;
                                  if (thisRigidbody.useGravity)
                                         deltaVelocity.y = 0;
                                 
                                 force = deltaVelocity * accelerationRate;

Then we can calculate the angular velocity

                 // Make the character rotate towards the target rotation
                 var turnDir = nextDirection;
                 if (turnDir == Vector3.zero) 
                 {
                         angularVelocity = Vector3.zero;
                 }
                 else 
                 {
                         var rotationAngle = AngleAroundAxis (transform.forward, turnDir, Vector3.up);
                         angularVelocity = (Vector3.up * rotationAngle * turningSmoothing);
                 }

Then you can set the rigidbody --

 thisRigidbody.AddForce (force, ForceMode.Acceleration);
 thisRigidbody.angularVelocity = angularVelocity;

Here is how to calculate the angle around an axis (make it turn around its head y axis)

 public static float AngleAroundAxis (Vector3 dirA, Vector3 dirB, Vector3 axis) 
                 {
                     // Project A and B onto the plane orthogonal target axis
                     dirA = dirA - Vector3.Project (dirA, axis);
                     dirB = dirB - Vector3.Project (dirB, axis);
                    
                     // Find (positive) angle between A and B
                     float angle = Vector3.Angle (dirA, dirB);
                    
                         // Return angle multiplied with 1 or -1
                     return angle * (Vector3.Dot (axis, Vector3.Cross (dirA, dirB)) < 0 ? -1 : 1);
                 }
 
     
 To make sure it doesn't overshoot make use of a in range calculation and the adjust the speed for the component above. You can do this with ( b - a).magnitude or Vector3.Distance. The trick is to calculate the gradual deceleration or you can just make it stop with a speed = 0. Let me know if you need more help.
Comment
Add comment · Show 5 · 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 pulkit8.mahajan · Jan 13, 2014 at 05:40 AM 0
Share

It is giving me a bunch of errors, probably because it is in js. $$anonymous$$y code is in c#.

avatar image morbidcamel · Jan 13, 2014 at 05:51 AM 0
Share

I just gave you snippets of code, you need to put this into a component,, I'm illustrating what you should do not giving you a complete solution. You need to declare "speed" as public fields for example...

avatar image morbidcamel · Jan 13, 2014 at 05:52 AM 0
Share

That is C#........... Notice the lack of the word function

avatar image pulkit8.mahajan · Jan 15, 2014 at 02:21 AM 0
Share

it also has the word var spread thorughout

avatar image morbidcamel · Jan 16, 2014 at 05:49 AM 0
Share

Cool kids use var, it means you don't have to spell out your intent twice ;)

avatar image
0

Answer by sath · Jan 13, 2014 at 06:30 AM

you may want to try


edit .. avoid from falling through floor added ...

     GameObject Player;
     public float speed;
     public float rotationSpeed=3f;
     public float howCloseToPlayer=3f;//Adjust here to stop enemy in a safe distance from Player to avoid moves in a circular orbit around the player
     
     //set the position Y to be stable
     public float minY = 0f, maxY = 0.01f;// keep position Y (avoid fall through)
     public Vector3 currentPosition ;
     
     void Start () {
         Player=GameObject.Find("Player");
         speed = Random.Range(3.0f,10.0f);
         rotationSpeed=3f;
     }
     
     void Update () {
     
         // get the position to a variable
         currentPosition = transform.position;
         // modify the variable to keep y within minY to maxY
         currentPosition.y = Mathf.Clamp( currentPosition.y, minY, maxY);
            // and now set the transform position to our modified vector
         transform.position = currentPosition;
     
         
         //rotate to look at the player
         transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(Player.transform.position - transform.position), rotationSpeed*Time.deltaTime);
         
         //move towards the player
         transform.position += transform.forward * speed * Time.deltaTime;
         
         if (Vector3.Distance(Player.transform.position,transform.position)<=howCloseToPlayer){
             rotationSpeed=0;
             speed=0;
             //Do Damage or whatever you want in here
         }
     }
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 pulkit8.mahajan · Jan 14, 2014 at 12:48 AM 0
Share

I will up vote this as soon as i can

avatar image robertbu · Jan 14, 2014 at 12:50 AM 0
Share

@pulkit8.mahajan - if this is the answer you consider best/correct, click on the checkmark at the top left of this answer.

avatar image getyour411 · Jan 14, 2014 at 12:51 AM 0
Share

If your terrain has height variance, beware of using transform.position to move things in this manner, they will fall through

avatar image sath · Jan 14, 2014 at 06:59 AM 0
Share

as @getyour411 said and propably is true , edit up the above script for position.y

avatar image SunnyChow · Jan 14, 2014 at 07:08 AM 1
Share

use this: rigidbody.$$anonymous$$ovePosition(newPosition)

if you arrange position though transform, the physics system doesn't know you moved the objects and won't help you to check the collision

Show more comments
avatar image
0

Answer by Nirav-Madhani · Nov 30, 2014 at 09:28 AM

Add to Update (JS)

 // add outside functions
 var Player : Transform;
 var MoveSpeed = 4;
 var MaxDist = 10;
 var MinDist = 5;
 // add to Update
 transform.LookAt(Player);
     if(Vector3.Distance(transform.position,Player.position) >= MinDist){
     transform.position += transform.forward*MoveSpeed*Time.deltaTime;
     if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
     {
     //Here Call any function U want Like Shoot at here or something
     
     }
     }





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

22 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

Related Questions

Moving object detection in real time 2 Answers

Object move using android Accelerameter. 1 Answer

Need help moving player object towards an instantiated ball 1 Answer

Why do my triggers not work when I build my game? 0 Answers

Reposition camera behind the player character during movement 1 Answer


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