Search Unity

  1. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  2. Unity 2017.2 is now released.
    Dismiss Notice
  3. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  6. Unity 2017.3 beta is now available for download.
    Dismiss Notice

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

Discussion in '2D' started by genaray, Oct 11, 2017.

  1. genaray

    genaray

    Joined:
    Feb 8, 2017
    Posts:
    56
    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 :

    Unbenannt.PNG

    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 :

    Unbenannt1.png

    And two more examples : Unbenannt4.png
    Unbenannt5.png

    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 :

    Code (CSharp):
    1. Vector3 targetDir = tapped.position - transform.position;
    2.  
    3.    float step = speed * Time.deltaTime;
    4.  
    5.    Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0F);
    6.  
    7.    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 !
     

    Attached Files:

  2. MiladZarrin1

    MiladZarrin1

    Joined:
    Jul 7, 2013
    Posts:
    65
    Is your character standing on your map? or is it parallel to it?
     
  3. genaray

    genaray

    Joined:
    Feb 8, 2017
    Posts:
    56
    Thanks for your fast answer ! The character is parallel to the map to make it look like hes standing on it ^^.
     
  4. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    638
    @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.
     
    Last edited: Oct 12, 2017
  5. MiladZarrin1

    MiladZarrin1

    Joined:
    Jul 7, 2013
    Posts:
    65
    The solution I found is this:

    Code (CSharp):
    1. // find the difference vector
    2. Vector3 diff = targetTransform.position - modelTransform.position;
    3.  
    4. // find the angle between our model and the target
    5. float angle = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
    6.  
    7. // translate the angle
    8. 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.
     

    Attached Files:

    Last edited: Oct 12, 2017
    genaray likes this.
  6. genaray

    genaray

    Joined:
    Feb 8, 2017
    Posts:
    56
    Wonderfull ! You totally saved my freaking day :D ! Thank you so much !
     
    MiladZarrin1 likes this.