c# - Move player to exact touch/mouse click location -


in 2d unity game, trying move sprite location of touch/cursor (right it's mouse click).

my sprite @ location (173, 48, -52.1). however, when click location few pixels away position changed (399, 129, 0) , sprite apparently hurled vast unknown.

if (input.getmousebuttondown(0)) {      //fingerpos =  input.gettouch(0).position;     fingerpos = input.mouseposition;     transform.position = fingerpos;     debug.log(transform.position); } 

edit

current code

if (input.getmousebuttondown (0)) {         fingerpos = camera.main.screentoworldpoint(input.mouseposition); //desktop         transform.position = fingerpos;         debug.log (transform.position);     }    

update . apprantly, when click position recorded (and 1 sprite moved to) directly on camera. refer image click

transform.position in world coordinates. input.mouseposition returns values in pixel coordinates. need convert world coordinates.

fingerpos = camera.main.screentoworldpoint(input.gettouch(0).position); //mobile fingerpos = camera.main.screentoworldpoint(input.mouseposition); //desktop 

then can assign position.

transform.position = fingerpos; 

edit:

if (input.getmousebuttondown(0)) {     fingerpos = input.mouseposition;     fingerpos.z = 10;     fingerpos = camera.main.screentoworldpoint(fingerpos);      transform.position = fingerpos;     debug.log(transform.position); } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -