Search Unity

Reset Camera and GameObject to original view in game with button

Discussion in 'Scripting' started by kirstenmunro, Jul 25, 2017.

  1. kirstenmunro

    kirstenmunro

    Joined:
    Jul 25, 2017
    Posts:
    1
    Hi everyone

    I am developing an application for a Touch Table which allows you to view a 3D model from 360 degrees as well as move it around the screen and zoom in/out. I have all those functionalities set up but cannot for the life of me get a Reset button to work, where by it will return the camera and model to the original view.

    The Zoom function works by moving the camera so I have been able to get that to reset but rotate and translate are done by moving the model rather than the camera. The script I am working on is below:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class ResetCamera : MonoBehaviour {


    public Transform initialBoneParentTransform;
    public float initialFOV;

    public GameObject Skeleton;


    void Start()
    {

    initialBoneParentTransform = Skeleton.transform;
    initialFOV = Camera.main.fieldOfView;

    }

    public void ResetButton()
    {
    Skeleton.transform.position = initialBoneParentTransform.position;
    Skeleton.transform.rotation = initialBoneParentTransform.rotation;
    Camera.main.fieldOfView = initialFOV;
    }

    }

    I know this is really simple but I am a complete beginner and am totally stumped. I've assigned my GameObject in Unity and attached the ResetButton method to the appropriate button but all it will work on is the zoom.

    Any ideas much appreciated.

    Thanks

    Kirsten
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    If the transform that you dragged into the initialBoneParentTransform is the same one that is moving, this solution won't quite work: when you go to hit reset, it will have moved!

    When you get a reference to a Transform, that doesn't actually copy its contents: it just is a reference to the Transform object.

    What you may want to consider is at the Start() function copy out the position and rotation from that initial transform, then apply those values at reset.