Character Directon Change while moving on a Tilemap (grid)

Hey everybody,

I tried implementing a character direction change based on the path the character is moving on. As I am using a Pathfinder algorithm it happens that he needs to change twice direction during the same path (L shape path) in the case it would mean changing position once to DOWN and at the corner to RIGHT

the Code shown below works for a single direction change, any idea on how to make it recognize a second one ?

private void MoveAlongPath()
{

var step = speed * Time.deltaTime;

float zIndex = path[0].transform.position.z;
character.transform.position = Vector2.MoveTowards(character.transform.position, path[0].transform.position, step);

character.transform.position = new Vector3(character.transform.position.x, character.transform.position.y, zIndex);


for (int i = 0; i < path.Count; i++)
{

var previousTile = i > 0 ? path[i - 1] : character.standingOnTile;
var futureTile = i < path.Count - 1 ? path[i + 1] : null;
var currentTile = path;

Debug.Log("currenttile : " + previousTile.grid2DLocation);

if (currentTile.transform.position.x > previousTile.transform.position.x && currentTile.transform.position.y > previousTile.transform.position.y)
{
character.SetDirection(Vector2.right);
}
else if (currentTile.transform.position.x < previousTile.transform.position.x && currentTile.transform.position.y < previousTile.transform.position.y)
{
character.SetDirection(Vector2.left);
}
else if (currentTile.transform.position.x > previousTile.transform.position.x && currentTile.transform.position.y < previousTile.transform.position.y)
{
character.SetDirection(Vector2.down);
}
else if (currentTile.transform.position.x < previousTile.transform.position.x && currentTile.transform.position.y > previousTile.transform.position.y)
{
character.SetDirection(Vector2.up);
}

}

if (Vector2.Distance(character.transform.position, path[0].transform.position) < 0.00001f)
{
PositionCharacterOnLine(path[0]);
path.RemoveAt(0);
}

if (path.Count == 0)
{

GetInRangeTiles(false);
isMoving = false;
}

}

8124989--1053482--upload_2022-5-13_14-18-43.png

Please use code-tags when posting code.

Sorry about that wasn’t aware of this tag :slight_smile:

1 Like

That is a metric ton of repeated code. Here’s how to simplify it:

  • you have current and previous positions: subtract them to get a difference vector.

  • now analyze that difference vector to sort out your direction.

Vector3 delta = position2 - position1;

if (delta.x > 0) face right
if (delta.y > 0) face down
etc.

Generally speaking, if you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums

Yes true it helped us a lot to simplify the code and make it more lisible, but still our character isnt changing direction while going on a L shape path, as you can see on the video
https://www.youtube.com/watch?v=PSb07lEHlhk

our current code with your insight :

var previousTile = i > 0 ? path[i - 1] : character.standingOnTile;
                var futureTile = i < path.Count - 1 ? path[i + 1] : null;
                var currentTile = path[i];
                bool isFinal = futureTile == null;
                Vector3 delta = previousTile.transform.position - currentTile.transform.position;
                if (delta.x > 0 && delta.y > 0) character.SetDirection(Vector2.left);
                if (delta.x < 0 && delta.y < 0) character.SetDirection(Vector2.right);
                if (delta.x > 0 && delta.y < 0) character.SetDirection(Vector2.up);
                if (delta.x < 0 && delta.y > 0) character.SetDirection(Vector2.down);

Time to start debugging! Here’s how:

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3