Need help with making my character rotate.

Hi i have been working on making my character turn the way the character is going for my top down game. I know i'm missing something very obviuos that will make it alot easier and make the script work to begin with but i'm still quite new to scripting and i cant figure it out. What i'm trying to do in my script at the moment is just rotating the graphics to not mess up the controller. This is what i got right now, and it's not working as you can most likely see.

private var xKey : float;
private var zKey : float;
private var diagonalSpeed : float = 1;
var speed : float = 10.0;

var Stunned : boolean = false;
var Sprinting : boolean = false;

var playerParts : GameObject;
var lastDirection : int = 1;

function Update () {

    if(!Stunned){
    zKey = (Input.GetAxis("Vertical") * speed);
    xKey = (Input.GetAxis("Horizontal") * speed );
    }

    if((xKey != 0) && (zKey != 0)) {diagonalSpeed = 0.75;} else {diagonalSpeed = 1;}
        rigidbody.velocity = Vector3(xKey * diagonalSpeed,0,zKey * diagonalSpeed);

    if(Input.GetKey("left shift") && (!Stunned))
    {
        speed = 15;
        Sprinting = true;
    }
    else
    {
        speed = 10;
        Sprinting = false;
    }

    if(Input.GetKey("a") && (lastDirection != 3))
    {
        lastDirection = 3;
        playerParts.transform.Rotate(0, -90, 0);
    }

    if(Input.GetKey("d") && (lastDirection != 4))
    {
        lastDirection = 4;
        playerParts.transform.Rotate(0, 180, 0);
    }
}

Any suggestions how i can do this would be cool.

Still having trouble with this? Just wondering because it's been a while since you've posted.

1 Answer

1

var rotSpeed : float = 6;

function Update ()
{
	if (Input.GetAxis("Vertical") > 0)
		transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), rotSpeed * Time.deltaTime);
	
	if (Input.GetAxis("Vertical") < 0)
		transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.back), rotSpeed * Time.deltaTime);
	
	if (Input.GetAxis("Horizontal") > 0)
		transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.right), rotSpeed * Time.deltaTime);
	
	if (Input.GetAxis("Horizontal") < 0)
		transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.left), rotSpeed * Time.deltaTime);
}

May not be the BEST way to do it but it’ll give you the general idea.

What this is doing is finding out which directional key is being pressed then using Slerp to rotate from a rotation to another rotation. In this case our to rotation is the Vector we want to face. But since Slerp takes two Quaternions and LookRotation takes a Vector3 it’s a great place to place LookRotation there. Instead of specifying a rotation we specify a direction. Then smooth it out with a rotation speed.

This is a basic way to do it but it should get you started. I wouldn’t recommend you just take this and use it. Build on this. Good luck.