123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- using UnityEngine;
- public class PlayerMovement : MonoBehaviour
- {
- public static PlayerMovement instance;
- public float speed = 0;
- Vector3 movement;
- Animator anim;
- Rigidbody playerRigidbody;
- int floorMask;
- float camRayLength = 100f;
- public Vector3 GetMousePoint { get; set; }
- void Awake()
- {
- floorMask = LayerMask.GetMask("Floor");
- anim = GetComponent<Animator>();
- playerRigidbody = GetComponent<Rigidbody>();
- instance = this;
- }
- void FixedUpdate()
- {
- float h = Input.GetAxis("Horizontal");
- float v = Input.GetAxis("Vertical");
- Move(h, v);
- Turning();
- Animating(h, v);
- }
- void Move(float h,float v)
- {
- movement.Set(h, 0, v);
- movement = movement.normalized * speed * Time.deltaTime;
- playerRigidbody.MovePosition(transform.position + movement);
- }
- void Turning()
- {
- Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit floorHit;
- if(Physics.Raycast(camRay,out floorHit, camRayLength, floorMask))
- {
- Vector3 playerToMouse = floorHit.point - transform.position;
- GetMousePoint = playerToMouse;
- playerToMouse.y = 0f;
- Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
- playerRigidbody.MoveRotation(newRotation);
- }
- Debug.DrawRay(Camera.main.transform.position, floorHit.point, Color.red);
- Debug.DrawLine(Camera.main.transform.position, floorHit.point, Color.red);
- }
- void Animating(float h,float v)
- {
- bool walking = h != 0f || v != 0f;
- anim.SetBool("IsWalking", walking);
- }
- }
|