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 /
  • Help Room /
avatar image
1
Question by Higgsboson728 · Feb 01, 2017 at 12:02 PM · 3daienemyai

Help with enemy AI

Hi,

I'm having some trouble programming the AI of an enemy in my game. The enemy I'm making is supposed to ram the player and try to get the player off the edge of the map. So far, I have programmed the enemy to follow the player around. I want to program the enemy to actually start backing up and ramming the player once it gets close enough, instead of just pushing against the player when it reaches you.

Here is (a part of) my code so far:

 void FixedUpdate () {
 
         if (transform.position.y < -5 || player.transform.position.y < -50) {
 
             WaveSpawner.enemiesAlive--;
             Destroy (gameObject);
         }
             
         inactive = transform.position.y < 0.5;
 
         if (!inactive) {
 
             if (Vector3.Distance (transform.position, player.transform.position) > 2) {
 
                 rb.AddForce ((player.transform.position - transform.position).normalized * speed);
 
             } else if (Vector3.Distance (transform.position, player.transform.position) < 2) {
 
                 rb.AddForce (-(player.transform.position - transform.position).normalized * (speed / 3));
             } 
 
         }
 
     }
 

This doesn't work because whenever the enemy starts to ram the player, the script that tells the enemy to back up starts coming in and stops the enemy from ramming the player.

I tried many other possible solutions to the problem, but none of them worked.

Is there any way to make it work?

*Note: I know that the code is messy, I just want to make sure it works first before fixing it

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

1 Reply

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

Answer by oStaiko · Feb 04, 2017 at 07:34 PM

Try to make a script that's not based on distance, but based on collision and time!

 float collisionTime;
 
 OnCollisionEnter (Collider other)
 {
     collisionTime = Time.time;
 }
 
 Update ()
 {
     if (Time.time > collisionTime + 2)
     {
         //Go Forwards
     }
     else
     {
         //Reverse
     }
 }

I've done something exactly like this once, although I took a different approach where I would just add a strong backwards force to them if they got stuck on a wall/player for too long. It also helps if you add a bouncy PhysicsMaterial to the enemies, so they automatically bounce off a bit.

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 Higgsboson728 · Feb 05, 2017 at 05:02 AM 1
Share

Thank you very much! This is exactly what I was looking for.

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

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

Related Questions

Problems with basic enemyAI on sphereical world 0 Answers

AI stops following player. 1 Answer

Enemy AI Issues!!?! 2 Answers

Help to make enemy AI not go through terrain and not fly when chasing player up hills. 0 Answers

How to keep vehicles in correct lanes 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