简体   繁体   中英

3D Platformer Controller - Running in different directions

I'm trying to make a platformer game with controls like Super Mario 64 where the player can make the character run in different directions. I have almost achieved this with the current code I have but I need the camera to be able to update the character model with which direction is forward, back, etc. Right now, the character's model can change direction when running while the camera is in one position and it would look fine. But if I turn the camera around, the model will run in the desired direction, but the model will animate while facing the wrong direction. Any thoughts? I'm very new to coding and have followed a couple guides to make this.

static Animator anim;

public bool walking;

public GameObject playerModel;

//Transforms
public Transform playerCam, character, centerPoint;

//character controller declaration
CharacterController player;

//Mouse Rotation
private float rotX, rotY;

//Mouse Y Position
public float mouseYPosition = 1f;

//Mouse Sensitivity
public float Sensitivity = 10f;

//Mouse Zoom
private float zoom;
public float zoomSpeed = 2;

//Clamping Zoom
public float zoomMin = -2f;
public float zoomMax = -10f;

public float rotationSpeed = 5f;

//Move Front Back left & Right
private float moveFB, moveLR;

//Movement Speed
public float Speed = 2f;

//Velocity of Gravity
public float verticalVelocity;

//Jump Distance
public float jumpDist = 5f;

//Multiple Jumps
int jumpTimes;

// Use this for initialization
void Start () 
{
    //character controller
    player = GameObject.Find("Player").GetComponent<CharacterController> ();

    anim = GetComponent<Animator>();

    //mouse zoom
    zoom = -3;

    centerPoint.transform.position = playerCam.transform.position;
    centerPoint.transform.parent = null;

}

// Update is called once per frame 
void Update ()
{

    //Mouse Zoom Input
    zoom += Input.GetAxis ("Mouse ScrollWheel") * zoomSpeed;
    if (zoom > zoomMin)
        zoom = zoomMin;
    if (zoom < zoomMax)
        zoom = zoomMax;

    //Mouse Camera Input
    playerCam.transform.localPosition = new Vector3 (0, 0, zoom);

    //Mouse Rotation

    rotX += Input.GetAxis ("Mouse X") * Sensitivity;
    rotY -= Input.GetAxis ("Mouse Y") * Sensitivity;      

    //Clamp Camera
    rotY = Mathf.Clamp (rotY, -60f, 60f);
    playerCam.LookAt (centerPoint);
    centerPoint.localRotation = Quaternion.Euler (rotY, rotX, 0);
    character.localRotation = Quaternion.Euler(0, rotX, 0);

    //Movement Speed
    moveFB = Input.GetAxis ("Vertical") * Speed;
    moveLR = Input.GetAxis ("Horizontal") * Speed;

    //Movement Direction

    Vector3 movement = new Vector3 (moveLR, verticalVelocity, moveFB);

    //Movement Rotation
    movement = character.rotation * movement;

    player.Move (movement * Time.deltaTime);

    centerPoint.position = new Vector3 (character.position.x, character.position.y + mouseYPosition, character.position.z);

    //Movement Input
    if (Input.GetAxis ("Vertical") != 0 || Input.GetAxis ("Horizontal") != 0)   
    {           
        Quaternion turnAngle = Quaternion.LookRotation(new Vector3(moveLR, 0 ,moveFB));
        playerModel.transform.rotation = Quaternion.Slerp (playerModel.transform.rotation, turnAngle, Time.deltaTime * rotationSpeed);

            anim.SetBool("isWalking",true);

    }
    else
    {
            anim.SetBool("isWalking",false);
    }  

    if (player.isGrounded == true)  
    {
        jumpTimes = 0;
        verticalVelocity  = -Physics.gravity.y * Time.deltaTime;
    }

    else
    {
        verticalVelocity += Physics.gravity.y * Time.deltaTime;
    }

    if (jumpTimes < 1)
    {
        //Jump Input
        if (Input.GetButtonDown ("Jump"))
        {
            verticalVelocity += jumpDist;
            //print("I'm jumping.");
            anim.SetTrigger("isJumping");
            //set jumptimes +1
            jumpTimes += 1;
        }
    } 

}

I might suggest using "transform.forward" to determine the direction in which your animation is facing, and use that in movement. (It basically gives you the local +Z axis.) Rotation will change it appropriately. If you derive the movement vector without using current transform data, it's generally a recipe for wonky animations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM