Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class PlayerShooting : MonoBehaviour
  4. {
  5.  
  6. public float speed = 6f;
  7.  
  8. Vector3 movement;
  9. Animator anim;
  10. Rigidbody playerRigidbody;
  11. int floorMask;
  12. float camRayLength = 100f;
  13.  
  14. void Awake()
  15. {
  16. floorMask = LayerMask.GetMask("Floor");
  17. anim = GetComponent<Animator>();
  18. playerRigidbody = 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. playerRigidbody.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. playerRigidbody.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