- Home /
Smooth and restrict rotations?
I'm Stuck!! How do I correctly lock the z axis so it cannot rotate, and how can I get the rotations to be smoother (less sensitive)? Thanks a bunch for any help!
var howMuch = .1;
var speed = 8;
private var yRotationOffset : float = 0;
function Update () {
var controller : CharacterController = GetComponent(CharacterController);
var hit : RaycastHit;
var castPos = Vector3(transform.position.x,transform.position.y- howMuch,transform.position.z);
if (controller.isGrounded)
{
if (Physics.Raycast (castPos, -transform.up, hit)) {
transform.up = hit.normal; //direction
transform.Rotate (0, yRotationOffset, 0); //Apply y-rotation offset.
}else{
// If we're not aligning the player, we can apply the rotation directly
transform.Rotate (0, Input.GetAxis ("Horizontal"), 0);
}
yRotationOffset += Input.GetAxis ("Horizontal") * speed;
if (!controller.isGrounded){
transform.rotation.z = 0;
transform.rotation.x =0;
}
}//if (controller.isGrounded)
}//update()
Answer by SirGive · May 29, 2011 at 06:34 AM
You can try using the "lerp" function to get smoother rotations, and setting rotation.x to zero fixes the rotation problem right? Check here for a reference on all kinds of "lerp" functions.
Here is how you would "lerp" angles:
var minAngle = 0.0;
var maxAngle = 90.0;
function Update () {
//Time.time is one second. Change that to any time you want
var angle : float = Mathf.LerpAngle(minAngle, maxAngle, Time.time);
transform.eulerAngles = Vector3(0, angle, 0);
}
And here is how you would "lerp" a vector3:
var objPos : transform.position;
var endPos : Vector3(0,0,0);
function Update () {
//Time.time is one second. Change that to any time you want
transform.position = Vector3.Lerp(objPos, endPos, Time.time);
}
no, I set the x to zero to just when the character controller is not grounded (for when they jump or swim). I am ai$$anonymous$$g to get the player to rotate to the slope beneath it when grounded. How would I plug in a lerp function?
lerp functions basically work like this: the move from one position to the next and how fast. It all depends on what you want to lerp, though. For instance, Vector3.Lerp(fromPositionOne, toPositionTwo, timeToLerpInSeconds) is how you would lerp a vector. Check the link for a bunch of different types of lerps. Also, if you want there to be no rotation then set the rotation dimension to be zero.
I tried plugging transform.rotation.z =0; into the script when it is grounded, but it made things worse by upsetting all the rotations, so that is why I need help there. Is your example for locking with a new variable in C#? I only know java script. Also, I meant how I would alter transform.Rotate to lerp ins$$anonymous$$d.
yeah. for JS just don't use "new"
And for lerping, just use mathf.Rotate(firstAngle, secondAngle, speed) for the angle you want to rotate.
Okay, I'm sorry but I keep getting lost- I don't know how to set up either Vector3(var.x, var.y, 0) or Quaternion(var.x, 0, 0, var2) correctly. I keep getting error messages. var.x? I've never seen a var set like that. Also, how would I define either the first angle or the next angle I need to lerp to?