In the project I am working on currently, I have a C# script attached to an asset that onMouseDown, the asset flips horizontally 180 degrees. What command should I use in my code so that when the asset is double clicked it rotates 180 vertically. Do I need to separate scripts or can this be accomplished with only one? Thanks in advance!
using UnityEngine;
using System.Collections;
public class Color1 : MonoBehaviour {
void OnMouseDown ()
{
gameObject.transform.Rotate(new Vector3(0, 180, 0));
}
void Update () {
}
}
To make this work, you need to introduce a timer of some sort don’t do the single click until the double click detection has passed. Here is an exmaple:
using System.Collections;
using UnityEngine;
public class Example : MonoBehaviour {
public float clickDelta = 0.35f; // Max between two click to be considered a double click
private bool click = false;
private float clickTime;
void Update() {
if (click && Time.time > (clickTime + clickDelta)) {
transform.Rotate(new Vector3(0, 180, 0)); // Single click
click = false;
}
}
void OnMouseDown() {
if (click && Time.time <= (clickTime + clickDelta)) {
transform.Rotate (new Vector3(180,0,0)); // Double click
click = false;
}
else {
click = true;
clickTime = Time.time;
}
}
}
I was looking for double click script. I got it , but i have another problem.
I am working on puzzle game. I have 2 gameobjects. first is the sample and i cant move it , second gameobject i need to move to the first as close as possible. When it is close Vector3.Lerp starts working. Everything works but…
at the begining both gameobjects have transform.rotation.z = 0 . And Vector3.Lerp works only if transform.rotation.z = 0.
Everytime i double click my transform.rotation.z +=90. When i am doing double click couple times , and transform.rotation.z is again 0 , vector3.lerp doesn’t work.
Does anybody knows why is it so and how can i fix it ?