0
votes

I currently have a script on the Player Gameobject. I have it set up so that when I "move" the player using the WASD keys, it is to rotate my player's legs to simulate walking (I do not know how to do animations so this is the only way I know). See code below:

public float WalkingTime;  //timer for walking animation
public GameObject PlayerLeftLeg;
public GameObject PlayerRightLeg;

Code below is under the "Update" function

if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
    {
        //timer
        WalkingTime += Time.deltaTime;

        //right leg stepping forward
        if (WalkingTime > 0 && WalkingTime < .4)
        {
            PlayerRightLeg.transform.rotation = Quaternion.AngleAxis(PlayerRightLeg.transform.rotation.x - (60  * WalkingTime), Vector3.forward);
            PlayerLeftLeg.transform.rotation = Quaternion.AngleAxis(PlayerLeftLeg.transform.rotation.x + (60 * WalkingTime), Vector3.forward);
        }

        //left leg stepping forward
        if (WalkingTime > .4 && WalkingTime < 1.2)
        {
            PlayerRightLeg.transform.rotation = Quaternion.AngleAxis(PlayerRightLeg.transform.rotation.x + (60 * (WalkingTime - .8f)), Vector3.forward);
            PlayerLeftLeg.transform.rotation = Quaternion.AngleAxis(PlayerLeftLeg.transform.rotation.x - (60 * (WalkingTime - .8f)), Vector3.forward);
        }

        //right leg stepping forward
        if (WalkingTime > 1.2 && WalkingTime < 1.59)
        {
            PlayerRightLeg.transform.rotation = Quaternion.AngleAxis(PlayerRightLeg.transform.rotation.x - (60 * (WalkingTime - 1.6f)), Vector3.forward);
            PlayerLeftLeg.transform.rotation = Quaternion.AngleAxis(PlayerLeftLeg.transform.rotation.x + (60 * (WalkingTime - 1.6f)), Vector3.forward);
        }

        //resetting
        if (WalkingTime > 1.6)
        {
            PlayerRightLeg.transform.rotation = Quaternion.AngleAxis(0, Vector3.forward);
            PlayerLeftLeg.transform.rotation = Quaternion.AngleAxis(0, Vector3.forward);
            WalkingTime = 0;
        }
    }
    else
    {
        //if player not walking then reset
        PlayerRightLeg.transform.rotation = Quaternion.AngleAxis(0, Vector3.forward);
        PlayerLeftLeg.transform.rotation = Quaternion.AngleAxis(0, Vector3.forward);
        WalkingTime = 0;
    }

Right now the code works fine, except that when I use my mouse to make my player rotate or look in a different direction, so does the "Forward" for the "PlayerRightLeg" and "PlayerLeftLeg". My player still walks in the correct direction that I want but the leg movement rotates not correctly.

Is there a code I can use to replace the "Vector3.Forward" so that my player's legs always move "Forward"?

1
Transform has a property localRotationtrollingchar

1 Answers

0
votes

Never mind all, I figured it out lol

I used the code below and this utilizes the world space directions

PlayerRightLeg.transform.rotation = Quaternion.AngleAxis(PlayerRightLeg.transform.rotation.x - (60  * WalkingTime), transform.TransformVector(1,0,0));