I have some code that's working well for steering my vehicle towards a waypoint. It orients the vehicle in the direction of its travel and all is good except for one problem: Hills.
I need my vehicle to orient itself in the direction of its travel, but also rotated so its down is aligned with the ground.
Here's my code. I'm not at all sure what to put in the raycast to orient myself to the normal without breaking the rotation that points in the direction I'm going:
Vector3 targetVector = predictivePoint - transform.position;
targetVector.Normalize();
steerVector = Vector3.Slerp(steerVector, targetVector, turnSpeed);
transform.LookAt(transform.position + steerVector);
transform.Translate(steerVector * speed, Space.World);
if (transform.position.y != ground) {
RaycastHit hit;
Vector3 origin = transform.position;
origin.y += 1;
if (Physics.Raycast(origin, -transform.up, out hit, 5f, groundLayer)) {
origin.y = hit.point.y;
transform.position = origin;
}
}
World of Warcraft uses the "orient to normal" trick for non-bipeds, which gives them that excessively jerky tilt (pets near a wall flip on their sides.) Instead, maybe check the ground near front and back and orient to that slope.
– Owen-ReynoldsThat requires two raycasts firing really often though. Aren't raycasts expensive? As long as I smooth the orientation it shouldn't be jerky.
– anon23814272Raycasts are not that expensive. You can even limit them to be cast only against certain objects or layers to further optimize them.
– StephanK