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);
}
}
}
댓글 없음:
댓글 쓰기