Answers for "unity drag sprite"

C#
0

unity drag sprite

[RequireComponent(typeof(BoxCollider2D))]
 public class dragGameSprite : MonoBehaviour
 {
     private Vector3 screenPoint;
     private Vector3 offset;
 
     void OnMouseDown()
     {
         offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
     }
 
     void OnMouseDrag()
     {
         Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
         Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
         transform.position = curPosition;
     }
 }
Posted by: Guest on December-19-2020

Code answers related to "unity drag sprite"

C# Answers by Framework

Browse Popular Code Answers by Language