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 Bob5602 · Mar 02, 2011 at 02:45 PM · javascriptcollisionaiobstacleavoid

AI Object Avoidance help

I am working on making my AI avoid obstacles during space flight, but things aren't working well.

The ship is moving forward, using 'addrelativeforce' to maintain a speed of 100. I put a wall infront of the ship that I want it to avoid. Eventually I'm going to add some things to make it avoid to the left, right, up, or down, but for now I am only concentrating on the 'up'

My current workflow -

Checks to see if obstacle is in the way (using a rigidbody sweep test) about 100 units out. Normally here it would check to see if left,right,up,or down are easiest to escape, but for the purposes of testing I am only dealing with 'up.' After the test, I am trying to avoid the wall by adding relative torque so that the nose of my ship goes up.

Basically, as soon as my sweep test is true, my ship applies breaks (addrelativeforce in a negative forward direction), applies relative torque to turn 'up' and adds a forward acceleration using 'addrelativeforce' again.

This all happens in a function that is called every fixed update. Once that function returns false(aka rigidbody sweeptest fails), I call a 'straightenout' function I have that adjust the ship velocity from whatever direction it was in to forward.

EDIT -----

Here is some code. I don't have access to my actual code at the moment so this is coming from memory

var wasTurning : boolean; var status : String; var straightening : boolean = false;

function FixedUpdate(){

if(ObstacleAvoid()){ wasTurning = true; status = "evading"; }

if(!ObstacleAvoid()){ if (wasTurning){ StraightenOut(); wasTurning = false; }

}

switch(status){ case "normal": Accelerate(); TrackTarget(); break; case "evading": Accelerate(); ObstacleAvoid(); break; }

}

function Accelerate(){ rigidbody.AddRelativeForce(Vector3.forward 25); } function ObstacleAvoid(){ var bodyHit : RayCastHit; if(rigidbody.SweepTest(transform.position, Transform.forward, bodyHit, 100){ Break(); rigidbody.AddRelativeTorque(Vector3.right 10); rigidbody.AddRelativeForce(Vector3.forward * 25); } }

function Break(){ rigidbody.AddRelativeForce(-Vector3.forward * 10); }

function StraightenOut(){ straightening = true; startVelocity = rigidbody.velocity.magnitude; }

This code is obviously not EVERYTHING I have. The way straightening works is if the function is called and straighten = true, then it lerps between the old velocity to the new velocity which is equal to Transform.forward.normalized * startVelocity. This way, it takes the speed you were moving before in a certain direction, and slowly morphs it into the same velocity going forward. This is what I use to keep my ships from flying around in big circles due to momentum.

Also, TrackTarget() which is called everytime you're in status "normal" merely takes the enemy position and rotatestoward it over time so the ship faces the target. Once the target is lined up, it also calls the StraightenOut() function to make flying straight once again.

Since this is not all my code, there are other cases that set the status back to "normal" during the FixedUpdate and unless the ObstacleAvoid sets it back to "evading" the status stays at normal.

----EDIT

However, my ship doesn't smoothly avoid by going over the obstacles, and in stead does a weird slowdown while turning upward, even moves backwards for a moment, then goes forward off in a weird direction as soon as it starts trying to fly straight again. I presume this is due to the relativetorque I kept adding as it was avoiding, but I don't know a better way to do it.

Any ideas?

Comment
Add comment · Show 8
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 Jesse Anders · Mar 02, 2011 at 03:38 PM 0
Share

If you could edit your post and include your 'fixed update' function (or the relevant portions of it), that might make it easier to guess as to what the problem might be.

avatar image Bob5602 · Mar 02, 2011 at 03:59 PM 0
Share

Sadly I do not have access to my code at the moment. I'll write up a suitable facsimile of my code and post that shortly.

avatar image PrimeDerektive · Mar 02, 2011 at 04:04 PM 0
Share
  • for surfing UnityAnswers at work ;)

avatar image Bob5602 · Mar 02, 2011 at 04:11 PM 0
Share

I can't work on my game during work, might as well try to figure out some things via unityanswers before I get a chance to work on it again :)

avatar image PrimeDerektive · Mar 02, 2011 at 04:29 PM 0
Share

I probably learned everything I know using the same strategy!

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

AI to make an object wander around and avoid obstacles? 5 Answers

Collision detection with raycast 1 Answer

Keeping an raycasting AI within a patrol area 1 Answer

Collsions problem 0 Answers

Adding Object Avoidance to my Enemy AI Script 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