Drag Object with Mouse – Unity3D (C#) It’s too easy and quick to drag an object with mouse. Just use OnMouseDown() and OnMouseDrag() functions. private Vector3 screenPoint; private Vector3 offset; void OnMouseDown(){ screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position); offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); } void OnMouseDrag(){ Vector3 cursorPoint = new […]
click
Move Object to Mouse Click Position – Unity3D (C#) No matter 2D or 3D, we use same method to move an object to mouse click position. We need to get mouse click position on ground or whatever, and move object to this position. Create a plane and a cube. We […]
Move Object to Mouse Click Position – Unity3D (C#)
Mouse Click on Game Object – Unity3D (C#) There are two ways to perform click function on a game object: 1- Create and attach a script to the target object. Write this function in it. void OnMouseDown(){ Destroy(gameObject); } 2- You can use Physics.Raycast to detect target with ray. […]
