Question by
CrazyCGChick85 · Nov 08, 2018 at 07:03 PM ·
animationrotationcollisioncollectible-game-objects
How do I make my character face the right direction?
HI Guys,
Have been working on a click to move type game. Currently I have a character that walks around a basic scene collecting objects that I click on. During animation the character walks and faces the right direction, however when I click on an object to collect the character will walk up to the object and then suddenly face the Z direction. I have looked at the LookAt function but it didn't seem to work.
Some help will be great
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI; //Needs to be added to be used with the events system
using UnityEngine.EventSystems; //Needs to be added when an event system ia required
[RequireComponent(typeof(Animator))]
public class PlayerMovement : MonoBehaviour
{
public Animator animator;
public NavMeshAgent agent;
public float inputHoldDelay = 0.5f;
public float turnSpeedThreashold = 0.5f;
public float speedDampTime = 0.1f;
public float slowingSpeed = 0.175f;
public float turnSmoothing = 15f;
public bool isStopped;
public Transform target;
private WaitForSeconds inputHoldWait;
private Vector3 destinationPosition;
private Interactable currentInteractable;
private bool handleInput = true;
private const float stopDistanceProportion = 0.1f;
private const float navMeshSampleDistance = 4f;
private readonly int hashSpeedPara = Animator.StringToHash("Speed");
private readonly int hashLocomotionTag = Animator.StringToHash("Locomotion");
private void Start()
{
agent.updateRotation = false; //Don't update mesh rtoation
inputHoldWait = new WaitForSeconds(inputHoldDelay);
destinationPosition = transform.position;
}
/*private void OnAnimatorMove()
{
//root motion contoller for imported player
Animator animator = GetComponent<Animator>();
if (animator)
{
Vector3 newPosition = destinationPosition; //Does nothing but if removed it breaks everything
newPosition = animator.deltaPosition / Time.deltaTime;
}
}*/
private void Update()
{
// RaycastHit hit;
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 > turnSpeedThreashold)
{
Moving();
}
animator.SetFloat(hashSpeedPara, speed, speedDampTime, Time.deltaTime);
}
private void Stopping(out float speed)
{
//If agent has stopped, it needs to stop and stay at new position
agent.isStopped = true; // replaces 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.isStopped = true;
transform.position = Vector3.MoveTowards(transform.position, destinationPosition, slowingSpeed * Time.deltaTime);
float proportionalDistance = 1f - distanceToDestination / agent.stoppingDistance;
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)
{
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.isStopped = false; // replaces agent.Resume();
}
public void OnInteractableClick (Interactable interactable)
{
Debug.Log("Object collected first");
if (!handleInput)
{
return;
}
Debug.Log("Object collected second "+interactable.interactionLocation);
currentInteractable = interactable;
destinationPosition = currentInteractable.interactionLocation.position;
//destinationPosition = new Vector3(0,0,10);
agent.SetDestination(destinationPosition);
agent.isStopped = false;
//Debug.Log("Object collected third");
}
private IEnumerator WaitForInteraction ()
{
handleInput = false;
yield return inputHoldWait;
while(animator.GetCurrentAnimatorStateInfo(0).tagHash != hashLocomotionTag)
{
yield return null;
}
handleInput = true;
}
}
Comment