2022년 6월 8일 수요일

Unity PlayerMovement

 




using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class PlayerMovement : MonoBehaviour
{
    public CharacterController controller;
    public NavMeshAgent playerNavMeshAgent;


    public float speed = 12f;
    public float gravity = -9.81f;
    public float jumpHeight = 3f;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;


    public Vector3 clickPos;

    Vector3 velocity;
    bool isGrounded;

    public bool isClicked;

   

    // Update is called once per frame
    void Update()
    {


        if (Input.GetKey(KeyCode.LeftAlt))
        {
            playerNavMeshAgent.enabled = true;
            controller.enabled = false;
            if (Input.GetMouseButtonDown(0))
            {
                MoveClickPosition();
            }
        }

        gravityMove();



        if (isClicked)
        {
           
        }
        else
        {
            NormalMove();
        }

        if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
        {
            playerNavMeshAgent.enabled = false;
            controller.enabled = true;
            isClicked = false;
        }
        //if (Input.anyKeyDown)
        //{
        //    if (isClicked)
        //        isClicked = false;
        //}
    }


    void NormalMove()
    {
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z;

        controller.Move(move * speed * Time.deltaTime);
       
    }

    void gravityMove()
    {
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }
        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
    }



    void MoveClickPosition()
    {
        isClicked = true;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out RaycastHit raycastHit))
        {
            playerNavMeshAgent.SetDestination(raycastHit.point);
        }
    }
}





댓글 없음:

댓글 쓰기

git rejected error(feat. cherry-pick)

 문제 아무 생각 없이 pull을 받지않고 로컬에서 작업! 커밋, 푸시 진행을 해버렷다. push에선 remote와 다르니 당연히 pull을 진행해라고 하지만 로컬에서 작업한 내용을 백업하지 않고 진행하기에는 부담스럽다(로컬작업 유실 가능성) 해결하려...