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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

ScreenToWorldPoint not working correctly.

Discussion in '2D' started by leechbeech, Nov 29, 2018.

  1. leechbeech

    leechbeech

    Joined:
    Sep 25, 2018
    Posts:
    2
    Hey everyone, I am currently experiencing an issue with my code which finds the mouse position in order to place prefabbed objects in my game (It works in a grid placement system).
    Here is my code:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class PlowScript : MonoBehaviour
    8. {
    9.  
    10. [SerializeField]
    11. private GameObject finalObject; //The object which is created upon clicking the mouse
    12. [SerializeField]
    13. private Vector2 mousePos;
    14. [SerializeField]
    15. private LayerMask allTilesLayer;
    16.  
    17. void Update()
    18. {
    19.  if (Input.GetMouseButtonDown(0))
    20.  {
    21.    mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);    //This line is the issue
    22.    RaycastHit2D rayHit = Physics2D.Raycast(mousePos, Vector2.zero, Mathf.Infinity, allTilesLayer);
    23.                
    24.    if (rayHit.collider == null)
    25.    {
    26.       Instantiate(finalObject, transform.position, Quaternion.identity);
    27.    }
    28.   }
    29.  }
    30. }
    The issue i'm having is, In my game the camera follows my player. For some strange reason the above code is not finding my mouse position but is finding the camera position. this means whenever i click to place an object, the object is placed where my character is standing and not where my mouse is positioned. Any help would be greatly appreciated.

    I have included photos of my camera and game object which houses the script in my inspector.

    Many thanks.
     

    Attached Files:

  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Well, you are using a perspective camera, that might be the problem.
     
    Nelz01 and leechbeech like this.
  3. Rockaso

    Rockaso

    Joined:
    Oct 31, 2016
    Posts:
    85
    You are Instantiating using the Script's transform position, and that's going to be the parent's position, always, so try changing that, to the mouse world position:

    Code (CSharp):
    1. Instantiate(finalObject, mousePos, Quaternion.identity);
     
    leechbeech likes this.
  4. leechbeech

    leechbeech

    Joined:
    Sep 25, 2018
    Posts:
    2
    Thank you very much, that solved it. Many thanks
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,470
    Just wanted to add that using raycast to find if 2D colliders are overlapping a point in space is awkward, especially passing a zero direction vector using infinity as distance. Just use OverlapPoint, which will be quicker and you won't have to pass odd values to it.