Hi,
I’m trying to rotate an object along its X axis that’s using a NavMesh component. The AI is already finished and works fine. The problem is that the NavMesh object needs to match the terrain’s angle to avoid looking unrealistic like below. The object seems to always stay at 0 degrees regardless of it being rotated.
I’m using the code below (JS) to get the angle of the front and back of the model using raycasts which then aligns the model according to the angle of the terrain. The problem is that for some reason Unity will not let me rotate an object that’s using a NavMesh component and I’m not sure why. I hope I’m mistaken and it’s something on my part.
I’ve tested this out with an object not using NavMesh and it works perfectly fine so I’m not really sure what the issue is. If anyone can point me in the right direction it would be greatly appreciated.
function FixedUpdate()
{
var hit: RaycastHit;
if (Physics.Raycast(transform.position + transform.forward, Vector3.down, hit)){
var fHit = hit.point; // get the point where the front ray hits the ground
if (Physics.Raycast(transform.position - transform.forward, Vector3.down, hit)){
var bHit = hit.point; // get the back hit point
transform.forward = fHit - bHit; // align the object to these points
}
}
}