Make a 3D Object look like hes facing a point in an 2D space

he first thing you will notice is the complicated and confusing title. So let me explain that.

Im trying to make an 2D game in an 3D space using Unity. And im using a 3D Character as an Player. This looks like that :

As you can see the Background ( A Google map ) is two dimensional. While the Player is a 3D Object laying on the ground ( It just Looks like hes Standing ).

Thats working fine so far. But i want that the 3D Character Looks like hes facing a tapped Point on the Background map.

For example :

And two more examples :


The black circle represents the tapped Position. So i have totally no clue if theres a way to do that, or even if its possible to do that.

I tried the following code, but that only rotates my character on an different axis :

Vector3 targetDir = tapped.position - transform.position;

   float step = speed * Time.deltaTime;

   Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0F);

   transform.rotation = Quaternion.LookRotation(newDir);

Is there even a way to achieve that ? Im currently out of ideas… I would be very glad for any help i can get !

Is your character standing on your map? or is it parallel to it?

Thanks for your fast answer ! The character is parallel to the map to make it look like hes standing on it ^^.

@genaray

If you have the data for the tapped point on the map and the character’s current position on the map, you can get the difference between them, normalize it, and set the character’s Transform.forward to the resulting vector.

If the map space is oriented differently from the character’s (a direction vector on the map might be in x-y, but the character rotation might need to be about y or in x-z), you can convert the difference vector from the map to an angle, then use that to set the angle of the character. You may need to add a certain number of degrees to the angle before applying it to the character, depending on what the default (0 rotation) direction of the character model is about the y-axis.

If it doesn’t look right because of the way the character is oriented relative to the map, you could apply a simple transformation to the output angle/vector to nudge it closer to the best-looking angle-- we would have to come up with some formula, but it shouldn’t be too hard.

The solution I found is this:

// find the difference vector
Vector3 diff = targetTransform.position - modelTransform.position;

// find the angle between our model and the target
float angle = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;

// translate the angle
modelTransform.localRotation = Quaternion.AngleAxis(90 - angle, Vector3.up);

I’ve attached this small sample for you.
Just move the cross and the character looks at it.
I hope it helps.

3251816–250378–3DCharacterLooksIn2DSpace.rar (623 KB)

1 Like

Wonderfull ! You totally saved my freaking day :smile: ! Thank you so much !

1 Like