Direction of Movement

Trying to make the 3D model face towards the direction of movement.
I am using “transform.lookAt” and initially tried using currentPath.endPoint but realized it only points to the end point of the path.

Not sure how to get the direction of the immediate next node or the general movement direction. I see there is a “GetMovementVector” but not sure what parameter I should be passing through that.

Well, the easy way to do this is look at the node your walking to~

`
public void MoveAB(int NextGridSpace, Path CurrentPath, float UnitSpeed ){
Vector3 Direction;
//--------------Moving-------------------//
// The direction to the next waypoint – -- Normalize makes the Vector (IE: 0,0,50) all in values of 1 – * our speed
Direction = (CurrentPath.vectorPath[NextGridSpace]-Player.position).normalized * UnitSpeed;
RVOController.Move(Direction);
transform.lookAt(Direction);
//--------------Moving-------------------//

	`

But there is a better WAY!.. But I got to go right now~ So give me a few hours and I will get back too you :stuck_out_tongue:

Ok~ So I use a custom class called Movementmanager for all this stuff… Here are some of its more useful turning methods…

`using UnityEngine;
using System.Collections;
using Pathfinding;

public class Movementmanager {

public RVOController RVOController;
    // RVOController's are basically the same a CharacterController, except you don't need to * DeltaTime 
public Transform Player;

public Move(Transform player){
	RVOController = player.GetComponent<RVOController>();
	Player = player;
}

`
This is just a basic class (No Mono nonsense), So any Unit or player just needs to call new Movementmanager(this.transform) on start to gain its methods

`

public void TurnAB(float UnitTurnSpeed, Vector3 LastPosition){
	Vector3 LookAt = Player.position - LastPosition;
            // This gets the opposite LOCAL position of the *LastPosition*.... IE(If the player is now at 1,1,1 and the LastPosition was 0,0,0....  LookAt will be 1,1,1 in local space, and will be 2,2,2 in world space)
	Quaternion LookRotation = Quaternion.LookRotation(LookAt);
            // Pretty simple here...
	Quaternion rotation = Quaternion.Slerp(Player.rotation, LookRotation , Time.deltaTime * UnitTurnSpeed);
            // Pretty simple here too.....
	Player.rotation = Quaternion.Euler(0, rotation.eulerAngles.y, 0);
           // This just makes sure no *Tilting* happens when, walking up stairs and such
}

}
`
By passing my LastPosition I can make sure the unit is ALWAYS looking where its moving~ If you use local avoidance, or have any colliders this is VERY useful…

Just make sure you Interpol the LastPosition a little, at higher FPS when the values are very small, you can get a bit of twitchyness~

Something like averaging the last 3 frames, or looking away from the Position 5 frames ago works well…

I use
`
private List LastPositionList;
private Vector3 LastPosition;

void LastPositionCheck(){
	if(LastPositionList.Count >= 5){
		LastPositionList.Add (this.transform.position);
		LastPosition = LastPositionList[0];
		LastPositionList.RemoveAt(0);
	}
	else{
		LastPositionList.Add (this.transform.position);
	}
}

`

And it works… I bet there is a faster way though

You can also reduce bugs by making very slow speeds not pass count~
`

	if(Vector3.Distance(transform.position, LastPosition) > 1 * Time.deltaTime){//And you can also add stuff like *Is not stuned* or any other exception 
		Move.TurnAB(UnitTurnSpeed, LastPosition);
	}`

EDIT: private List<Vector3> LastPositionList; should be protected not private :stuck_out_tongue:
Replace it with protected List<Vector3> LastPositionList;

It worked thanks! :slight_smile: