I felt this was a better place to ask this, rather than the forums. Ok, so I have a car that I can move around and I need it to align itself to the ground normals for a Mario Kart/Little Big Planet Kart type of physics. The script I have has some problems. Whenever it “aligns” me to the ground normals it sets my Y rotation to the specific rotation every time. It also doesn’t align me often enough. It usually only works when I go up and down slopes. It is in the Update, so that’s not the problem.
Here is my script so far:
var hit : RaycastHit;
var castRot = Vector3(transform.position.x,transform.position.y-.50,transform.position.z);
if (Physics.Raycast (castRot, -transform.up, hit)) {
transform.rotation = Quaternion.FromToRotation (Vector3.up, hit.normal);
}
Does anybody have an idea of what is wrong?
You could also try setting transform.up = hit.normal. What you don't want to do is set the rotation to just that FromTo as @robertbu says - you would want to modify the existing rotation. transform.up = hit.normal does try to do that.
– whydoidoitOk, is this right? var hit : RaycastHit; var castRot = Vector3(transform.position.x,transform.position.y-.50,transform.position.z); if (Physics.Raycast (castRot, -transform.up, hit)) { prevNormal = hit.normal; Quaternion q = Quaternion.FromToRotation(prevNormal, hit.normal); transform.rotation = q * transform.rotation; It says ';' is expected on the line, "Quaternion q = Quaternion.FromToRotation(prevNormal, hit.normal); ". Can you not declare Quaternion variables like this?
– Baskyn