Guest User

Untitled

a guest
Jul 28th, 2017
142
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class MovementScript : MonoBehaviour {
  2.     public float speed = 5;
  3.     public float CameraSpeed = 3;
  4.     public GameObject mainCamera;
  5.     private Rigidbody rigidBody;
  6.     private Vector3 movingVectorVertical;
  7.     private Vector3 movingVectorHorizintal;
  8.     void Start () {
  9.         rigidBody = GetComponent<Rigidbody>();
  10.     }
  11.     // Update is called once per frame
  12.     private void Update()
  13.     {
  14.         if (Input.GetKey("up") || Input.GetKey("w"))
  15.         {
  16.             movingVectorVertical = transform.forward * speed;
  17.         }
  18.         else if (Input.GetKey("down") || Input.GetKey("s"))
  19.         {
  20.             movingVectorVertical = -transform.forward * speed;
  21.         }
  22.         if (Input.GetKey("right") || Input.GetKey("d"))
  23.         {
  24.             movingVectorHorizintal = -transform.right * speed;
  25.         }
  26.         else if (Input.GetKey("left") || Input.GetKey("a"))
  27.         {
  28.             movingVectorHorizintal = transform.right * speed;
  29.         }
  30.         if (!(Input.GetKey("up") || Input.GetKey("w")) && !(Input.GetKey("down") || Input.GetKey("s")))
  31.         {
  32.             movingVectorVertical = Vector3.zero;
  33.         }
  34.         if (!(Input.GetKey("right") || Input.GetKey("d")) && !(Input.GetKey("left") || Input.GetKey("a")))
  35.         {
  36.             movingVectorHorizintal = Vector3.zero;
  37.         }
  38.         transform.Rotate(0, Input.GetAxis("Mouse X") * CameraSpeed, 0);
  39.     }
  40.     private void FixedUpdate()
  41.     {
  42.         var moveVec = movingVectorVertical + movingVectorHorizintal;
  43.         rigidBody.velocity = new Vector3(moveVec.z, 0, moveVec.x);
  44.         rigidBody.rotation = 
  45.             Quaternion.Euler(rigidBody.rotation.eulerAngles + new Vector3(0f, Input.GetAxis("Mouse X") * CameraSpeed, 0f));
  46.         mainCamera.transform.rotation =
  47.             Quaternion.Euler(mainCamera.transform.rotation.eulerAngles + new Vector3( -CameraSpeed * Input.GetAxis("Mouse Y"), 0f, 0f));
  48.     }
  49. }
RAW Paste Data