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
1
Question by Delthrox · Oct 18, 2016 at 04:27 PM · movementaiai problemsdodge

Ai dodge bullets

I am making a geometry wars clone in 3d. I am now working on the green enemy which dodges the players bullets. https://www.youtube.com/watch?v=OxtZUURTHzs

I have a system that detects when a bullet comes close. It then calls the evadeBullet() function. The problem I have with this, is that it doesn't work as I want to. I want the enemy to sidestep when the bullet comes close. I have no idea how to do that.

With the code below, if the enemy comes from above the code works but when the enemy comes from the right the enemy runs right into the bullet. I could really use some help to get the same behaviour as the original geometry wars.

  public void evadeBullet(GameObject bullet)
  {
      Vector3 moveDirection = bullet.transform.position - this.transform.position;
      moveDirection *= -1;
      float step = evadeSpeed * Time.deltaTime;
 
      //Debug.Log(moveDirection);
 
      transform.position = Vector3.MoveTowards(this.transform.position, moveDirection, step);
      
  }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by stormAster720 · Oct 11, 2018 at 10:29 PM

@Delthrox i think, you should use transform.RotateArround(moveDirection) or if you are using onTriggerEnter to detect bullets, you can use transform.RotateArround(other.transform) sorry for my bad english, it is not my native languaje

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
avatar image
0

Answer by jasperbarg · Aug 15, 2019 at 07:54 PM

@Delthrox I happened to come across your problem trying to find a solution myself. I managed to fix the problem by having putting an empty gameobject (Right or Left) besides the projectile gameobject. Whenever you want the AI to sidestep just calculate the direction form the projectile to the empty gameobject and send that to the AI you want to evade, since that direction is the direction you want the AI to move in. Sorry in advance if the code is a bit messy. The following code is located on the bullet gameobject:

     private void InPath()
     {
         if (EnemyAI != null)
             EnemyAI.inBulletPath = false;
         Ray ray = new Ray(transform.position, transform.forward);
         RaycastHit hit;
 
         if (Physics.SphereCast(ray, 0.5f, out hit))
         {
             if (hit.transform.tag == "Enemy")
             {
                 EnemyAI = hit.transform.GetComponentInParent<EnemyAI>();
                 EnemyAI.inBulletPath = true;
                 if (rayHitTarget)
                 {
                     Vector3 ghostPosition = RightOrLeft();
                     EnemyAI.evadeDirection = new Vector3(transform.position.x, 0, transform.position.z) - new Vector3(ghostPosition.x, 0, ghostPosition.z);
                     rayHitTarget = false;
                 }
             }
             else
                 rayHitTarget = true;
         }
     }
     private Vector3 RightOrLeft()
     {
         if (Random.Range(0, 2) < 1)
             return Right.position;
         else
             return Left.position;
     }

Then in the AI script just call the methode whenever the AI is in the path of the bullet. The two pieces of code a both located on the AI gameobject

 private void EvadeBullet()
     {
         Entity.Move(evadeDirection.normalized);
     }

Entity.Move Basicly handles the movement and rotation of the gameobject.

 //verander acceleration
         if(direction.magnitude > 0)
         {
             movementAcceleration += movementAccelerationFactor;
             lastDirection = direction;
         }
         else
         {
             movementAcceleration -= movementAccelerationFactor;
         }
 
         //clamp acceleration tussen 0 en 1
         movementAcceleration = Mathf.Clamp(movementAcceleration, 0f, 1f);
 
         //move
         transform.Translate(lastDirection * movementSpeed * movementAcceleration * Time.deltaTime, Space.World);
 
         //audio moet nog gebeuren
 
         //rotation tank
         if (direction.magnitude > 0)
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed* Time.deltaTime);

I hope this helps a bit

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
avatar image
0

Answer by Cornelis-de-Jager · Aug 15, 2019 at 09:39 PM

Hi @Dlethrox, your solution is very close to what it should be. I mean really close! Here is a tip that will help you to remember direction:

 var direction = [Destination] - [Origin]
 

Now for the solution... Change this:

 transform.position = Vector3.MoveTowards(transform.position, moveDirection, step);

To this:

 transform.position = Vector3.MoveTowards(transform.position, transform.position + moveDirection, step);
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

AI code for enemy car to follow the player car 0 Answers

I'm trying to make AI that walks (and faces) towards a waypoint 1 Answer

Simple movement causing lag (on standalone) 1 Answer

Enemy Spaceship Movement AI Curve Movement Need help Please 0 Answers

Random movement AI 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