- Home /
Steering while following terrain surface normal
I have a camera following a vehicle (third person perspective) and i added raycasting to make it follow the terrains height, but also its lean (normal).
While rotating according to the normal my steering is ignored as the terrain rotation overwrites it. I can't really seems to wrap my head around the 3D math right now, so can anyone tell my how i can incorporate the steering into the leaning?
I know this is very simple and creates a quite jittery ride while following the terrain, but anyhow:
void Update() {
float horizontal = Input.GetAxis("Horizontal") * turningSpeed * Time.deltaTime;
transform.Rotate(0, horizontal, 0);
float vertical = Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime;
transform.Translate(0, 0, vertical);
RaycastHit hit = RayToTerrain(new Vector3(transform.position.x, rayOriginY, transform.position.z));
transform.position = new Vector3(transform.position.x, hit.point.y, transform.position.z);
transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
}
RayToTerrain casts a ray downwards and finds the terrain hit.
Comment