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
1
Question by MonkeyAssassin8 · Dec 15, 2010 at 05:45 AM · airaycast

Does anyone know how to make an AI that dodges obstacles when chasing the player?

Here's what i've got so far, as it says in the title; I want my AI to move around obstacles when he is chasing the player, if anyone would be kind enough to give me some tips it would be much appreciated.

so far, the enemy chases the player when they get into range and if the enemy loses sight of the player, it will go back the other way and patrol using raycast...

var speed : float = 9; var rotateSpeed : float = 1; private var direction : float; var forwardRay : float = 2; var leftRay : float = 5; var rightRay : float = 5; var player : Transform; var MaxDistance : int = 30; var MinDistance : int = 5;

function Update () {

var distance : Vector3 = player.position - transform.position; //if player is in range, enemy will follow if(distance.magnitude < MaxDistance){ if(distance.magnitude > MinDistance){ Debug.DrawLine(transform.position, player.position); if(!Physics.Linecast(transform.position, player.position)){ Pursue(); }else if(Physics.Linecast(transform.position, player.position)){ Patrol(); } } }else{ Patrol(); } }

function Patrol(){ //if there is nothing within range, then move forward if(!Physics.Raycast(transform.position, transform.forward, forwardRay)){ transform.Translate(Vector3.forward speed Time.smoothDeltaTime); }else{ //if there is something to the left, then direction = 1 if(Physics.Raycast(transform.position, -transform.right, leftRay)){ direction = 1; //if there is something to the right, the direction = -1 }else if(Physics.Raycast(transform.position, transform.right, rightRay)){ direction = -1; //if there is nothing to the left or right, then direction = 1 }else{ var RandomDirection = Random.value; if(RandomDirection > 0.5){ direction = 1; }else{ direction = -1; } } //rotate 90 degrees in the direction (1 = right, -1 = left) transform.Rotate(Vector3.up, 90 rotateSpeed direction); } }

function Pursue(){ var distance : Vector3 = player.position - transform.position;

     //move towards player
     velocity = distance.normalized * speed;
     rigidbody.velocity = velocity;

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Jesse Anders · Dec 15, 2010 at 11:25 AM

Depending on the nature of the obstacles, steering behaviors might be appropriate (this approach allows you to easily combine and prioritize different behaviors, such as chasing a target and avoiding obstacles).

Although I've never looked at it myself, I believe there's a 'steering behaviors' library available that can be used with Unity.

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 Statement · Dec 15, 2010 at 12:22 PM

Aron Granberg have made an impressive A* path-finding system. Maybe you can use it to give your AI a boost?

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

No one has followed this question yet.

Related Questions

How to get information from raycast? 2 Answers

Raycast intersection 1 Answer

How to get a Vector3 from a RaycastHit variable? 1 Answer

destroy gameobject with ray 1 Answer

For each Raycast? 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