TeHArGiS10

Untitled

Jul 26th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(Rigidbody))]
  4. public class PlayerController : MonoBehaviour {
  5.  
  6. Rigidbody rb;
  7. public Camera mainCam;
  8. public float speed = 5f;
  9. public float sensitivity = 3f;
  10.  
  11. void Start ()
  12. {
  13. rb = GetComponent<Rigidbody>();
  14. }
  15.  
  16. void Update ()
  17. {
  18. //Movement
  19. float xMov = Input.GetAxisRaw("Horizontal");
  20. float zMov = Input.GetAxisRaw("Vertical");
  21.  
  22. Vector3 movHorizontal = transform.right * xMov;
  23. Vector3 movVertical = transform.forward * zMov;
  24.  
  25. Vector3 velocity = (movHorizontal + movVertical).normalized * speed;
  26.  
  27. if(velocity != Vector3.zero)
  28. {
  29. rb.MovePosition(rb.position + velocity * Time.deltaTime);
  30. }
  31.  
  32. //Camera
  33. float yRot = Input.GetAxisRaw("Mouse X");
  34. float xRot = Input.GetAxisRaw("Mouse Y");
  35.  
  36. Vector3 yRotation = new Vector3(0, yRot, 0) * sensitivity;
  37. Vector3 xRotation = new Vector3(xRot, 0, 0) * sensitivity;
  38.  
  39. rb.MoveRotation(rb.rotation * Quaternion.Euler (yRotation));
  40. mainCam.transform.Rotate(-xRotation);
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment