'Rotate and Drag (if dragging don't rotate, else rotate) Problem Unity 2D (Not Solved)
I am moving my object with onDrag, but there is a situation like this: I do not want to execute a drag if the sprites are rotating, but if the sprites are not rotating, I want drag to be executed. The Unity 2d code is shown below.
if (!drag)
{
if (rota < 13)
{
rota = rota + 0.5f;
//rectTransform.anchoredPosition += eventData.delta;
mPosDelta = eventData.delta - mprevpos;
transform.Rotate(0, 0, rota, Space.Self);
}
else
{
this.gameObject.transform.rotation=Quaternion.Euler(0, 0,180);
Invoke("Wait",3f);
}
}
else
{
rectTransform.anchoredPosition += eventData.delta;
}
Solution 1:[1]
EDIT:this will rotating the ball and make 6
so if it turns it won't move if it doesn't it will move
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Level33DragSystem : MonoBehaviour, IDragHandler
{
// Start is called before the first frame update
Vector2 mprevpos = Vector2.zero;
Vector2 mPosDelta = Vector2.zero;
float rota;
bool drag;
void Start()
{
}
private RectTransform rectTransform;
private void Awake()
{
rectTransform = GetComponent<RectTransform>();
}
//Mouse Hareketi , Telefon ve Bilgisayar için
public void OnDrag(PointerEventData eventData)
{
if (!drag)
{
if (rota < 13)
{
rota = rota + 0.5f;
//rectTransform.anchoredPosition += eventData.delta;
mPosDelta = eventData.delta - mprevpos;
transform.Rotate(0, 0, rota, Space.Self);
}
else
{
this.gameObject.transform.rotation = Quaternion.Euler(0, 0, 180);
Invoke("Bekle", 3f);
}
}
else
{
rectTransform.anchoredPosition += eventData.delta;
}
}
public void Bekle()
{
drag = true;
this.gameObject.name = "6";
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Sayi1")
{
this.gameObject.transform.position = collision.transform.position;
this.GetComponent<Level33DragSystem>().enabled = false;
GameObject.Find("Say?1").GetComponent<BoxCollider2D>().isTrigger = false;
GameObject.Find("Say?1").GetComponent<Text>().text = this.gameObject.name;
GameObject.Find("Level 33").GetComponent<FindTwenty>().toplam = GameObject.Find("Level 33").GetComponent<FindTwenty>().toplam + int.Parse(this.gameObject.name);
GameObject.Find("Level 33").GetComponent<FindTwenty>().eklenen = GameObject.Find("Level 33").GetComponent<FindTwenty>().eklenen + 1;
GameObject.Find("Level 33").GetComponent<FindTwenty>().ControlTwenty();
}
if (collision.tag == "Sayi2")
{
this.gameObject.transform.position = collision.transform.position;
this.GetComponent<Level33DragSystem>().enabled = false;
GameObject.Find("Say?2").GetComponent<BoxCollider2D>().isTrigger = false;
GameObject.Find("Say?2").GetComponent<Text>().text = this.gameObject.name;
GameObject.Find("Level 33").GetComponent<FindTwenty>().toplam = GameObject.Find("Level 33").GetComponent<FindTwenty>().toplam + int.Parse(this.gameObject.name);
GameObject.Find("Level 33").GetComponent<FindTwenty>().eklenen = GameObject.Find("Level 33").GetComponent<FindTwenty>().eklenen + 1;
GameObject.Find("Level 33").GetComponent<FindTwenty>().ControlTwenty();
}
if (collision.tag == "Sayi3")
{
this.gameObject.transform.position = collision.transform.position;
this.GetComponent<Level33DragSystem>().enabled = false;
GameObject.Find("Say?2").GetComponent<BoxCollider2D>().isTrigger = false;
GameObject.Find("Say?3").GetComponent<Text>().text = this.gameObject.name;
GameObject.Find("Level 33").GetComponent<FindTwenty>().toplam = GameObject.Find("Level 33").GetComponent<FindTwenty>().toplam + int.Parse(this.gameObject.name);
GameObject.Find("Level 33").GetComponent<FindTwenty>().eklenen = GameObject.Find("Level 33").GetComponent<FindTwenty>().eklenen + 1;
GameObject.Find("Level 33").GetComponent<FindTwenty>().ControlTwenty();
}
}
private void Update()
{
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

