I want to move a character a set distance only, no matter how far away I click on the terrain.

Hello all, I’m trying to make a script for a character to move a set distance, regardless of how far I click. It’s like how armies/units move in the campaign map of a Total War game.

If I click 50 units away, the character only moves 10 in that direction.
If I click 100 units away, the character only moves 10 in that direction.

This is the code I have so far. I’ve tried to solve it for a couple days but I’ve sort of hit a wall. The code allows me to move a unit if I click less than 5 units away, but if I click more than that, it will not move at all.

Any pointers would be appreciated, I just have no idea where to look or what I should be using.

using UnityEngine;
using System.Collections;

public class ClickToMove : MonoBehaviour {

NavMeshAgent navAgent;
public float elapsed = 0.0f;
private NavMeshPath path;
public float soFar = 0.0f;
public float maxDist = 5.0f;
Vector3 startPos;
// Use this for initialization
void Start () 
{
	path = new NavMeshPath ();
	navAgent = GetComponent<NavMeshAgent> ();
	elapsed = 0.0f;
	soFar = 0.0f;

}


void Update () 
{
	RaycastHit hit;
	Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	Debug.Log (soFar);

	if (Input.GetMouseButtonUp (1)) 
	{

		if (Physics.Raycast (ray, out hit, 100)) 
		{
			navAgent.Resume();
			soFar = 0.0f;
			startPos = transform.position;
			soFar += Vector3.Distance (transform.position, hit.point);
			NavMesh.CalculatePath(transform.position, hit.point, NavMesh.AllAreas, path);
			navAgent.SetPath(path);
			//Debug.Log(path.corners.Length);
			if (soFar > maxDist){

						navAgent.Stop ();
			}
			//navAgent.SetDestination(hit.point);
			//Debug.DrawLine(transform.position, hit.point, Color.red);

		}
	}
	for (int i = 0; i < path.corners.Length-1;i++)
	     Debug.DrawLine(path.corners*, path.corners[i+1], Color.black);*
  • }*
  •   		    }*
    

This depends on exactly what you’re looking to accomplish. When you say you want your character(s) to move a specific distance at a time, are you looking for that to be their maximum no matter what their pathing is, or is it a maximum range reached regardless of total distance traveled?

Let’s say a destination is selected 50 units away when the maximum distance to travel is only 10 units.

50970-ex1.jpg

Example 1: 10 units’ distance traveled as an absolute limit

For this method, the resulting destination would involve determining the pathing the character(s) would follow to the destination (50 units away), then tracing the distance from the starting point to each pathing node along the path until the maximum (10) distance is reached.

50971-ex2.jpg

Example 2: 10 units’ distance reached no matter what

In this case, the process would be handled in the opposite order: First, calculate the distance to the position selected (50 units away), normalize the vector and multiply by the maximum travel distance (10). Now you have the exact spot (not counting exact ground height) where your character(s) will be sent to. Then, calculate the best (if possible) way of reaching that destination from the pathing nodes.