- Home /
How do I implement double click feature to move camera back to previous position?
So I have several objects, when I click on one object, the camera zooms in and moves to that object. Then I click on that object and the camera moves to another coordinate where another object is located. However, I would like the user to trace back to the original object and then back to the initial position of the camera. So, I was thinking about implementing a double click to have the camera move back to the original position.
I tried looking for a double click reference and I tried to attach this script to my object. However, nothing happens and I'm not sure why. I even set the camera to main camera.
using UnityEngine;
using System.Collections;
public class DoubleClickBack : MonoBehaviour {
public Camera mainCam;
float doubleClickStart = 0;
void OnMouseUp() {
if ((Time.time - doubleClickStart) < 0.3f) {
this.OnDoubleClick();
doubleClickStart = -1;
}
else {
doubleClickStart = Time.time;
}
}
void OnDoubleClick()
{
Debug.Log("Double Clicked!");
mainCam.transform.position = new Vector3(0, 1, -7);
Camera.main.orthographicSize = 100;
}
}
Yeah I did. I attached it the object that needed to be double clicked in order for the camera to move back to the previous position.
Should there be anything on the object besides a basic box collider, animator and the script?
I'm not exactly sure where your problem is. detecting the double click? moving the camera? is the "Double Clicked!" shown in the console? Cause I don't exactly understand your problem
I figured it out. I didn't call my function. It works now.
Answer by Eowyn27 · Jan 14, 2014 at 03:19 PM
I figured out what was wrong. I didn't call my function on Update()!
using UnityEngine;
using System.Collections;
public class DoubleClickBack : MonoBehaviour {
public Camera mainCam;
private float doubleClickStart = 0;
private float doubleClickLapseTime = 0.3f;
void Update() {
CheckDoubleClick();
}
void CheckDoubleClick()
{
if(Input.GetMouseButtonUp(0)) {
if ((Time.time - doubleClickStart) < doubleClickLapseTime)
{
this.OnDoubleClick();
doubleClickStart = -1;
}
else
{
doubleClickStart = Time.time;
}
}
}
void OnDoubleClick()
{
Debug.Log ("Double Click");
mainCam.transform.position = new Vector3(-0.13f, 0.87f, -8);
Camera.main.orthographicSize = 0.4f;
}
}
Needed to call the function.... and I added GetMouseButtonUp() instead of Down().
Your answer
Follow this Question
Related Questions
How can I produce the typical FPS mouse steering effect? 2 Answers
Mouse Follow Script Help 1 Answer
Camera Movement issue 1 Answer
Move the camera when the mouse reaches the edges of the screen 1 Answer
Select/Drag/Drop Objects with a mouse 0 Answers