• 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.

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

No one has followed this question yet.

Related Questions

Is there a solution to when colliders bypass? 2 Answers

Collider question 0 Answers

How do I move this rigidbody? 1 Answer

What does rigidbodies sleeping have to do with collision detection? 0 Answers

How to properly move a rigidbody/collider? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges