Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class DragDrop : MonoBehaviour {
- public string SpawnGroupName = "SpawnGroup";
- public float ScaleSpeed = 1;
- //public GameObject DragObj;
- public bool OnClick = false;
- private GameObject PlantNew;
- public List<GameObject> PlantList;
- private GameObject SpawnGroup;
- Vector3 MousePosition()
- {
- Vector3 MousePos = Input.mousePosition;
- MousePos = Camera.main.ScreenToWorldPoint(MousePos);
- MousePos = new Vector3(MousePos.x, MousePos.y, 0f);
- return MousePos;
- }
- void Start () {
- SpawnGroup = new GameObject(SpawnGroupName);
- }
- void Update () {
- //OnClick = false;
- Vector3 MousePos = MousePosition();
- bool IsFlipped = false;
- if (Input.GetKey("f"))
- {
- IsFlipped = true;
- }
- if (Input.GetMouseButton(0))
- {
- if (OnClick == false) {
- int ListIndex = Random.Range(0, PlantList.Count);
- //Debug.Log(ListIndex);
- PlantNew = (GameObject)Instantiate(
- PlantList[ListIndex],
- MousePos + new Vector3(0,0, Random.Range(-0.1f, 0.1f)),
- transform.rotation);
- PlantNew.transform.parent = SpawnGroup.transform;
- OnClick = true;
- }
- if (PlantNew)
- {
- //PlantNew.transform.position = MousePos;
- Vector3 diff = MousePos - PlantNew.transform.position;
- float MagScale = Vector3.Distance(MousePos, PlantNew.transform.position) * ScaleSpeed;
- //Debug.Log(MagScale);
- float AngleInRadians = Mathf.Atan2(diff.y, diff.x);
- float AngleInDegrees = AngleInRadians * Mathf.Rad2Deg;
- PlantNew.transform.rotation = Quaternion.Euler(0.0f, 0.0f, AngleInDegrees);
- if (IsFlipped)
- {
- PlantNew.transform.localScale = new Vector3(-MagScale, MagScale, 1);
- }
- else
- {
- PlantNew.transform.localScale = new Vector3(MagScale, MagScale, 1);
- }
- }
- }
- if (Input.GetMouseButtonUp(0))
- {
- OnClick = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment