- Home /
Reacting to terrain height changes without a rigidbody component
I am attempting to make a vehicle react to changes in terrain height (my terrain is a combination of mesh colliders) without using a rigidbody component. I don't want to use a rigidbody because the vehicle does not need gravity or to be modelled with forces as I want it to be as bare-bones as possible for efficiency. In addition, it does not need to have perfect collision. The vehicle simply needs to follow a path and react to the terrain. The vehicle must never leave the ground and the terrain should only affect the vehicles y-position, its x-rotation and z-rotation. Without any method, my vehicle will simply follow the path at y = 0 and with no x or z rotation.
I initially attempted this using 4 Raycasts around the vehicle, returning the height of the terrain at each. From this I calculate the mean-y-position and the x and z-rotation. I have represented this in the image where the red lines are the Raycasts, the brown is the terrain and the blue is the triangle used for calculating the angles. The hypotenuse is the known width/length of the vehicle and the opposite is the height difference between the two ray collisions. This method has mostly worked but seems to have some unexpected issues.
First I would like to know whether to avoid this method, as I know raycasts are expensive. If it is something I should avoid then Is there a different method I can use for this type of behaviour without using a rigidbody component or using a static collider. If it is a valid method then I will further explain my issues and post my code as I would love to know what I have done wrong. Thanks.