Người chơi (Tiếp theo)

0
115

Người chơi (Tiếp theo)
Đã kiểm tra với phiên bản: 5.5
– –
Độ khó: Trung cấp
Giai đoạn 1 dạy bạn làm thế nào để xây dựng một nhấp chuột để di chuyển nhân vật hoạt hình bằng cách sử dụng EventSystem, NavMesh, Animator và Prefabs. Tải xuống gói nội dung cho Giai đoạn 1 tại đây.
Tải xuống gói nội dung cho Giai đoạn 1 tại đây.https://www.assetstore.unity3d.com/en/?&_ga=2.55919019.1082230255.1536856804-659907597.1536596969#!/content/76783

PlayerMovement

Expand view

Copy code
using System.Collections;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.EventSystems;
public class PlayerMovement : MonoBehaviour
{
public Animator animator;
public NavMeshAgent agent;
public float turnSmoothing = 15f;
public float speedDampTime = 0.1f;
public float slowingSpeed = 0.175f;
public float turnSpeedThreshold = 0.5f;
public float inputHoldDelay = 0.5f;

private Interactable currentInteractable;
private Vector3 destinationPosition;
private bool handleInput = true;
private WaitForSeconds inputHoldWait;
private readonly int hashSpeedPara = Animator.StringToHash("Speed");
private readonly int hashLocomotionTag = Animator.StringToHash("Locomotion");
public const string startingPositionKey = "starting position";
private const float stopDistanceProportion = 0.1f;
private const float navMeshSampleDistance = 4f;
private void Start()
{
agent.updateRotation = false;
inputHoldWait = new WaitForSeconds (inputHoldDelay);
destinationPosition = transform.position;
}
private void OnAnimatorMove()
{
agent.velocity = animator.deltaPosition / Time.deltaTime;
}
private void Update()
{
if (agent.pathPending)
return;
float speed = agent.desiredVelocity.magnitude;

if (agent.remainingDistance <= agent.stoppingDistance * stopDistanceProportion)
Stopping (out speed);
else if (agent.remainingDistance <= agent.stoppingDistance)
Slowing(out speed, agent.remainingDistance);
else if (speed > turnSpeedThreshold)
Moving ();

animator.SetFloat(hashSpeedPara, speed, speedDampTime, Time.deltaTime);
}
private void Stopping (out float speed)
{
agent.Stop();
transform.position = destinationPosition;
speed = 0f;
if (currentInteractable)
{
transform.rotation = currentInteractable.interactionLocation.rotation;
currentInteractable.Interact();
currentInteractable = null;
StartCoroutine (WaitForInteraction ());
}
}
private void Slowing (out float speed, float distanceToDestination)
{
agent.Stop();
float proportionalDistance = 1f - distanceToDestination / agent.stoppingDistance;
Quaternion targetRotation = currentInteractable ? currentInteractable.interactionLocation.rotation : transform.rotation;
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, proportionalDistance);
transform.position = Vector3.MoveTowards(transform.position, destinationPosition, slowingSpeed * Time.deltaTime);
speed = Mathf.Lerp(slowingSpeed, 0f, proportionalDistance);
}
private void Moving ()
{
Quaternion targetRotation = Quaternion.LookRotation(agent.desiredVelocity);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, turnSmoothing * Time.deltaTime);
}
public void OnGroundClick(BaseEventData data)
{
if(!handleInput)
return;

currentInteractable = null;
PointerEventData pData = (PointerEventData)data;
NavMeshHit hit;
if (NavMesh.SamplePosition (pData.pointerCurrentRaycast.worldPosition, out hit, navMeshSampleDistance, NavMesh.AllAreas))
destinationPosition = hit.position;
else
destinationPosition = pData.pointerCurrentRaycast.worldPosition;
agent.SetDestination(destinationPosition);
agent.Resume ();
}
public void OnInteractableClick(Interactable interactable)
{
if(!handleInput)
return;
currentInteractable = interactable;
destinationPosition = currentInteractable.interactionLocation.position;
agent.SetDestination(destinationPosition);
agent.Resume ();
}
private IEnumerator WaitForInteraction ()
{
handleInput = false;
yield return inputHoldWait;
while (animator.GetCurrentAnimatorStateInfo (0).tagHash != hashLocomotionTag)
{
yield return null;
}
handleInput = true;
}
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here