CharacterController movement with A*, Please Help!

I have this movement script attached to a unit. When I right click, my camera does a ScreenPointtoRay and gets the Vetor3 of where I clicked. This I know works because it is how I was moving my units pre-Astar. The issue Im having now is getting my Character Controller to move properly using the VectorPath[] of waypoints. The CharacterController either doesn’t move at all, or Zooms away off the map at incredible speeds. I’m also using PhotonNetwork, and the unit has a PhotonView attached observing the CharacterController. Here is my code:

Edit: Speed = 15f

`void Start () 
	{
		CharController=GetComponent<CharacterController>();
		Speed=this.gameObject.GetComponent<_Unit>().Speed;
		seeker=gameObject.GetComponent<Seeker>();
		currentWaypoint=0;
	}

	private void UpdateMove(Vector3 newDirection)
	{
		CharController.SimpleMove(newDirection);
	}

	void FixedUpdate()
	{
		if(path!=null)
			//Unit has a path
		{

			if(Vector3.Distance(transform.position,path.vectorPath[currentWaypoint]) < wpoffset)
				//Unit has arrived at next WayPoint
			{
				if(path.vectorPath.Count!=(currentWaypoint+1))
					//Path has another WayPoint
				{		
					currentWaypoint++;

					//changing the Y coordinates so that my Character Controller doesn't try to walk down?
					changeY=path.vectorPath[currentWaypoint];
					changeY.y+=1f;
					
					movedirection=((changeY-transform.position).normalized)*Speed;
					UpdateMove(movedirection);
				}
				else
					//Unit has arrived at last WayPoint of current Path
				{
					movedirection=Vector3.zero;
					UpdateMove(Vector3.zero);
					path=null;
					currentWaypoint=0;
				}					
			}

			if(movedirection != Vector3.zero)
				 // keep moving
			{
				UpdateMove(movedirection);
			}
		}



		if(gameObject.GetComponent<UnitSelection>().selected  &&  Input.GetMouseButtonDown(1))
		{
			//if Unit is Selected and I Right Click to a new Destination
			destination=PrimeCamera.GetDestination();
			seeker.StartPath(transform.position, destination, OnPathComplete);
			if(path==null)
			{
				Debug.Log ("No path");
				return;
			}
			else
			{
				Debug.Log("Got Path");

				//changing the Y coordinates so that my Character Controller doesnt try to walk down?
				changeY=path.vectorPath[currentWaypoint];
				changeY.y+=1f;
				movedirection=((changeY-transform.position).normalized)*Speed;
				UpdateMove(movedirection);
			}
		}
	}

	public void OnPathComplete(Path p)
	{
		path=p;
		currentWaypoint=0;
	}`

I’m fairly new to using Character Controllers, so I’m not sure if I should be using .Move or .SimpleMove., or if the height of the character off of the ground is affecting its movement.

Also possibly related is that I frequently get “No Path” returns when right clicking- even when the path is along flat, Grid Graphed ground that is clearly navigatable.

Hi

You get No Path because the path has not been calculated yet.
The system operates asynchronously, so you might not get back the path immediately. If you want to force the system to calculate the path immediately you can use

var newPath = seeker.StartPath (...); AstarPath.WaitForPath (newPath);

Think about this case.
The character gets the path back, your FixedUpdate code runs but the first waypoint is NOT within the distance wpoffset, then the movedirection will never be set to anything other than Vector3.zero and the character will just stand still.

You should update your movedirection variable every frame not just when getting a new waypoint.

1 Like