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.