- Home /
Avoidance obstacle C#
Hi guys, I´m doing a script in C# based on this video: http://vimeo.com/9304844 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;
}
Answer by digpex · Dec 04, 2014 at 08:27 AM
Hey try to tag all the gameObjects you want to avoid one name So that in your condition if statements you can say
//this can make you detect all the gameObjects you want to avoid by tagging them
if (hit.transform.gameObject.tag == "the name you tag objects") {
if (hit.transform != transform)
{
Debug.DrawLine(transform.position, hit.point, Color.blue);
dir += hit.normal * 100;
}
}
// below is stuff to check if stuff moving on the wrong directions
Also try to check your rigidbody and freeze all the rotations as it can make object fly or act wired when collide. and if that is not the case check your local and Global transform of your objects because if your player's moving forward is not on the X or Z base on its local transform position it will go up. you can use -transform.forward to make it go the opposite direction as you want.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
need some help with basic pathfinding with obstacle avoidance 0 Answers
Get AI behavior to avoid enclosure walls? 1 Answer
How do i get the IsPathPossible() function to ignore some nodes using Astar Pathfinding Project 0 Answers