Hi guys,
My guy is still doing a little dance when he reaches his destination. I am not seeing a solution besides changing
if (playerToClickPoint.magnitude > 0f)
to
if (playerToClickPoint.magnitude > 0.5f)
Has anyone expierenced this and found more favorable solution?
using System;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
using UnityStandardAssets.CrossPlatformInput;
[RequireComponent(typeof (ThirdPersonCharacter))]
public class PlayerMovement : MonoBehaviour
{
[SerializeField]
float walkMoveStopRadius = 0.2f;
[SerializeField]
float meleeAttackMoveStopRadius = .5f;
[SerializeField]
float rangedAttackMoveStopRadius = 2f; //Put in place now for future ranged attacks.
ThirdPersonCharacter ThirdPersonCharacter; // A reference to the ThirdPersonCharacter on the object
[SerializeField]
bool isInClickToMove = true;
CameraRaycaster cameraRaycaster;
private Transform mainCamera; // A reference to the main camera in the scenes transform
private Vector3 CameraForward; // The current forward direction of the camera
private Vector3 PlayerMovementDirection;
Vector3 currentDestination, clickMousePoint;
private void Start()
{
cameraRaycaster = Camera.main.GetComponent<CameraRaycaster>();
ThirdPersonCharacter = GetComponent<ThirdPersonCharacter>();
currentDestination = transform.position;
mainCamera = GameObject.FindGameObjectWithTag("MainCamera").transform;
}
// Fixed update is called in sync with physics
private void FixedUpdate()
{
//TODO Have this a selectable option in the options menu later.
if (Input.GetKeyDown(KeyCode.G)){
isInClickToMove = !isInClickToMove;
currentDestination = transform.position; //Clear the click target.
}
if (isInClickToMove)
{
ProcessClickToMove();
}else
{
ProcessWASDMovement();
}
}
private void ProcessClickToMove()
{
if (Input.GetMouseButton(0))
{
clickMousePoint = cameraRaycaster.hit.point;
switch (cameraRaycaster.currentLayerHit)
{
case Layer.Walkable:
currentDestination = ShortDestination(clickMousePoint, walkMoveStopRadius); // Set the target move location to the shortened move location.
break;
case Layer.Enemy:
currentDestination = ShortDestination(clickMousePoint, meleeAttackMoveStopRadius); // Set the target move location to the mouse raycast hit point.
break;
case Layer.RaycastEndStop:
break;
default:
break;
}
}
WalkToDestination();
}
private void ProcessWASDMovement()
{
// read inputs
float h = CrossPlatformInputManager.GetAxis("Horizontal");
float v = CrossPlatformInputManager.GetAxis("Vertical");
bool crouch = Input.GetKey(KeyCode.C);
// calculate move direction to pass to character
if (mainCamera != null)
{
// calculate camera relative direction to move:
CameraForward = Vector3.Scale(mainCamera.forward, new Vector3(1, 0, 1)).normalized;
PlayerMovementDirection = v * CameraForward + h * mainCamera.right;
}
else
{
//TODO We should never have camera relative directions. This else section should not be needed.
//Commented out line on March 9th, 2017, but left in to see if there are any issues.
// we use world-relative directions in the case of no main camera
//m_Move = v * Vector3.forward + h * Vector3.right;
}
#if !MOBILE_INPUT
// walk speed multiplier
if (Input.GetKey(KeyCode.LeftShift)) PlayerMovementDirection *= 0.5f;
#endif
// pass all parameters to the character control script
ThirdPersonCharacter.Move(PlayerMovementDirection, false, false);
}
Vector3 ShortDestination( Vector3 dest, float shortening)
{
Vector3 reductionVector = (dest - transform.position).normalized * shortening;
return dest - reductionVector;
}
private void WalkToDestination()
{
var playerToClickPoint = currentDestination - transform.position;
if (playerToClickPoint.magnitude > 0f)
{
ThirdPersonCharacter.Move(playerToClickPoint, false, false);
}
else
{
print("I am here!");
ThirdPersonCharacter.Move(Vector3.zero, false, false);
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawLine(transform.position, currentDestination);
Gizmos.DrawSphere(currentDestination, 0.1f);
Gizmos.DrawSphere(clickMousePoint, 0.2f);
}
}