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 Chimera3D · May 27, 2012 at 02:05 AM · rotationaiobstacleturningavoidance

Unity AI Obstacle Avoidance Help

Ok I have an AI script that works (so far), except for the obstacle avoidance. What I want is, when one of the raycast hits an object for it to turn and then continue to head towards the target (which may turn from the next waypoint to the player). The problem is that the enemy jitters in a certain direction and then tries to go to the waypoint. Also is there anyway I can write this in less code? I'm pretty noob at complex stuff like this, the collision detection was horribly written but sort of works aside from the jittering instead of turning and then trying to go to the waypoint. Here is the code, it's very long:

 var rotationSpeed = 5;
 var moveSpeed = 5;
 var maxHealth = 100.0;
 var curHealth = 100.0;
 var healthBarLength : float;
 var waypoint : GameObject[];
 var currentWaypoint : int;
 var maxWaypoint : int;
 var target : GameObject;
 var startingDrag : float;
 var groundDistance : float;
 var isGrouned : boolean;
 var isObstructed : boolean;
 
 function Start(){
 healthBarLength = Screen.width / 4;
 
 distToGround = collider.bounds.extents.y;
 
 waypoint = GameObject.FindGameObjectsWithTag ("Waypoint");
 
 System.Array.Reverse(waypoint);
 
 startingDrag = rigidbody.drag;
 
 isObstructed = false;
 }
 
 function Update(){
     
     target = waypoint[currentWaypoint];
     
     var fwd = transform.TransformDirection (Vector3.forward);
     var l25D = Quaternion.Euler(0,-25,0) * transform.forward;
     var r25D = Quaternion.Euler(0,25,0) * transform.forward;
     var l50D = Quaternion.Euler(0,-50,0) * transform.forward;
     var r50D = Quaternion.Euler(0,50,0) * transform.forward;
     
     var left1 = Quaternion.LookRotation(l25D);
     var right1 = Quaternion.LookRotation(r25D);
     var left2 = Quaternion.LookRotation(l50D);
     var right2 = Quaternion.LookRotation(r50D);
     
     
     var targetPos = target.transform.position;
     var targetRotation = Quaternion.LookRotation(targetPos - transform.position);
     var i = Time.deltaTime * rotationSpeed;
     var m = Time.deltaTime * moveSpeed;
     var relativePos = targetPos - transform.position;
    
   if(isObstructed == false){ 
     transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, i);
     rigidbody.AddForce(m * relativePos.normalized);
   }
     
     if(currentWaypoint >= maxWaypoint){
         currentWaypoint = 0;
     }
     
     if (Physics.Raycast (transform.position, fwd, 3)) {
         
     }
     
     if (Physics.Raycast (transform.position, l25D, 3)) {
         isObstructed = true;
         transform.rotation = Quaternion.Slerp(transform.rotation, right2, i);
         rigidbody.AddForce(Vector3.right2 * m);
     }else
     
     Invoke ("turn", 4);
     
     if (Physics.Raycast (transform.position, l50D, 3)) {
          isObstructed = true;
          transform.rotation = Quaternion.Slerp(transform.rotation, right1, i);
          rigidbody.AddForce(Vector3.right1 * m);
     }else
     
     Invoke ("turn", 4);
     
     if (Physics.Raycast (transform.position, r25D, 3)) {
         isObstructed = true;
         transform.rotation = Quaternion.Slerp(transform.rotation, left2, i);
         rigidbody.AddForce(Vector3.left2 * m);
     }else
     
     Invoke ("turn", 4);
     
     if (Physics.Raycast (transform.position, r50D, 3)) {
         isObstructed = true;
         transform.rotation = Quaternion.Slerp(transform.rotation, left1, i);
         rigidbody.AddForce(Vector3.left1 * m);
     }else
     
     Invoke ("turn", 4);
 
 }
 
 function Health() {
         
         if(curHealth < 0)
             curHealth = 0;
         
         if(curHealth > maxHealth)
             curHealth = maxHealth;
     
         if(maxHealth < 1)
             maxHealth = 1;
         
         healthBarLength = (Screen.width / 4) * (curHealth / maxHealth);
         
 }
 
 function OnGUI() {
         GUI.Box(Rect(1050, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
 }
 
 function OnDrawGizmos () {
     
     var fwd = transform.TransformDirection (Vector3.forward);
     var l25D = Quaternion.Euler(0,-25,0) * transform.forward;
     var r25D = Quaternion.Euler(0,25,0) * transform.forward;
     var l50D = Quaternion.Euler(0,-50,0) * transform.forward;
     var r50D = Quaternion.Euler(0,50,0) * transform.forward;
 
     // Draws a 5 meter long red line in front of the object
     Gizmos.color = Color.red;
     var direction : Vector3 = transform.TransformDirection (Vector3.forward) * 3;
     var direction2 : Vector3 = l25D * 3;
     var direction3 : Vector3 = l50D * 3;
     var direction4 : Vector3 = r25D * 3;
     var direction5 : Vector3 = r50D * 3;
     Gizmos.DrawRay (transform.position, direction);
     Gizmos.DrawRay (transform.position, direction2);
     Gizmos.DrawRay (transform.position, direction3);
     Gizmos.DrawRay (transform.position, direction4);
     Gizmos.DrawRay (transform.position, direction5);
 }
 
 function OnTriggerEnter(info : Collider){
     
     if(info.tag == "Waypoint"){
         currentWaypoint++;
         yield slow();
     }
 }    
 
 function slow(){
     
     rigidbody.drag = 100;
     
     yield WaitForSeconds(.1);
     
     rigidbody.drag = startingDrag;
     
 }
 
 function turn(){
     
     isObstructed = false;
     
 }
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

3 Replies

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

Answer by csetter · Aug 18, 2012 at 03:59 PM

I'm doing something similar and ran into the same problem. To stop the jittering, you need to stop the your game object from trying to head to your next waypoint, until it's passed the obstacle.

I did this by creating a temporary waypoint at the colission point, and then moving the temporary waypoint along the z axis until the raycast hits stopped.

You need to store the original waypoint you're heading too, and then re-establish it once you reach the temporary waypoint, and then destroy the temporary waypoint.

You could also add some velocity retardation based on distance if you needed too.

Hope this helps (took me a few nights to work this out)

Chris

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
1

Answer by lyzard · May 27, 2012 at 10:08 AM

I think I had a similar problem with my NPC. For collision avoidance, I looked at the tangent points of the obstacle to define the new target and what happened is that for 1 frame direction was corrected by the collision avoidance, but the frame after, the path was clear and the direction was setup by the next waypoint, however while moving the NPC, the next frame made it necessary to correct again. So it alternated between a waypoint target and a fixed target, making the character to jitter. What I did was simply smoothing the current rotation to the expected rotation, hence reducing the jitter, with something like:

rigidbody.MoveRotation(Quaternion.Slerp(rigidbody.rotation, Quaternion.LookRotation(look), 0.2f));

It's not perfect though, but it's a first step in the right direction I think.

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 Chimera3D · May 27, 2012 at 08:40 PM 0
Share

Thanks I'm gonna try it.

avatar image
0

Answer by GC1983 · May 27, 2012 at 02:40 AM

One name....SimplePath.

Comment
Add comment · Show 2 · 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 GC1983 · May 27, 2012 at 04:08 AM 0
Share

Really? Youre going to give me negative karma for suggesting something that will make your life so much easier than writing 1000 lines of uncertain object avoidance code?....

avatar image Bunny83 · May 27, 2012 at 10:38 AM 0
Share

I don't see misinformation or something that is wrong in this answer. Well it's not that comprehensive, but nothing that would explain a downvote. If you downvote a post, give at least a reason in a comment. Voting is nothing personal here. This is like a wiki to collect knowledge for the whole community.

I'll add at least a link to increase the value of this answer ;)

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Follow AI which avoides obstacles 1 Answer

How can I make an AI that avoids obstacles WITHOUT using NavMesh? 3 Answers

Raycast Steering Problem 1 Answer

Script is Flipping Out! 2 Answers

Enemy Rotation AI 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