9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Unityでクリックした場所の平面座標を取得

Posted at

RPGなどで上からの視点にして、Diabloのようにクリックした位置にキャラを移動させたいときに、
下記のコードでクリックした場所のXZ平面上の座標を取得できます。

		var cursor:Vector3;	
		var _isMouseDown = false;
		var _isMouseUp = false;	 
		if(Input.touches.Length > 0){
		    var touch = Input.touches[0];
		    cursor = touch.position;	    
		    if(touch.phase == TouchPhase.Began){
		    	_isMouseDown = true; 
		    } else if (touch.phase == TouchPhase.Ended
		     || touch.phase == TouchPhase.Canceled){ 
		    	_isMouseUp = true;
		    }
		} else {
			cursor = Input.mousePosition; 
			if(Input.GetMouseButtonDown(0)){  
				_isMouseDown = true; 
	 	    } else if(Input.GetMouseButtonUp(0)) { 
		     	_isMouseUp = true;
		    }
		}

		cursor.z = Camera.main.transform.position.y;
		cursor = Camera.main.ScreenToWorldPoint(cursor) - transform.position;

		var position:Vector2 = Vector2(cursor.x, cursor.z);

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?