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

[SOLVED] ScreenToWorldPoint movement is moving to wrong place

Discussion in 'Scripting' started by Waizujin, Dec 28, 2015.

  1. Waizujin

    Waizujin

    Joined:
    Dec 28, 2015
    Posts:
    9
    So I'm sure my project is structured weird, which is why it's giving me issues. Not worked with unity much, I'm a web developer and just playing with Unity as a hobby where I might be able to make something cool years down the road.

    My problem is: I am attempting to right click anywhere on the screen and move a gameobject to that location. However when I do this, it doesn't go to the location I am clicking, this is made worse when I zoom. When I zoom I am moving the camera closer to the objects on the z axis.

    I am using a perspective camera, but the camera is fixed at 0,0,0 with rotation 0,0,0.
    I have planets at 0,0,50
    Space backdrop (Quad) at 0,0,60
    Ship (the game object I am trying to move) is at 0,1,45 (Slightly closer to the camera than the planets, and slightly above the planet.

    My code for ship movement is:
    Code (CSharp):
    1. if(Input.GetMouseButton(1)) {
    2.             Debug.Log("Right clicked, we need to move!");
    3.  
    4.             Vector3 mousePosition = Input.mousePosition;
    5.             Vector3 worldPosition = Camera.main.ScreenToWorldPoint(new Vector3(mousePosition.x, mousePosition.y, Camera.main.nearClipPlane));
    6.             worldPosition.z = 45;
    7.             transform.DOMove(worldPosition, 1);
    8. }
    worldPosition.z = 45 is intended to keep the object at 45 so it doesn't get closer or further from the camera.
    transform.DOMove is part of DOTween. I get same results with a simple transform.position.

    This script is attached to only the main camera.

    I'm sure I'm doing a number of things wrong, but hopefully you guys can help me solve this. I've poured a ton of hours into this and tried many different solutions but none of them have worked for me.
     
  2. elelec

    elelec

    Joined:
    Jul 21, 2014
    Posts:
    7
    My approach is to create a ray using Camera.main.ScreenPointToRay, then Raycast and after that, get the hit point, if there is one, else get the point with some calculations.

    Try something like this:
    Code (CSharp):
    1.             Vector3 mousePos = new Vector3(Input.mousePosition.x, 0, Input.mousePosition.y);
    2.             Vector3 worldPosition;
    3.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    4.             RaycastHit hit;
    5.             if (Physics.Raycast(ray.origin, ray.origin + ray.direction * 100f, out hit))
    6.                 worldPosition = hit.point;
    7.             else
    8.                 worldPosition = ray.origin + ray.direction * 100f;
    It might become better or more efficient with some tweaking, but I hope it works
     
  3. Waizujin

    Waizujin

    Joined:
    Dec 28, 2015
    Posts:
    9
    It acts about the same. It doesn't go exactly where the mouse is as soon as I zoom in or out.
     
  4. Waizujin

    Waizujin

    Joined:
    Dec 28, 2015
    Posts:
    9
    I solved it! It's stupid, but pretty sure it's just because I have my project structured stupid. So if anyone else has this issue, here is what I did.

    Code (CSharp):
    1. Vector3 mousePosition = Input.mousePosition;
    2.  
    3. mousePosition.z = mousePosition.z - (Camera.main.transform.position.z - 45);
    4.  
    5. Vector3 worldPosition = Camera.main.ScreenToWorldPoint(mousePosition);
    6.  
    7. transform.DOMove(worldPosition, 1);
    This is all inside of the Input.getMouseButton
    You have to modify the z axis before, not after. If I removed the above - 45 value then the object will follow my mouse at the 0 position, but because I want my ship to be at the 45 position I added the - 45.

    If that makes any sense. I've tried something like this many times but I guess I just didn't get it right.

    So glad to have it fixed!