- Home /
Question by
mikstar890 · Apr 16, 2015 at 12:47 PM ·
torque
using torque to alighn an object with surface
Im attempting to create a whipeout like game where i have a hovering vehicle 'sticking' to the road surface.
Currently i have a ray going down from the vehicle to get the normal of the surface its supposed to align with, and i want to use AddTorque so its smooth with later physics.
The issue im having is getting a relative rotation that i can add as torque.
current code works, until i start going on steeper slopes, tho only on near vertical surfaces, at which point is starts turning it in the relative y direction. Works fine upside down tho.
void alighnWithRoad(RaycastHit hit)
{
float xDelta = ((Mathf.Atan2(trans.up.y,trans.up.z)) - (Mathf.Atan2(hit.normal.y,hit.normal.z)))* Mathf.Rad2Deg ;
float zDelta = ((Mathf.Atan2(hit.normal.y,hit.normal.x)) - (Mathf.Atan2(trans.up.y,trans.up.x)))* Mathf.Rad2Deg ;
if(xDelta > 180){
xDelta -= 360;
}
else if(xDelta < -180){
xDelta += 360;
}
if(zDelta > 180){
zDelta -= 360;
}
else if(zDelta < -180){
zDelta += 360;
}
rigBody.AddTorque(new Vector3(xDelta,0,zDelta) * 6f,ForceMode.Acceleration);
}
Comment