- Home /
AI Detecting Object (Raycast)
Hi again!
I'm still working on trying to get my AI to get around object around the map without running through them. What I need is for my AI to send out rays in the 180 degree radius in front of him. And if one of those rays hit an object that is say 50 units away from him...He will rotate little by little until that ray isn't hitting anything again. This would be very helpful for my AI when he is chasing my player around and my player runs behide a wall. Also when the AI is just wondering around freely...What I have so far is one ray sent out in front of the AI that picks up objects up to 50 units away...I don't know where to go from there on rotating the AI or sending out more then just one ray...The script is below and if anyone needs any other script to help salve this problem just ask and I would be happy to get it to them...Any help would be greatly appreshated!!! Thanks!!!
function Update () {
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (transform.position, fwd, 50)) {
print ("There is something in front of the object!");
}
}
there is some information on this link that you may find useful :
http://answers.unity3d.com/questions/235017/enemy-ai-avoiding-collision.html
I saw this today as I was searching for this answer!!! It seems very useful be the coding looks really forign to me...any ideas? Thanks
Ok I got almost all of it after looking back at the code u posted...Thanks!!! Now all I need is for the AI to dectect which path is the shortes. Like if there is a wall and hes to the very right it of it...go right...not left...Any ideas for that? Thanks!!!
sorry i havn't replied, been busy. Also I am not very good with rotations, but here is a video with some more information that might help. Even if you follow the video, write the script , and see how this works. Obstacle Avoidance - Unity 3d link : http://vimeo.com/9304844
Here is the script to help you along (I have changed it when I was testing my own ideas , but it should be almost the same. I tested This script and it works. Just set your target vector.) :
// Avoid Obstacle Script
// ---------------------
var speed : float = 10.0;
var target : Vector3 = Vector3(0, 0, 0);
private var dir : Vector3;
private var dirFull : Vector3;
// ----
function FixedUpdate()
{
// -- Obstacle Avoidance Tutorial --
// the directional vector to the target
dir = (target - transform.position).normalized;
var hit : RaycastHit;
// check for forward raycast
if (Physics.Raycast(transform.position, transform.forward, hit, 1)) // 20 is raycast distance
{
if (hit.transform != this.transform)
{
Debug.DrawLine (transform.position, hit.point, Color.white);
dir += hit.normal * 20; // 20 is force to repel by
}
}
// more raycasts
var leftRay = transform.position + Vector3(-0.125, 0, 0);
var rightRay = transform.position + Vector3(0.125, 0, 0);
// check for leftRay raycast
if (Physics.Raycast(leftRay, transform.forward, hit, 1)) // 20 is raycast distance
{
if (hit.transform != this.transform)
{
Debug.DrawLine (leftRay, hit.point, Color.red);
dir += hit.normal * 20; // 20 is force to repel by
}
}
// check for rightRay raycast
if (Physics.Raycast(rightRay, transform.forward, hit, 1)) // 20 is raycast distance
{
if (hit.transform != this.transform)
{
Debug.DrawLine (rightRay, hit.point, Color.green);
dir += hit.normal * 20; // 20 is force to repel by
}
}
// --
// $$anonymous$$ovement
// rotation
var rot = Quaternion.LookRotation (dir);
//print ("rot : " + rot);
transform.rotation = Quaternion.Slerp (transform.rotation, rot, Time.deltaTime);
//position
transform.position += transform.forward * (2 * Time.deltaTime); // 20 is speed
// -- end tutorial --
}
This works by getting the .normal of the surface the Raycast hit, and modifying the (dir) , then the gameObject looks at the position (dir), then the gameObject moves forward in the rotation it is facing. I think ... =]
Answer by AlucardJay · Apr 13, 2012 at 07:22 PM
See My Comments =]
YES!!! I got the AI finily to dodge objects!!! Thank you for all your help!!! One last question to finish off the AI...I can't get the AI to ignore the player for raycasting as far as trying to avoid it. The Layer thing dosn't seem to like me...any ideas? THAN$$anonymous$$S! O and also...where should I add the script were the AI avoids in corners? THAN$$anonymous$$S!!!
Answer by yezzer · Apr 12, 2012 at 05:42 PM
Have you considered using either UnitySteer or if you have Pro, using the built-in pathfinding in 3.5?
Yes. I have tried UnitySteer and I do not have pro...UnitySteer isn't really what I Need and its way to compicated...Thanks though!!!
Answer by MyCatFishSteve · Jan 29, 2013 at 09:53 PM
i wouldnt set the Raycast to 180 degrees, otherwise it might be hard to get through small gaps
Answer by sjmurray · Feb 21, 2013 at 06:26 PM
i think the suggested code above will also have problems with small gaps and also if the enemy and the player create a line that is exactly perpendicular to a wall (the enemy will actually run straight away from you). Good for small objects though.