- Home /
AI create path, help.
Ok so I gave up the navmesh/agent way of doing AI for my vehicles. Its not going to work because of many reasons not just it being too stupid, so I am making my own. My game WIP: http://forum.unity3d.com/threads/200629-Omega-Void-WIP
What I need now is for my car to reorient itself left or right in the general direction of the target... Or turn around completely(but that is not needed yet).
I got it to stop and reverse so far but need it either to pick right or left depending on the direction of the target.
Here I am using 3 rays to detect whats in front and then stop. Shaped like spread out fork or three fingers pointing out at the front.
How would I make it choose to turn left(or right) and move forward until the rays are not hitting?
Code:
target = waypointHolder.transform;
Vector3 relativeWaypointPos = transform.InverseTransformPoint( new Vector3(wayPoints[currentWaypoint].position.x, transform.position.y,wayPoints[currentWaypoint].position.z));
// Vector3 targetDir = target.position - transform.position;
targetDistance = Vector3.Distance(transform.position, target.transform.position);
Vector3 chassisLook = chassis.TransformDirection(new Vector3(0, -navSeeDistance, 0));
Vector3 left = chassis.TransformDirection(new Vector3(-navSeeLeftDist, -19, 0));
Vector3 right = chassis.TransformDirection(new Vector3(navSeeRightDist, -19, 0));
RaycastHit chit;
Debug.DrawRay(chassis.position, chassisLook, Color.white);
Debug.DrawRay(chassis.position, left, Color.white);
Debug.DrawRay(chassis.position, right, Color.white);
//Raycasting
if(Physics.Raycast(transform.position, chassisLook, out chit, navSeeDistance)){
if(chit.transform.gameObject.layer == LayerUnpassable01){
pathBlocked = true;
inputBrake = true;
inputTorque = -100.0f;
if(rigb.velocity.sqrMagnitude <= 20.0f){
inputTorque = 0.0f;
}
}
}else{
// pathBlocked = false;
}
//
//This is where the car steers and AI inputs speed/torque in order to get to target.
if(pathBlocked == false){
inputSteer = relativeWaypointPos.x / relativeWaypointPos.magnitude;
if(Mathf.Abs(inputSteer)<0.5)
inputTorque = relativeWaypointPos.z / relativeWaypointPos.magnitude - Mathf.Abs(inputSteer);
else
inputTorque = 0.0f;
suggest you just get the "Astar Project" from the asset store. Writing pathfinding or AI is simply a professional job, not a hobbyist job. Why waste months of your life? It makes no sense.
Answer by RealSoftGames · Jul 19, 2015 at 02:20 PM
no don't listen, you can accomplish this, all that matters is what your end goal is, if you are a hobbyist, it is well worth learning, for your Car AI
https://www.youtube.com/watch?v=HeQ89e27ehc&list=PL67XFC3MYQ6JV0l9485jB1Dpn4atLsFSo
here is a link for a Car AI tutorial. you can probably find many more out their, the most common path is the raycasting which i believe you have managed to figure out already, i was doing a similar thing to this but i was doing it with a Drone, a little more tricky when this sucker doenst even sit on the ground.
if you are using the Navmesh agent or not, you can accomplish this using a Rigidbody
as you know you can obtain onHit with a raycast,
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, fwd, 10))
{
//apply Angular Velocity to rigidbody, or depending on how you control your car, ifd you use the wheel physics make the the approriate wheels turn or accelerate(if you are using Tank Mechanics)
}
http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
when the ray is not hitting anything it will continue driving forward, unless you have other behaviors that do other things.
Your answer
Follow this Question
Related Questions
Car AI Avoiding Obstacle 0 Answers
Car Ai - Calculate max corner speed 1 Answer
AI question, characters escaping from light 0 Answers
Have falling object exit from a collider after collision? 2 Answers