• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ErhMuhGurd · Aug 30, 2014 at 08:15 AM · onmousedowndouble-click

Double click detection

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 () {
     
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Aug 30, 2014 at 08:20 AM

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;
         }
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ErhMuhGurd · Aug 30, 2014 at 03:15 PM 0
Share

Excellent answer! It works but now I need to go back through the code so I can try to understand why it works. I have no idea what I am looking at or what the statements mean. :-( THAN$$anonymous$$ YOU! THAN$$anonymous$$ YOU! THAN$$anonymous$$ YOU!

avatar image
0

Answer by Legrandes · Mar 03, 2015 at 09:58 AM

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 ?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Double-click Detection script on every Game Object? 1 Answer

moving a ball when touch 0 Answers

Rigidbody and raycasting 6 Answers

Multiple GUI textures one script 2 Answers

Display message when OnMouseDown 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges