Getting mouse position in unity

后端 未结 4 2170
感情败类
感情败类 2020-12-11 03:32

I\'m trying to move a object to the mouse position. But it\'s giving me large x value like 300 but at that place the pre placed object\'s x position is -4.

         


        
4条回答
  •  孤街浪徒
    2020-12-11 03:52

    If you wanted to use the new Unity InputSystem you could do the following:

    using UnityEngine;
    using UnityEngine.InputSystem;
    
    ...
    
    Vector2 screenPosition = Mouse.current.position.ReadValue();
    Vector2 worldPosition = Camera.main.ScreenToWorldPoint(screenPosition)
    

    Note that you need to install the InputSystem package for this to work.

提交回复
热议问题