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 Karsnen_2 · Oct 30, 2012 at 07:59 PM · c#iosaipathfinding

Evade a Collider on Vector3.forward

Hello,

My player is following a target (constantly moving). As the player is looking at the target always ( say LookAt function), I would like to evade all the objects on the pathway. The target can go thro' the collider though but the player can't.

Now, my game does not have terrain or does it have tiles. It happens in mid-air. Even though there are a lot of ways to do it, i am not sure of the what would be the best approach.

Possible options I have in my mind:

  1. Unity Steer

  2. Physics Ray-cast

With either of the above I have not got that experience. If you find any other conservative way to deal with it, let me know.

Thank you & Please!!!

EDIT:

The below picture is how my scene would look like. Kindly imagine that, non of the objects are grounded. They are in mid-air. The Blue line connecting the player and the target is the intended path where has a collider right in the middle. The red dotted line is the path evading the collider. It is also like to be noted that, as the gameobjects are lying in mid-air, the redline could be drawn below the collider also.

The Image :

alt text

Just in case - I have also uploaded here.

scenarioshot.jpg (59.2 kB)
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 ThePunisher · Oct 30, 2012 at 11:42 PM 0
Share

I'm not sure I understand you entirely. Can you provide a more defined scenario detailing what the target/player is avoiding/colliding with.

avatar image Karsnen_2 · Oct 31, 2012 at 12:14 AM 0
Share

ThePunisher, I am glad that you are trying to help me. Thank you. $$anonymous$$indly look into the question. I have updated the question with more info at the "EDIT" section.

3 Replies

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

Answer by tavoevoe · Oct 31, 2012 at 09:17 PM

You're asking a relatively simple question, but that's kind of what AI is. You're player wants to A: get to the target and B: avoid all obstacles. You've got to balance the SEEK desire with the AVOID desire to generate a direction for the player each update.

1) generate a vector towards the target each update. Call it seekVector.

2) Generate a vector away from anything you don't want to touch. You can do this by giving your player "wiskers". Raycast in front of him so he can "see" if he's going to hit something. If he will, generate a Vector3 to the right or left. Something, that, if applied, would drive him away from the obstacle. Call it avoidVector.

3) Now you need to give each of these desires a weight. avoidWeight and seekWeight. Normalize each vector, multiple by its weight, and move your character that amount.

  • You could make the avoid weight based on the distance between your character and the obstacle, if you want to be class about it. So, the closer you are to a wall, the more strongly you want to avoid it.

  • Alternatively, if you want him to run straight into walls in his haste to get to his target, crank seekWeight through the roof.

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 Karsnen_2 · Nov 05, 2012 at 08:58 PM 0
Share

Exactly what I was expecting!!! Thank you.

avatar image
1

Answer by Sonaten · Oct 30, 2012 at 09:02 PM

You could make use of two colliders on the targets you want to avoid. One collider that takes care of physical collition. And another, bigger collider, that is used as a trigger. Once the character is within the trigger, it will start to de-route (you could calculate the way around based on vector projections).

Hope this approach is viable for you.

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 Karsnen_2 · Oct 31, 2012 at 12:15 AM 0
Share

Niklas - Thanks. I see that but how to de-route once it is been activated by the trigger. I lost you on that path. "Calculate a way around the vector projections".

Just to let you know, I have updated the question with more info at the "EDIT" section. $$anonymous$$indly do look into it.

avatar image
-1

Answer by Montraydavis · Oct 31, 2012 at 06:35 PM

I created something similar with Raycasts, but, it will require you to do a lot of polishing to get the effect to look very realistic. But none the less, the object will avoid obstacles, but requires a Target be set . . .

With having that said, I suggest using this only as a starting point. It contains most of what you need for a fully fledged obstacle avoidance .

Currently, it supports obstacle avoidance forward, left, and right, but does not work if there are colliders on both the left and right side. . . You can change this, and fix it up a bit though.

PS: Feel free to use this script in whatever way you want .

