Hi guys,
I´m doing a script in C# based on this video: Obstacle Avoidance - Unity 3d on Vimeo
The problem is that my object doesn´t avoid all objects, and when collide, it goes up.
Here´s my code:
{
Vector3 dir = (casaContador.position- transform.position).normalized;
RaycastHit hit = new RaycastHit();
if(Physics.Raycast(transform.position, transform.forward, out hit, 20))
{
if (hit.transform != transform)
{Debug.DrawLine(transform.position, hit.point, Color.blue);
dir += hit.normal * 100;
}
}
Vector3 leftR = transform.position;
Vector3 rightR = transform.position;
leftR.x -= 5;
rightR.x += 5;
if(Physics.Raycast(leftR, transform.forward, out hit, 20))
{
if (hit.transform != transform)
{Debug.DrawLine(leftR, hit.point, Color.red);
dir += hit.normal * 100;
}
}
if(Physics.Raycast(rightR, transform.forward, out hit, 20))
{
if (hit.transform != transform)
{Debug.DrawLine(rightR, hit.point, Color.yellow);
dir += hit.normal * 50;
}
}
Quaternion rot = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime);
transform.position += transform.forward*speed*Time.deltaTime;
}