반응형
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | 2-2 PlayerMovement using UnityEngine; public class PlayerMovement : MonoBehaviour { public float speed = 6f; Vector3 movement; Animator anim; Rigidbody playerRigidbody; int floorMask; float camRayLength = 100f; void Awake() { floorMask = LayerMask.GetMask("Floor"); anim = GetComponent<Animator>(); playerRigidbody = GetComponent<Rigidbody>(); } void FixedUpdate() { float h = Input.GetAxisRaw("Horizontal"); float v = Input.GetAxisRaw("Vertical"); Move(h, v); Turning(); Animating(h, v); } void Move(float h,float v) { movement.Set(h, 0f, 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; playerToMouse.y = 0f; Quaternion newRotation = Quaternion.LookRotation(playerToMouse); playerRigidbody.MoveRotation(newRotation); } } void Animating(float h,float v) { bool walking = h != 0f || v != 0f; anim.SetBool("IsWalking",walking); } } | cs |
반응형
'etc-posts > Unity :: C# 튜토리얼' 카테고리의 다른 글
[유니티C#][기초] 15. 튜토리얼 SurvivalShooter 정리.4 (0) | 2018.07.14 |
---|---|
[유니티C#][기초] 14. 튜토리얼 SurvivalShooter 정리.3 (0) | 2018.07.08 |
[유니티C#][기초] 12. 튜토리얼 SurvivalShooter 정리.1 (0) | 2018.07.08 |
[유니티C#][기초] 11. 적에게 체력 부여하기 (0) | 2018.07.08 |
[유니티C#][기초] 10.플레이어 슈팅 5 (최종) (0) | 2018.07.08 |