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 Persona · Mar 26, 2011 at 11:04 PM · collisionrigidbodytransformaiforce

Make AI run from Player

I have an AI the relies on Force to move, but I need it to flee from the player's position once it reaches a certain distance. Normally this wouldn't be a problem, but once they hit a wall, they get stuck and using OnCollisionEnter doesn't help since they will force themselves towards the wall until they get locked. The game runs in 2D with the Z-axis locked. I tried the bounce material with similar results and raycasting is out of the question.

Question: How do I get the AI to flee from the player, but go the opposite way once it hits the wall, without touching the player?

var FromPlayer: Vector2 = Target.position - transform.position; if(FromPlayer.magnitude < detectionRange){ rigidbody.AddForce(1,2,1, ForceMode.Impulse); //rigidbody.velocity = -transform.forward * 2;

 if(transform.position.y == Target.position.y){
 rigidbody.AddForce(0, Random.Range(-1, 1), 0, ForceMode.Impulse);
 }

 if(FromPlayer.magnitude &lt; 2){
 rigidbody.AddForce(transform.forward * 1, ForceMode.Impulse);

}

function OnCollisionEnter(other: Collision){ if(other.gameObject.CompareTag("Wall")){ rigidbody.AddForce (Random.Range(-2, 2), Random.Range(-1, 1), 0, ForceMode.Impulse);

 }


}

Comment
Add comment · Show 2
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 Michael 12 · Mar 27, 2011 at 02:41 AM 0
Share

I just posted a script that might work well for you. Only thing is that they stop when out of range and start running again when you get into range, But I'll bet that can be tweaked though.

LIN$$anonymous$$Y:

http://answers.unity3d.com/questions/42238/an-ai-that-will-move-away-or-move-to-another-waypoint-when-player-goes-near

avatar image Persona · Mar 28, 2011 at 02:21 AM 0
Share

Sorry, but no character controllers are used for it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Uzquiano · Mar 26, 2011 at 11:51 PM

Hi,

Just one advise for checking the distance with the playes, take a look to colliders, specially to the OnTriger...() functions .

Then with the wall staff, I'm trying to figure out that what you want to get is just a constant minimum distance respect the player, so why not OnTriggerStay?

If you it is not exactly what you need, please give me more details.

Cheers,

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 Persona · Mar 28, 2011 at 02:20 AM 0
Share

The idea is to avoid the player's collider by stopping and reversing direction, trigger or not, otherwise they are destroyed.

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

No one has followed this question yet.

Related Questions

Detect collision/trigger between two body without rigidbody? 3 Answers

Why I've a gap between meshcolliders? 0 Answers

can not move my enemy with rigidbody 1 Answer

Limit How Far an Object is Pushed After a Collision 0 Answers

Reuse a collision 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