I’m attempting to create a system wherein a character has a maxMoveRadius
, and cannot move farther from their startPosition
, on the XZ plane, than this distance. The remainingMoveRadius
should then show the player how much movement is left, based on the character’s currentPosition
, which is relative to their startPosition
.
In the editor, I have drawn a gizmo that represents the maxMoveRadius
and it was working until I tried to calculate the distance between the character’s startingPosition
and currentPosition
, but now the gizmo won’t draw anymore. I believe I’ve simply made a logical/mathematical error, but I cannot see it. Below is the script in question.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
private Vector3 startPosition;
private Vector3 currentPosition;
private float distanceMoved;
private float remainingMoveRadius;
public float maxMoveRadius = 5.0f;
public float speed = 5.0f;
public GameObject player;
void Start()
{
startPosition = player.transform.position;
remainingMoveRadius = maxMoveRadius;
}
void Update()
{
if (currentPosition != startPosition)
{
currentPosition = player.transform.position;
}
distanceMoved = Vector3.Distance(startPosition, currentPosition);
remainingMoveRadius = maxMoveRadius - distanceMoved;
}
void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, remainingMoveRadius);
}
}