0

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

track

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:

enter image description here

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

3
  • You can get the vector your train is looking Vector3 forwardVector = transform.forward; and then just use float angle = Vector3.Angle(vectorA, vectorB); to get the angle between two vectors
    – Epidermis
    Commented Oct 29 at 7:15
  • It is not clear to me what exactly you are after. Is the goal to select the right train sprite based on the view direction and its position along the track?
    – JonasH
    Commented Oct 29 at 7:38
  • @JonasH so the isometric direction is not equal to the normal direction meaning when the train goes ne or sw its on the track and when its going nw and se its offset.. I will try the answer shingo provided and hope it works.. me and two versions of chatgpt have been at this for 3 days now with various degrees of luck but no perfect result.. wish I paid more attention in geometry class..
    – Jaxx0rr
    Commented Oct 29 at 20:24

1 Answer 1

3

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.