Calculate direction of travel.

Hello everyone.

Before asking my question I’ll just tell you a bit about the project I’m working on.

I’m making a MOBA or ARTS game with a few friends for us to play at lans.
We’ve created a map and have all the camera controls working as well as Fog of war… All that good stuff.

Now the way you move the character/champion you control is by clicking on the point you want to move to. Once you click on the screen a prefab called WalkPoint is instantiated. Your champion will (if it does not already have a reference to the WalkPoint) search for the WalkPoint placed by the client who controls him.

The problem we’re having is the champion does not face the direction he’s traveling.

Now using transform.LookAt(); is not a possibility since the champion will have Object Avoidance and if he walks into a wall he will start sliding along it thus changing his direction of travel (We’re using the Character Controller’s Move(); method).

We’ve already tried several different methods like saving his position into a temp variable before moving him and then calculate the angle between that variable and his new position.

Vector3 dirOfTravel = (transform.position - oldPos).normalized;

Without luck. What happens using these methods is that he simply doesn’t turn to face the direction we calculate or he bugs out and screws up the Move(); method (Making him walk the wrong way or rotate rapidly while moving up on the Y axis).

Here’s the current base for our champions if it is of any help.

using UnityEngine;
using System.Collections;

public class Champ_Base : MonoBehaviour {
	
	public float WalkSpeed = 5.0f;
	public float Gravity = 20.0f;
	
	private Transform WalkPoint = null;
	private CharacterController cc;
	
	void Start(){
		cc = GetComponent<CharacterController>();	
	}
	
	void Update () {
		//Get a walk point to move to.
		if(WalkPoint == null){
			GameObject[] walkpoints = GameObject.FindGameObjectsWithTag("walkindicator");
			if(walkpoints.Length > 0){
				WalkPoint = walkpoints[0].transform;
			}
		}
		else {
			//Move towards the walkpoint.
			Vector3 moveDir = (WalkPoint.position-transform.position).normalized;
			moveDir = transform.TransformDirection(moveDir);
			moveDir *= WalkSpeed;
			cc.Move(moveDir*Time.deltaTime);
			
			//Remove the walk point once we get close enough.
			float disToPoint = Vector3.Distance(transform.position, WalkPoint.position);
			if(disToPoint < 1.25f){
				Destroy(WalkPoint.gameObject);
				WalkPoint = null;
			}
		}
		
		//DEBUG FACING DIR.
		Debug.DrawRay(transform.position, transform.forward, Color.red);
	}
	
}

(Please note that there is no turning to face direction in the code above)

I appriciate you talking your time to read this and I hope someone can explain a way to calculate the direction of travel.

Thanks.

Calculate the position in which the player has to move to the next frame, but before you actually move him to that location make him LookAt the location.

Vector3 moveTo = transform.forward * Time.deltaTime;
transform.LookAt(moveTo);
transform.position += moveTo;

:slight_smile:

I guess you’re using some kind of pathfinding, so you should have access to the points in the path. As the character is following the path let him look at the point he is moving to. If the distance is lower than a threshold you define, let him look at the next point in the path and so on till he reaches the destination.

Did you have a look at this:
http://unity3d.com/support/documentation/ScriptReference/Transform.LookAt

works fine for my character…

I don’t know how to help you, but it’s very interesting