I have a TANK with a Turret on top, the turret follows my mouse around the screen and it rotates perfectly. And the turret never clips into the tank thanks to x and z being zerod out. When my tank is climbing a hill though the turret goes right through the tank. I think it has something to do with local angles or something but I can’t figure it out.
SEE IT HERE - http://dl.dropbox.com/u/24020723/GAME/TANK.html Take the tank onto a hill and see what happens to turret
Thanks!
Plane zeroPlane = new Plane( Vector3.up, Vector3.zero );
Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
float distance;
if( zeroPlane.Raycast( ray, out distance ) )
{
Vector3 outputPosition = ray.origin + ray.direction* distance;
var newRotation = Quaternion.LookRotation(outputPosition-turretRotate.position).eulerAngles;
newRotation.x = 0;
newRotation.z = 0;
turretRotate.rotation = Quaternion.Euler(newRotation);
}