x


Rotate Character with movement

Right, I have a simple movement script. I haven't picked up unity in a while and for some reason it seems i have forgotten some of the basics. I want my character to face the direction he is moving. Here is the script i have for movement at the moment.

var speed : float = 6.0;

var jumpSpeed : float = 8.0;

var gravity : float = 20.0;

private var moveDirection : Vector3 = Vector3.zero;

function Update() {
    var controller : CharacterController = GetComponent(CharacterController);

    if (controller.isGrounded) {
        // We are grounded, so recalculate
        // move direction directly from axes
        moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
                                Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;

        if (Input.GetButton ("Jump")) {
            moveDirection.y = jumpSpeed;
        }
    }

    // Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;

    // Move the controller
    controller.Move(moveDirection * Time.deltaTime);
}
more ▼

asked Oct 09 '11 at 10:16 AM

TheFrankman123 gravatar image

TheFrankman123
162 25 35 37

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

3 answers: sort voted first

Like this-

if(moveDirection.magnitude > 0.05f)
{
    transform.LookAt(transform.position + moveDirection);
}

I mean, that's just one way- you can do it any way really.

more ▼

answered Oct 09 '11 at 10:21 AM

syclamoth gravatar image

syclamoth
15k 7 15 80

Oh yeah- you have to do it before any of the jump stuff gets added.

Oct 09 '11 at 10:22 AM syclamoth
(comments are locked)
10|3000 characters needed characters left

Thank you!

more ▼

answered Oct 12 '11 at 03:19 AM

TheFrankman123 gravatar image

TheFrankman123
162 25 35 37

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

Turns out it's just easier to use the third person controller script in the standard assets.

more ▼

answered Oct 12 '11 at 11:55 PM

TheFrankman123 gravatar image

TheFrankman123
162 25 35 37

Hi,

I am having same kind of trouble, but instead of moving all direction , my character is restricted to move in x-direction only -

  moveDirection = new Vector3(0,0,Input.GetAxis("Horizontal"));

basically what i want is to move my character in 2d platform.

Aug 03 '12 at 05:26 AM uno
(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:

x1417
x1079
x746

asked: Oct 09 '11 at 10:16 AM

Seen: 1469 times

Last Updated: Aug 03 '12 at 05:26 AM