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. Dismiss Notice

Screentoworldpoint Help

Discussion in 'Scripting' started by theBdrive, May 14, 2020.

  1. theBdrive

    theBdrive

    Joined:
    May 22, 2015
    Posts:
    20
    Using an orthographic camera set at 0,0,-10, I'm trying to set the position of an object to the bottom left of the screen on any device. I'm using
    Code (CSharp):
    1. Vector3 newPos = Camera.main.ScreenToWorldPoint(new Vector3(0,0, 10));
    2. gameObject.transform.position = newPos;
    But the objects x & y are way off to the left of the screen. The newPos ends up being at -2.1, -4.7 for the x and y.I thought with screen coordinates 0,0 was always the exact bottom left, not sure if what I'm doing wrong here. I've also tried ViewportToWorldPoint using 0,0 but I get the same result, any help would be appreciated. Thanks.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Depending on the size of the orthographic camera and aspect ratio of your screen, that sounds about right. If your camera is at (0, 0, -10) and is facing (0, 0, 1), I would expect both x and y to be negative at the bottom-left of the camera view. World coordinates (0, 0) would be at the very center of the camera view ((.5, .5) in viewport coordinates). What values are you expecting to get?
     
    Kurt-Dekker likes this.
  3. theBdrive

    theBdrive

    Joined:
    May 22, 2015
    Posts:
    20
    Solved, I overlooked the origin in Blender of the model was not set to geometry origin so it was causing the coordinates to be off in Unity. Silly me.
     
    PraetorBlue likes this.