So after watching the video I instantly encountered my player bugging out after reaching a destination. When before I had no issues. After some debugging I narrowed it down to the animation controller slightly offsetting the y-axis value which in turn offset the magnitude of the vector and returning a value of greater than 0. After fixing that issue, I also noticed when clicking an enemy, since the y-axis was different than the player, it could give a distance of .5f and greater just due to height of the object. So I have now temporarily solved this issue with the following code, I feel it could be greatly improved, but as I plan on using a tile map in my final product, I feel for me this fix is okay. I also have edited to gizmos method to correctly draw the lines, to show the changes. I hope this helps.
Code:
[code]using System;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
using UnityStandardAssets.CrossPlatformInput;
[RequireComponent(typeof (ThirdPersonCharacter))]
public class PlayerMovement : MonoBehaviour
{
[SerializeField] float walkDistanceThreshold = .4f;
[SerializeField] float attackMoveStopThreshold = 3.5f;
[SerializeField] float rotateSpeed = 1f;
ThirdPersonCharacter thridPersonCharacter; // A reference to the ThirdPersonCharacter on the object
CameraRaycaster cameraRaycaster;
Vector3 currentDestination;
Vector3 currentClickPoint;
Transform cameraTransform;
private void Start()
{
cameraTransform = Camera.main.transform;
cameraRaycaster = Camera.main.GetComponent<CameraRaycaster>();
thridPersonCharacter = GetComponent<ThirdPersonCharacter>();
currentDestination = transform.position;
}
// Fixed update is called in sync with physics
private void FixedUpdate() {
float h = CrossPlatformInputManager.GetAxis("Horizontal");
cameraTransform.RotateAround(transform.position, Vector3.up, h * rotateSpeed * Time.deltaTime);
if (Input.GetMouseButton(0)) {
currentClickPoint = cameraRaycaster.hit.point;
switch (cameraRaycaster.currentLayerHit) {
case Layer.Walkable:
currentDestination = ShortDestination(currentClickPoint, walkDistanceThreshold); // set new target on walkable
break;
case Layer.Enemy:
currentDestination = ShortDestination(currentClickPoint, attackMoveStopThreshold); // set new target on attack
break;
default:
//ignore for now
break;
}
}
WalkToDestination();
}
private void WalkToDestination() {
var destination = currentDestination - transform.position;
if (Math.Abs(destination.x) > 0.1f && Math.Abs(destination.z) > 0.1f) {
thridPersonCharacter.Move(currentDestination - transform.position, false, false);
} else {
thridPersonCharacter.Move(Vector3.zero, false, false);
}
}
Vector3 ShortDestination(Vector3 destination, float shortening) {
Vector3 reductionVector = (destination - transform.position).normalized * shortening;
return destination - reductionVector;
}
void OnDrawGizmos() {
//Draw movement
Gizmos.color = Color.black;
Vector3 floorDestination = currentDestination;
floorDestination.y = 0;
Gizmos.DrawLine(transform.position, floorDestination);
Gizmos.DrawLine(floorDestination, currentClickPoint);
Gizmos.DrawSphere(floorDestination, 0.1f);
Gizmos.DrawSphere(currentClickPoint, 0.1f);
//draw attack sphere
Gizmos.color = new Color(255f, 0f, 0f, 0.5f);
Gizmos.DrawWireSphere(transform.position, attackMoveStopThreshold);
}
}
[/code]
Preview: