Advertisement
Guest User

YouCantType

a guest
Sep 2nd, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class PlayerMovement : MonoBehaviour
  4. {
  5. public float speed = 6f;
  6.  
  7. private Vector3 movement;
  8. private Animator anim;
  9. private Rigidbody playerRidiRigidbody;
  10. private int floorMask;
  11. private float camRayLength = 100f;
  12.  
  13.  
  14. void Awake()
  15. {
  16. floorMask = LayerMask.GetMask("Floor");
  17. anim = GetComponent<Animator>();
  18. playerRidiRigidbody = GetComponent<Rigidbody>();
  19. }
  20.  
  21. void FixedUpdate()
  22. {
  23. float h = Input.GetAxisRaw("Horizontal");
  24. float v = Input.GetAxisRaw("Vertical");
  25.  
  26. Move(h, v);
  27. Turning();
  28. Animating(h, v);
  29. }
  30.  
  31. void Move(float h, float v)
  32. {
  33. movement.Set(h, 0f, v);
  34.  
  35. movement = movement.normalized * speed * Time.deltaTime;
  36.  
  37. playerRidiRigidbody.MovePosition(transform.position + movement);
  38. }
  39.  
  40. void Turning()
  41. {
  42. Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
  43.  
  44. RaycastHit floorHit;
  45.  
  46. if (Physics.Raycast(camRay, out floorHit, camRayLength, floorMask))
  47. {
  48. Vector3 playerToMouse = floorHit.point - transform.position;
  49. playerToMouse.y = 0f;
  50.  
  51. Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
  52. playerRidiRigidbody.MoveRotation(newRotation);
  53. }
  54. }
  55.  
  56. void Animating(float h, float v)
  57. {
  58. bool walking = h != 0f || v != 0f;
  59. anim.SetBool("IsWalking", walking);
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement