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
2
Question by PrimeDerektive · Dec 01, 2010 at 09:28 PM · rotationraycastslerpobstacleavoid

Can't figure out how to make my raycast obstacle avoidance less jittery

I'm currently using a raycast obstacle avoidance solution that I cobbled together, and it works okay, but has a few problems.

Chief among them is that when my AI object is avoiding, it is very jittery. I know exactly why its jittery, because I'm adding the hit.normal (multiplied by a modifier) of the raycast to my target direction for my eventual slerp, so the frames that the AI is avoiding the target direction is instantly changed, resulting in a jitter.

I know I need to probably Vector3.Lerp my avoidance direction (the original target direction plus the offset from the raycast hit.normal) and my original target direction, I just have no idea where to put it!

Here's my whole script in its current incarnation:

var rayLength : float = 10.0;

private var rayArray : Vector3[];

function Start(){ rayArray = new Vector3[3]; }

function MoveTowardsAndAvoid(target : Transform, speed : float, rotSpeed : float){

 var targetPos : Vector3 = target.position;
 targetPos.y = transform.position.y; //set y to zero so we only rotate on one plane
 var targetDir : Vector3 = targetPos - transform.position;

 rayArray[0] = transform.TransformDirection(-0.20,0,0.5); //ray pointed slightly left 
 rayArray[2] = transform.TransformDirection(0.20,0,0.5); //ray pointed slightly right 
 rayArray[1] = transform.forward; //ray 1 is pointed straight ahead

 //loop through the rays
 for (i=0; i<3; i++) {
     var hit : RaycastHit;
     // if you hit something with the ray......
     if (Physics.Raycast (transform.position, rayArray[i], hit, rayLength)) {        
         Debug.DrawLine(transform.position, hit.point, Color.magenta);
         targetDir += 50*hit.normal;
     }
 }

 // rotation and movement code 
 var targetRotation = Quaternion.LookRotation(targetDir);
 transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotSpeed*Time.deltaTime);
 transform.Translate(Vector3.forward*Time.deltaTime*speed);

}

Any ideas?

Comment
Add comment · Show 1
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 Hamesh81 · Mar 11, 2012 at 09:11 AM 0
Share

Did you end up solving this Derek?

I'm having the a very similar problem using a single raycast to rotate a gameobject based on the hit.normal and Quaternion.LookRotation(dir). It gets quite jittery only during the obstacle avoidance, otherwise it's very smooth. I have posted a question about it here: How to apply dampening to hit normal based rotation?

Would really appreciate some help about this from someone, or even an alternative approach.

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by reissgrant · Feb 07, 2011 at 12:58 AM

Try this:

var rayLength : float = 10.0; var lerpSpeed : float = 1.0; var theTarget : GameObject; var mult : float = 1.0; var speed : float = 1.0; var rotSpeed : float = .15;

private var rayArray : Vector3[]; private var lerpedTargetDir : Vector3;

function Start(){ rayArray = new Vector3[3]; }

function Update(){

 MoveTowardsAndAvoid(theTarget.transform);

}

function MoveTowardsAndAvoid(target : Transform){

 var targetPos : Vector3 = target.position;
 targetPos.y = transform.position.y; //set y to zero so we only rotate on one plane
 var targetDir : Vector3 = targetPos - transform.position;

 rayArray[0] = transform.TransformDirection(-0.20,0,0.5); //ray pointed slightly left 
 rayArray[2] = transform.TransformDirection(0.20,0,0.5); //ray pointed slightly right 
 rayArray[1] = transform.forward; //ray 1 is pointed straight ahead

 moveIt = false;

 //loop through the rays
 for (i=0; i<3; i++) {
     var hit : RaycastHit;
     // if you hit something with the ray......
     if (Physics.Raycast (transform.position, rayArray[i], hit, rayLength)) {     

         Debug.DrawLine(transform.position, hit.point, Color.magenta);

         targetDir += mult * hit.normal;
     } else {

         moveIt = true;

     }

 }

 // rotation and movement code 

     lerpedTargetDir = Vector3.Lerp(lerpedTargetDir,targetDir,Time.deltaTime * lerpSpeed);
     var targetRotation = Quaternion.LookRotation(lerpedTargetDir);
     transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotSpeed*Time.deltaTime);

     if(moveIt){     transform.Translate(Vector3.forward*Time.deltaTime*speed);  }

}

Comment
Add comment · Show 8 · 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 PrimeDerektive · Feb 07, 2011 at 01:04 AM 0
Share

Nah, I tried something basically just like this (and just tried yours just to be safe). It bounces slowly bounces back and fourth slowly now, like a more pronounced jitter.

avatar image reissgrant · Feb 07, 2011 at 01:08 AM 0
Share

You can try increasing the lerp speed variable to 10, then 100 to see if it helps. Sorry, I don't have the accompanying code to test this.

avatar image reissgrant · Feb 07, 2011 at 01:15 AM 0
Share

Ah ok I setup a test scene, that lerpspeed doesn't help. I'll see if theres something else that can be done.

avatar image reissgrant · Feb 07, 2011 at 01:57 AM 0
Share

I set the lerpSpeed to 1 and rotSpeed to .1 and I get nice smooth rotations.

  1. is probably too smooth though.

avatar image reissgrant · Feb 07, 2011 at 02:21 AM 1
Share

I updated my code above to get smooth rotations, also it only moves if there is an open path.

Show more comments
avatar image
1

Answer by Jesse Anders · Feb 07, 2011 at 02:21 AM

One thing you might try is to respond only to one of the raycasts, rather than (potentially) to all three. For example, you could respond to the closest one, or to the one for which the normal is most aligned towards the agent.

I'd also recommend consulting the literature on steering behaviors (or reviewing it, if you've already covered that material). These behaviors are all pretty well defined, and you can find example implementations in libraries such as OpenSteer, UnitySteer etc. By looking at how it's done elsewhere (or just reading the description in the 'canonical' paper on the subject) and comparing it to what you're doing, you should be able to figure out what's causing the problem.

Sorry to be a little vague, but to offer a specific suggestion I'd probably have to dig into the code myself. In the meantime though, perhaps the above suggestions will be of some help.

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 PrimeDerektive · Feb 07, 2011 at 02:48 AM 1
Share

The documentation for UnitySteer seems deliberately obtuse or borderline nonexistent, but I would love to use it if I could. It doesn't seem too easy to implement into your own AI scripts without being forced to use their vehicle scripts and code around it.

avatar image
0

Answer by DaveA · Feb 07, 2011 at 07:59 PM

What if you put the 'move' stuff in Update() and the 'find' stuff in LateUpdate()?

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 PrimeDerektive · Feb 07, 2011 at 08:27 PM 0
Share

What do you mean? If the move/rotate stuff was in Update() and I need to modify the targetDir of my rotation slerp when I hit an obstacle, how would I modify the rotation after it already happened?

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

2 People are following this question.

avatar image avatar image

Related Questions

Raycasting and changing player rotation accoording to what is underneath. 0 Answers

Keeping an raycasting AI within a patrol area 1 Answer

Jitter on Slerp - Raycast 0 Answers

Slerp doesnt go to the rotation its supposed to go to 1 Answer

Use Lerp to rotate object 2 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