Here you go:

 #pragma strict
 
 var Target : GameObject ;
 
 var TargetHit : RaycastHit ;
 var TargetHitLeft : RaycastHit ;
 var TargetHitBack : RaycastHit ;
 
 
 
 class _VisionRaycastHit{
     var Back : RaycastHit ;
     var Front : RaycastHit ;
     var Left : RaycastHit ;
     var Right : RaycastHit;
 }
 
 class _VisionDirection {
     var Left : float = 0 ;
     var Right : float = 0 ;
     
     var LeftActive : boolean = false ;
     var RightActive : boolean = false ;
 }
 
 class _Vision {
     var Back : boolean = false ;
     var Front : boolean = false ;
     var Left : boolean = false ;
     var Right : boolean = false ;
     
     var EaseOut : float = 2 ; 
     
     var Hit : _VisionRaycastHit ;
     
     var VisionDirection : _VisionDirection ;
 }
 
 var Vision : _Vision = new _Vision ( ) ;
 
 
 function Start () {
 
 }
 
 function Update () {
 
     Vision.Front = Physics.Raycast(Vector3(transform.position.x, transform.position.y+1, transform.position.z), transform.TransformDirection( Vector3.forward ), Vision.Hit.Front, 15);
     Vision.Back = Physics.Raycast(Vector3(transform.position.x, transform.position.y+1, transform.position.z), transform.TransformDirection( Vector3.back ), Vision.Hit.Back, 15);
     Vision.Left = Physics.Raycast(Vector3(transform.position.x, transform.position.y+1, transform.position.z), transform.TransformDirection( Vector3.left ), Vision.Hit.Left, 15);
     Vision.Right = Physics.Raycast(Vector3(transform.position.x, transform.position.y+1, transform.position.z), transform.TransformDirection( Vector3.right ), Vision.Hit.Right, 15);
     //VisionLeft = Physics.Raycast(Vector3(transform.position.x, transform.position.y+1, transform.position.z), transform.TransformDirection( Vector3.left), TargetHitLeft, 15);
     //VisionBack = Physics.Raycast(Vector3(transform.position.x, transform.position.y+1, transform.position.z), transform.TransformDirection( Vector3.forward ), TargetHitBack, 15);
     var Direction : Vector3 ;
     
     Direction = ( transform.TransformDirection ( Vector3.forward ) ) ;
     
     if ( Vision.Front )
     {
         if ( Vision.Left )
         {
             Vision.VisionDirection.Right += 5 * Time.deltaTime ;
             Vision.VisionDirection.RightActive = true ;
         }else if ( Vision.Right ){
             Vision.VisionDirection.Left += 5 * Time.deltaTime ;
             Vision.VisionDirection.LeftActive = true ;
         }
         
         if ( Vision.VisionDirection.LeftActive ){
             transform.rotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation ( - ( transform.position - Vector3(Target.transform.position.x-Vision.VisionDirection.Left , Target.transform.position.y, Target.transform.position.z) ) ), 1 * Time.deltaTime ) ;
             //Vision.EaseOut = 245 ;
     
         }else if ( Vision.VisionDirection.RightActive ){ // If Turning Right
             transform.rotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation ( - ( transform.position - Vector3(Target.transform.position.x+Vision.VisionDirection.Right , Target.transform.position.y, Target.transform.position.z) ) ), 1 * Time.deltaTime ) ;
             Vision.EaseOut = 2 ;
     
         }else{ // Turn Right By Default 
             transform.rotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation ( - ( transform.position - Vector3(Target.transform.position.x+Vision.VisionDirection.Right , Target.transform.position.y, Target.transform.position.z) ) ), 1 * Time.deltaTime ) ;
             Vision.EaseOut = 2 ;
     
         }
     }else{
         
         transform.position += Direction * Time.deltaTime ; 
         if ( Vision.Right == false && Vision.Left == false ){
             if ( Mathf.Ceil ( Vision.EaseOut ) == 0  ){
                 Vision.VisionDirection.LeftActive = false ;
                 Vision.VisionDirection.RightActive = false ;
             }else{
                 Vision.EaseOut -= 1 * Time.deltaTime ;
             }
         }
         
         if ( !Vision.VisionDirection.RightActive && !Vision.VisionDirection.LeftActive ){
             
             ///transform.rotation = Quaternion.Slerp ( transform.rotation, Quaternion.LookRotation ( - ( transform.position - Vector3(Target.transform.position.x , Target.transform.position.y, Target.transform.position.z) ) ), 1 * Time.deltaTime ) ;
         }
         
         if ( Vision.Right )
         {
             Vision.EaseOut = 2 ;
         }else if ( Vision.Left ){
             Vision.EaseOut = 2 ;
         }
     }
 }
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

12 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Movement around a huge object by avoiding obstacles 1 Answer

A* Pathfinding Bug: Dictionary Key Not Found Exception 1 Answer

Make player not be seen by AI, when player in foilage and shadows. 1 Answer

C# Script Not Working? (AdvancedAI) 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