Reset cam position with double click

Hi, how can i reset the camera position to original value with pressing doubleclick using this script?

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class ArcBall : MonoBehaviour
{

    public float radius = 5.0f,
            minRadius = 1.5f,
            maxRadius = 10.0f,
            scale = 0.002f;
    public GameObject target;
    private float targetRadius = 8.0f,
            mouseX = 0.0f,
            mouseZ = 0.0f;
    private Vector3 up = new Vector3(0.0f, 1.0f, 0.0f),
            right = new Vector3(0.0f, 0.0f, 1.0f),
            newPosition = Vector3.zero;

    // Use this for initialization
    void Start()
    {
        this.transform.position = new Vector3(radius, 0.0f, 0.0f);
    }

    // Update is called once per frame
    void Update()
    {
        newPosition = transform.position;
        if (Input.GetMouseButton(1))
        {
            mouseX = Input.GetAxis("Mouse X");
            mouseZ = Input.GetAxis("Mouse Y");
        }
        else {
            mouseX = Mathf.Lerp(mouseX, 0.0f, 0.2f);
            mouseZ = Mathf.Lerp(mouseZ, 0.0f, 0.2f);
        }

        newPosition += right * mouseX * radius / 4.0f
                + up * mouseZ * -radius / 4.0f;
        newPosition.Normalize();
        right = Vector3.Cross(up, newPosition);
        up = Vector3.Cross(newPosition, right);

        right.Normalize();
        up.Normalize();

        if (Input.GetAxis("Mouse ScrollWheel") > 0.0f)
        {
            targetRadius = Mathf.Max(targetRadius / 1.1f, minRadius);
        }
        else if (Input.GetAxis("Mouse ScrollWheel") < 0.0f)
        {
            targetRadius = Mathf.Min(targetRadius * 1.1f, maxRadius);
        }
        radius = Mathf.Lerp(radius, targetRadius, 0.1f);
        newPosition.Normalize();
        transform.position = newPosition * radius;


        transform.LookAt(new Vector3(0.0f, 0.0f, 0.0f), up);
    }
}

Check out some tutorials on sensing doubleclick. Generally it involves NOT acting upon the first click until enough time has passed that you assume there can be no second click coming.

Once you have perfected that (in a nice separate simple script so that it outputs to Debug.Log() either “One click!” or “Doubleclick” and you are sure it works), then connect that user intention to whatever your consider this

to be in your context.

1 Like
public class ResetCamera : MonoBehaviour
{

    public Camera Camera;

    private float doubleClickStart = 0;

    private float doubleClickLapseTime = 0.3f;
    public Quaternion originalRotationValue;
    Vector3 startPos;



    void Start()
    {
        originalRotationValue = this.transform.rotation;
        startPos = this.transform.position;
    }
    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");
        this.transform.rotation = originalRotationValue;
        this.transform.position = startPos;

    }
    }

[/code]

Is anything external touching the public Quaternion originalRotationValue? Make it private, find out.

Beyond that, start putting in Debug.Log() calls to find exact numbers of what is being set when.

If that doesn’t cut it, then to help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

Also obviously this script MUST be on the Transform that you want driven back… is it on the Camera?

If you want this script to live somewhere else, you already have a reference to the Camera… use that to get at its local Transform and observe and set those values instead.