How to bringback an object to its original position after rotation in unity?

I want the cube to return back to its original position when the user
stopped touching the cube

I can’t exactly tell which part of this you are struggling with but you can simply get the position of the GameObject in the Start or Awake function then set the transform.position to that value when OnMouseUp is called.

private Vector3 originalPos;

void Start()
{
  //Get the original position
  originalPos = transform.position;
}

void OnMouseUp()
{
    _isRotating = false;
    //Reset GameObject to the original position
    transform.position = originalPos;
}

EDIT:

For rotation, it is also the-same thing. Just use Quaternion and transform.rotation instead of Vector3 and transform.position.

private Quaternion originalPos;

void Start()
{
  //Get the original rotation
  originalPos = transform.rotation;
}

void OnMouseUp()
{
    _isRotating = false;
    //Reset GameObject to the original rotation
    transform.rotation = originalPos;
}

You still have to incorporate that into the original code from your answer. If this is something you can’t do then consider watching Unity’s scripting tutorial here.

Browse More Popular Posts

Leave a Comment