- Home /
Smoothly rotate to ground?
I am creating a snowboarding game, My character is going to have to match the slope so it doesn't go off into the air randomly. I have created the script to achieve this, but as the slope changes, it snaps to the rotation instead of smoothly transitioning. I have tried finding anything about it, Unfortunately they all use C# and I am after unity java. I have tried Lerping and RotateTowards but I havn't had any luck.
var hit : RaycastHit;
if (Physics.Raycast(transform.position, Vector3.down, hit)) {
transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
}
Answer by toddisarockstar · Apr 11, 2019 at 07:32 AM
sorry i don't have time to test. transform up and the hit.normal are in a euler rotation format. euler angles represent rotations in 3 numbers instead of 4 like a quaterton. so you would assign your result to transform.eularangles or cast it. your comparison is between the two "up" rotations. and the math will result in a an up rotation. if your character ends up looking at the sky after running this just add 90 to the x axis right before you assign your result back to the character. on a side note, over the last couple years seems no one talks about or seems to be using unity's javascript anymore. C# seems to be the popular language for unity and everything else. it's not really javascript. it's more commonly called Unityscript. because the type of coding only works with unity. i would recommend learning c# just cause it is compatible with everything else and everyone knows it. also, as you get into more advanced programming you have access to mr bill gate's fun librarys.
var dir : Vector3 = Vector3.RotateTowards(transform.up,hit.normal, speed*time.deltatime, 0);
// you may have to add 90 to the x in dir here if your character
// is flipping over
transform.eulerAngles = dir;
Hi there, Thank you for the reply. I have added to the script since I posted and tried to apply your script to $$anonymous$$e with no luck, $$anonymous$$aybe I am putting it in the wrong spot. Please assist me , here is the current code ( working )
if(Grounded == true){
var hit : RaycastHit;
if (Physics.Raycast(transform.position, Vector3.down, hit)) {
var distanceToGround = hit.distance;
transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
Snow$$anonymous$$esh.transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
distToGround = distanceToGround;
if( Snow$$anonymous$$esh.transform.rotation != Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation){
print("crash");
}
}
}