I wan’t my vehicle to automatically steer so that the player “only” has to accelerate. It turns out that this seems a bit harder than I initially thought.
Right now I have a Path defined by List<Vector3>
, I get the normal of the lookAhead to the path (that’s what ``NearestPointOnLine()
does). And then I rotate the vehicle accordingly and give it a relative Force forward.
I’m probably on the wrong track, so any hints or solutions in the right direction are highly appreciated.
This is what it currently looks like:
This is what I have:
void Update() {
FollowPath();
if(actions.Accelerate.IsPressed){
body.AddRelativeForce(Vector3.forward * acceleration * Time.deltaTime);
} else {
Vector3 vel = body.velocity;
body.velocity = Vector3.zero;
body.AddRelativeForce(Vector3.forward * vel.magnitude * Time.deltaTime);
}
body.velocity = Vector3.ClampMagnitude(body.velocity, maxSpeed);
}
void FollowPath() {
// predict future location
prediction = body.velocity;
prediction.Normalize();
prediction *= lookAhead;
prediction += transform.position;
normal = NearestPointOnLine(aimAt.waypoints[0], aimAt.waypoints[1]-aimAt.waypoints[0], prediction);
float distance = Vector3.Distance(prediction, normal);
if(distance > aimAt.radius) {
SteerAtPath();
} else {
Debug.Log("I'm on track");
}
}
void SteerAtPath() {
NearestNormal();
Transform desired = transform;
desired.LookAt(normal);
body.MoveRotation(Quaternion.RotateTowards(transform.rotation, desired.rotation, 10f * Time.deltaTime));
}