x


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()
more ▼

asked May 29 '11 at 06:29 AM

superventure gravatar image

superventure
634 44 53 63

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);
}
more ▼

answered May 29 '11 at 06:34 AM

SirGive gravatar image

SirGive
1.2k 25 32 50

no, I set the x to zero to just when the character controller is not grounded (for when they jump or swim). I am aiming to get the player to rotate to the slope beneath it when grounded. How would I plug in a lerp function?

May 29 '11 at 06:38 AM superventure

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.

May 29 '11 at 06:43 AM SirGive

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 instead.

May 29 '11 at 07:10 AM superventure

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.

May 29 '11 at 07:13 AM SirGive

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?

May 29 '11 at 07:35 AM superventure
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3446
x2158
x262
x41

asked: May 29 '11 at 06:29 AM

Seen: 784 times

Last Updated: May 29 '11 at 08:50 AM