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 in 2D

Discussion in '2D' started by ptgodz, Aug 4, 2016.

  1. ptgodz

    ptgodz

    Joined:
    Aug 4, 2016
    Posts:
    106
    Hi Guys, I'm new to the forums and new to Unity, I've been studying Unity and C# for about 2 weeks.

    I'm having one problem though with ScreenToWorldPoint.
    Code (CSharp):
    1.             Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    2.             GameObject bullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity) as GameObject;
    3.             bullet.GetComponent<Rigidbody2D>().velocity = mousePos - transform.position;
    I've wrote the code above myself and it does exactly what I want and understand it to a degree, the only issue I have is I don't fully understand ScreenToWorldPoint. After experimenting through unity, I've come to the conclusion that the screen position has the center of the screen as 0,0 on the X and Y plane and this corresponds to the transform position at origin.

    Why does mousePos - transform.position produce the results I want despite the fact that my gameobject is in a random world position? If I think about it long enough it sometimes makes sense but quickly loses meaning. If someone could explain this to me in simple terms I would greatly appreciate it.

    Thankyou
     
  2. ptgodz

    ptgodz

    Joined:
    Aug 4, 2016
    Posts:
    106
    One more thing I'd like to ask if how could I have the bullet move at a constant speed as opposed to a speed relative to the cursor position. Thanks
     
  3. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Screen coordinates are based on the acutal resolution of the screen. The lower, left corner is 0,0 and the upper, right corner is depends on the resolution of your screen/window. So, if your game is running in 1980x1080, then the upper, right corner has the coordinate of 1920, 1080.

    ScreenToWorldPoint converts what part of the world the camera "sees" at that particular screen point.

    When you subtract the two vectors, you'll get a vector that points from your transform.position towards the place where you clicked. Which is the direction you want your bullet to move.

    To get a consistent speed you need to normalize the vector. This will create a vector that is one unit long, then you can multiply this with the speed you want the bullet to have. Something like this:
    Code (CSharp):
    1. bullet.GetComponent<Rigidbody2D>().velocity = (mousePos - transform.position).normalized * speed;
    2.  
     
  4. ptgodz

    ptgodz

    Joined:
    Aug 4, 2016
    Posts:
    106
    Thanks for the quick reply. It deffinetly makes more sense now. I think I may need to go over Vector3's again so I can understand how it works when subtracting them. I understand that its a 3D position in game world space, I just get slightly confused with it comes to subtracting it. Thanks for your help
     
  5. asgaard777

    asgaard777

    Joined:
    Dec 1, 2019
    Posts:
    2
    Hello everyone,

    I am new to Unity, so please be patient... Can someone explain me with simple terms why we need to convert screen coordinates to world coordinates? Can it be that I am not fully aware of what world coordinates mean? Assuming that they represent a position in three dimensions, why do I need three coordinates if a 2D game takes place in a two-dimensional world? Does the third dimension represent how close or distant the camera is to the game objects on the screen?
     
  6. asgaard777

    asgaard777

    Joined:
    Dec 1, 2019
    Posts:
    2
    Hello,

    I am new to Unity, so please be patient... Can you explain to me with simple terms why we need to convert screen coordinates to world coordinates? Can it be that I am not fully aware of what world coordinates mean? Assuming that they represent a position in three dimensions, why do I need three coordinates if a 2D game takes place in a two-dimensional world? Does the third dimension represent how close or distant the camera is to the game objects on the screen?