简体   繁体   中英

Unity C# First Person Script not Moving

I'm pretty new to unity and I decided to make a simple first person game, however my script only allows the player to look around, not move. I have used AddForce, however I am not sure I have done it correctly.

public float walkSpeedForward = 5f;
public float walkSpeedStrafe = 4f;
public float walkSpeedBack = 3f;
public float sprintMultiplier = 2f;
public Camera cam;
public float sensitivityX = 0f;
public float sensitivityY = 0f;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
public Rigidbody rb;
float rotationY = 0f;


// Use this for initialization
void Awake () {
    rb = GetComponent<Rigidbody>();
    rb.freezeRotation = true;
}

// Update is called once per frame
void Update () {
    float rotationX = cam.transform.localEulerAngles.y + Input.GetAxis ("Mouse X") * sensitivityX;
    rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
    rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    cam.transform.localEulerAngles = (new Vector3(-rotationY, rotationX, 0f));

}
void FixedUpdate()
{
    rb.AddForce (new Vector3 (Input.GetAxis ("Horizontal") * walkSpeedStrafe * Time.deltaTime, 0, Input.GetAxis ("Vertical") * walkSpeedForward * Time.deltaTime));
}

Do not use AddForce, it is physics pushing an object. Not actually Ideal to move a character.

You can learn how to properly make a script or even, use a ready script by clicking

Assets>Import Package> Characters. This package already have characters sounds and the Scripts that you're looking for. You can choose only the scripts if you want, but I suggest for you to import them all since you're trying to learn it. So you would know how to attach sounds, controls, models. As you learn form this package. FYI you don't need to download this, it is already on your computer.

Check this video Click Here

It will only take 12 min of your time.

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