Guest User

Movement

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