2
public class Movement : MonoBehaviour { public float maxSpeed = 6.0f; public float jumpSpeed = 8.0f; private Vector3 moveDirection = Vector3.zero; public float gravity = 20.0f; public GameObject mainCam; // Update is called once per frame void Update () { //assign Character Controller to a variable CharacterController controller = GetComponent<Charac terController>(); //Set player y rotation to the Camera transform.rotation = Quaternion.Euler (0,mainCam.Get Component<MouseLook>().curYRotation,0); if(controller.isGrounded) { //variable that hold players move Direction moveDirection = new Vector3(Input.GetAxis("Horizonta l"),0,Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDir ection); moveDirection *= maxSpeed; //press space bar to jump if(Input.GetButton("Jump")) moveDirection.y = jumpSpeed; } //apply gravity moveDirection.y -= gravity * Time.deltaTime; //move playe

Simple_Movement_Class

Embed Size (px)

Citation preview

Page 1: Simple_Movement_Class

public class Movement : MonoBehaviour {

    public float maxSpeed = 6.0f;    public float jumpSpeed = 8.0f;    private Vector3 moveDirection = Vector3.zero;    public float gravity = 20.0f;    public GameObject mainCam;        // Update is called once per frame    void Update () {                //assign Character Controller to a variable        CharacterController controller = GetComponent<CharacterController>();

        //Set player y rotation to the Camera        transform.rotation = Quaternion.Euler (0,mainCam.GetComponent<MouseLook>().curYRotation,0);

        if(controller.isGrounded)    {        //variable that hold players move Direction        moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));        moveDirection = transform.TransformDirection(moveDirection);        moveDirection *= maxSpeed;

            //press space bar to jump        if(Input.GetButton("Jump"))                moveDirection.y = jumpSpeed;

    }        //apply gravity        moveDirection.y -= gravity * Time.deltaTime;

        //move playe        controller.Move(moveDirection * Time.deltaTime);    }}