cwisbg

Unity drag drop 2d

Oct 14th, 2018
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6. public class DragDrop : MonoBehaviour {
  7.     public string SpawnGroupName = "SpawnGroup";
  8.     public float ScaleSpeed = 1;
  9.     //public GameObject DragObj;
  10.     public bool OnClick = false;
  11.     private GameObject PlantNew;
  12.  
  13.     public List<GameObject> PlantList;
  14.  
  15.     private GameObject SpawnGroup;
  16.    
  17.     Vector3 MousePosition()
  18.     {
  19.         Vector3 MousePos = Input.mousePosition;
  20.         MousePos = Camera.main.ScreenToWorldPoint(MousePos);
  21.         MousePos = new Vector3(MousePos.x, MousePos.y, 0f);
  22.         return MousePos;
  23.     }
  24.  
  25.     void Start () {
  26.         SpawnGroup = new GameObject(SpawnGroupName);
  27.  
  28.     }
  29.     void Update () {
  30.        
  31.         //OnClick = false;
  32.         Vector3 MousePos = MousePosition();
  33.         bool IsFlipped = false;
  34.         if (Input.GetKey("f"))
  35.         {
  36.             IsFlipped = true;
  37.         }
  38.         if (Input.GetMouseButton(0))
  39.         {
  40.             if (OnClick == false) {
  41.                 int ListIndex = Random.Range(0, PlantList.Count);
  42.                 //Debug.Log(ListIndex);
  43.                 PlantNew = (GameObject)Instantiate(
  44.                 PlantList[ListIndex],
  45.                 MousePos + new Vector3(0,0, Random.Range(-0.1f, 0.1f)),
  46.                 transform.rotation);
  47.  
  48.                 PlantNew.transform.parent = SpawnGroup.transform;
  49.                 OnClick = true;
  50.             }
  51.            
  52.             if (PlantNew)
  53.             {
  54.                 //PlantNew.transform.position = MousePos;
  55.  
  56.                 Vector3 diff = MousePos - PlantNew.transform.position;
  57.                 float MagScale = Vector3.Distance(MousePos, PlantNew.transform.position) * ScaleSpeed;
  58.                 //Debug.Log(MagScale);
  59.                 float AngleInRadians = Mathf.Atan2(diff.y, diff.x);
  60.                 float AngleInDegrees = AngleInRadians * Mathf.Rad2Deg;
  61.                 PlantNew.transform.rotation = Quaternion.Euler(0.0f, 0.0f, AngleInDegrees);
  62.                 if (IsFlipped)
  63.                 {
  64.                     PlantNew.transform.localScale = new Vector3(-MagScale, MagScale, 1);
  65.                 }
  66.                 else
  67.                 {
  68.                     PlantNew.transform.localScale = new Vector3(MagScale, MagScale, 1);
  69.  
  70.                 }
  71.  
  72.             }
  73.            
  74.  
  75.  
  76.         }
  77.         if (Input.GetMouseButtonUp(0))
  78.         {
  79.             OnClick = false;
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment