Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Translate a point in 3D to a 2D point on the screen.

Discussion in 'Scripting' started by MrMagoo22, Apr 13, 2014.

  1. MrMagoo22

    MrMagoo22

    Joined:
    Apr 13, 2014
    Posts:
    12
    I'm working on a project which combines 3D models with 2D sprites. Here's a screenshot so you can get an idea of what I'm talking about: http://i.imgur.com/hNqvZ9r.png

    Basically what I'm looking to do is rotate the turret sprites to aim at targets within the environment. The sprite is all split up and I can rotate it around in the editor (http://i.imgur.com/n925TG2.png). Problem is, I want to replicate the behavior through code, and I'm not sure how. I need to isolate the 2D point on the camera field for both the turret and the target, find the rotation value between the two points, multiply the X scale by -1 if the target is to the left of the turret (So it doesn't rotate upside down) and rotate the tower so it all looks right. Most of that sounds pretty reasonable, I'm just getting stuck on the 3D to 2D point projection.

    Is there a good function or method in Unity to do this? I have a few visual effects that rely on being able to do that conversion very easily, so if there's something built into the engine, that would be awesome.
     
  2. Deleted User

    Deleted User

    Guest

  3. MrMagoo22

    MrMagoo22

    Joined:
    Apr 13, 2014
    Posts:
    12
    Sweet! Looks like that does exactly what I'm looking for. Thanks!