- Home /
Problem with using Ray Casting to avoid obstacles and walls..
Hi
I'm working on an AI demo showing steering behaviours and I've managed to get the behaviours working. The only issue I'm having is with collision and wall avoidance. I'm trying to use ray casting to detect if there is an obstacle in front of the ai (chicken) and if there is, move away from it but to no avail.
Here's the ray casting code:
void avoidObstacles(GameObject gob)
{
Vector3 ahead = gob.transform.TransformDirection (Vector3.forward);
RaycastHit hit;
ahead = ahead.normalized;
if(Physics.Raycast(gob.transform.position, ahead, out hit))
{
Debug.DrawRay(gob.transform.position, ahead, Color.red);
Evade(gob, hit.transform.gameObject);
}
}
which I'm running in the update function:
void Update () {
//for every chicken follower, change their behaviour
foreach(GameObject gob in chickens)
{
avoidObstacles(gob);
}
...
and here is the Evade function code I'm using to move away from the obstacle ahead:
void Evade(GameObject obj, GameObject obstacle)
{
//find direction of target
Vector3 direction = obj.transform.position - obstacle.transform.position;
direction.y = 0.0f;
//calculate distance
float distance = direction.magnitude;
//if the target comes within proximity
if(distance < 2.0f)
{
//rotate away from it
obj.transform.rotation = Quaternion.Slerp(obj.transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
//move away from it
moveVector = direction.normalized * moveSpeed * Time.deltaTime;
obj.transform.position += moveVector;
}
}
so yea, unfortunately this isn't working and my chickens disregard there being any obstacles and walk straight into them. Any help in figuring out this problem would be much appreciated.
Thanks in advance!
Answer by JinJin · Mar 13, 2015 at 08:54 PM
use
Vector3 ahead = gob.transform.forward;
float rayLength = 100.0f;
if(Physics.Raycast(gob.transform.position, ahead, out hit, rayLength))
instead of
Vector3 ahead = gob.transform.TransformDirection (Vector3.forward);
if(Physics.Raycast(gob.transform.position, ahead, out hit))
and draw the ray of the direction you are trying to set. also, try setting the roation without Slerp, just to see if it works
hi jinjin thank you for your reply!.
I tried changing the Vector3 ahead like you suggested but makes no difference.
I tried to set rotation without slerp, so thought of using Quaternion.FromToRotation but this still doesn't work. :(
add this to your Evade function
Debug.DrawRay(obj.transform.position, direction, Color.blue);
pause the game and use the "one frame forward" button in the editor to see what happens frame by frame.
also, how do you update your chicken if it is not in collision?
is the moveVector updated anywhere else? is it possible that your normal update overwrites the moveVector in the next frame?
I solved the issue I was having and you are right it was the moveVector being updated to move away from another object which was affecting the movement of my chickens.
Anyway it's solved now, I appreciate your assistance jinjin :)
Your answer
Follow this Question
Related Questions
Have falling object exit from a collider after collision? 2 Answers
Determining rotation for collision avoidance? 3 Answers
AI - Raycast collision and follow player 1 Answer
Collision detection with raycast 1 Answer
Raycast for 2d with touch. 0 Answers