I need a function that converts a isometric direction (from a unity isometric tilemap) to a normal one. For example in this image I created waypoints along the track (red line) they dont attach to the tilemap but float above it using linerenderer
so using this
Vector3 targetPosition = trackPositions[currentWaypointIndex];
// Move the locomotive towards the target waypoint
transform.position = Vector3.MoveTowards(transform.position, targetPosition, Mathf.Abs(speed) * Time.deltaTime);
// Calculate the direction to the next waypoint
Vector3 direction = (targetPosition - transform.position).normalized;
I get a direction
I then want to use that to put the train sprite on the track (its actually 36 images) and I need a normal direction in degrees /10 to set the correct image..
EDIT: after using shingo's class this is what I got:
and its not perfect but closer than anything I got so far so I will try to adjust that function .. luck is my only hope at this point..
EDIT2: shingo's solution worked when making this adjustment
public static float YFactor = Mathf.Cos(Mathf.Deg2Rad * 60);
(changed it from 45 to 60) also there was a small bug where it was missing the type (float)
TYTYTYTY
I think you should move and calculate angles in world coordinates, so you need such an helper class.
public static class IsometricConverter
{
// The tilemap angle is 60 degrees.
// Revised based on the updated question
public static readonly float YFactor = Mathf.Cos(Mathf.Deg2Rad * 60);
public static Vector2 ToWorld (Vector2 p)
=> new Vector2(p.x, p.y / YFactor);
public static Vector2 FromWorld (Vector2 p)
=> new Vector2(p.x, p.y * YFactor);
}
Now let's use the class.
var targetPosition = IsometricConverter.ToWorld(trackPositions[currentWaypointIndex]);
var currentPosition = IsometricConverter.ToWorld(transform.position);
// Move the locomotive towards the target waypoint
currentPosition = Vector2.MoveTowards(currentPosition, targetPosition, Mathf.Abs(speed) * Time.deltaTime);
transform.position = IsometricConverter.FromWorld(currentPosition)
// Calculate the angle and convert it to 0 ~ 360 degrees
var angle = Vector2.SignedAngle(Vector2.right, (targetPosition - currentPosition).normalized);
angle += 180;
Please note that the 0 degree axis is now facing towards the right of the diamond. And depending on your train sprites, you may need to adjust the angle or reverse it.