- Home /
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:
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 :
Just in case - I have also uploaded here.
I'm not sure I understand you entirely. Can you provide a more defined scenario detailing what the target/player is avoiding/colliding with.
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.
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.
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.
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.
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 ;
}
}
}
Your answer
Follow this Question
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