简体   繁体   中英

Trying to create a 3rd person controller in Unity but the player is not moving

This is what I have so far.

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {

    CharacterController control;

    [SerializeField]
    float moveSpeed = 5.0f;

    [SerializeField]
    float jumpSpeed = 20.0f;

    [SerializeField]
    float gravity = 1.0f;

    float yVelocity = 0.0f;

    // Use this for initialization
    void Start () {
        control = GetComponent<CharacterController> ();
    }

    // Update is called once per frame
    void Update () 
    {
        Vector3 direction = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
        Vector3 velocity = direction * moveSpeed;

        if (control.isGrounded) {
            if (Input.GetButtonDown ("Jump")) {
                yVelocity += jumpSpeed;
            }
        } else {
            yVelocity -= gravity;
        }

        velocity.y = yVelocity;

        control.Move (velocity*Time.deltaTime);
    }
}

I'm following a tutorial, and it looks like everything is the same, but the player is not moving.

If it is all same with tutorial, your problem must be about your input settings. Check your "Horizontal", "Vertical", "Jump" from Edit -> Project Settings -> Input

Then on inspector look at variables under Axis list to check them if they assigned true, maybe there is no Horizontal or Vertical variable.

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