storing simple rotation and position properties

hey, as someone who just started coding, i’m still struggeling with some basic methods in unity.

i’ve been experimenting with some simple movements and rotations.

what i couldn’t figure out is how i can store a single rotation angle or also the angles of all three axis in variable
in order to rotate the object (cube for example) back to it.

for example:

a cube rotates around the x axis and moves foward in deltatime…
whenever i press the “y” key the current rotation status should be stored
whenever i press the “x” key the current position should be stored
whenever i press the “c” key the cube should simply move and rotate back to the stored statuses
whenever i press the “v” key the cube’ position should immediately go back to the stored statuses, e.c jump back to it…

it may sound simple for you but i simply can’t get it done… :slight_smile:

regards from germany!

This should be relatively straightforward:

using UnityEngine;

public class RotatingCubeExample : MonoBehaviour {

    public float lerpSpeed = 5f;

    private Vector3 _storedPosition;
    private Quaternion _storedRotation;

    // Need to lerp for key 'C'!
    private float _lerpTime = 1f;
    private Vector3 _lerpFromPosition;
    private Quaternion _lerpFromRotation;

    private void Update() {
        if (_lerpTime < 1f) {
            _lerpTime = Mathf.Min(1f, _lerpTime + lerpSpeed * Time.deltaTime);
            transform.localPosition = Vector3.Lerp(_lerpFromPosition, _storedPosition, _lerpTime);
            transform.localRotation = Quaternion.Lerp(_lerpFromRotation, _storedRotation, _lerpTime);
        }

        if (Input.GetKeyDown(KeyCode.Y)) {
            // Store current rotation.
            _storedRotation = transform.localRotation;
        }
        else if (Input.GetKeyDown(KeyCode.X)) {
            // Store current position.
            _storedPosition = transform.localPosition;
        }
        else if (Input.GetKeyDown(KeyCode.C)) {
            // Gradually tween back to stored position.
            _lerpTime = 0f;
            _lerpFromPosition = transform.localPosition;
            _lerpFromRotation = transform.localRotation;
        }
        else if (Input.GetKeyDown(KeyCode.V)) {
            // Immediately restore position and rotation.
            transform.localRotation = _storedRotation;
            transform.localPosition = _storedPosition;
        }
    }

}

Disclaimer: The above script has not been tested, but hopefully you will see the basic idea :slight_smile: