- Home /
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.
Here are a couple of notes. (1) are you absolutely sure that your model is facing towards the local Z axis of the game object? (2) what happens if you translate using the unit vector transform.Translate(moveDir * walkSpeed, Space.World);
? I have not put this as an answer because I do not entirely understand the question...
Yes I'm completly sure my character is facing the Z axis.
If I use transform.Translate(); he still moves in the right direction. The reason I'm using the $$anonymous$$ove(); method of the Character Controller is so that the Character takes collisions into consideration when moving.
I'm not using transform.forward to move him along his facing direction since I want his movement (Towards the WalkPoint) and his rotation on Y (The direction he's facing) to be seperate so that he may face the direction he's travelling when for example sliding along a wall (Collider).
What I need is a way for me to calculate the characters direction of travel (The direction he's moving) no matter how he is rotated.
$$anonymous$$y initial idea was to use Vector3 dirOfTravel = (transform.position - oldPos).normalized; which does not seem to work.
I'm sorry if my problem seems unclear.. I really don't know how I can explain it better. If my question is still unclear I can probably upload the project as a webplayer?
A webplayer demo would be useful to understand the issue. Does the problem occur when the player stops and starts? The old position becomes the same as the new position and thus direction vector is zero.
Alright.
Here is the build without any code for facing the direction of travel. http://dl.dropbox.com/u/36230178/WorkingWebBuild/WebPlayer/WebPlayer.html (This shows how the character moves around, simply click on the point you want to move to). There is no rotation on the character when moving in this build.
Here is a build where I have added some facing code. http://dl.dropbox.com/u/36230178/NonWorkingWebBuild/WebPlayer/WebPlayer.html Here I save the position of the character before moving him then using that to make him face the direction he is travelling. As you may see he just stands there (moving very little). When I play the game in the editor I can see that he is spinning wildly around his Y axis.
What I would do is use the built in character controller. https://docs.unity3d.com/$$anonymous$$anual/class-CharacterController.html
Answer by irgendeine · Apr 24, 2012 at 01:58 PM
Did you have a look at this: http://unity3d.com/support/documentation/ScriptReference/Transform.LookAt
works fine for my character...
As I said.. LookAt(); is not a possibility. If I use LookAt() the character will always look at the point he's walking towards (Even if the object avoidance changes his direction of travel or if he slides along a wall) which does not look good at all.
What I need is a way to calculate which direction the character is moving.
Answer by RevVo98 · Aug 25, 2013 at 07:34 PM
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;
:)
Answer by Zodiarc · Oct 26, 2016 at 02:15 PM
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.
Your answer
Follow this Question
Related Questions
"Fake" Vector3.forward? 3 Answers
iTween Path Editor: ignore Y position in Vector3 1 Answer
How to find the direction to translate an object, to move towards a point, regardless of rotation? 1 Answer
How to make a Direction Indicator to show where and object is going? 1 Answer
How can I move character controller in one direction 0 Answers