简体   繁体   中英

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

I have created a project where a cube is rotated when we touch on it. I want the cube to return back to its original position when the user stops touching the cube. Below I have added the source code of rotating a cube:

using UnityEngine;

using System.Collections;

[RequireComponent(typeof(MeshRenderer))]

public class dr : MonoBehaviour 
{

    #region ROTATE
    private float _sensitivity = 1f;
    private Vector3 _mouseReference;
    private Vector3 _mouseOffset;
    private Vector3 _rotation = Vector3.zero;
    private bool _isRotating;


    #endregion

    void Update()
    {
        if(_isRotating)
        {
            // offset
            _mouseOffset = (Input.mousePosition - _mouseReference); // apply rotation
            _rotation.y = -(_mouseOffset.x + _mouseOffset.y) * _sensitivity; // rotate
            gameObject.transform.Rotate(_rotation); // store new mouse position
            _mouseReference = Input.mousePosition;
        }

    }

    void OnMouseDown()
    {
        // rotating flag
        _isRotating = true;

        // store mouse position
        _mouseReference = Input.mousePosition;
    }

    void OnMouseUp()
    {
        // rotating flag
        _isRotating = false;
    }

}

edited code:- using UnityEngine; using System.Collections;

[RequireComponent(typeof(MeshRenderer))]

public class pt : MonoBehaviour {

#region ROTATE
private float _sensitivity = 1f;
private Vector3 _mouseReference;
private Vector3 _mouseOffset;
private Vector3 _rotation = Vector3.zero;
private bool _isRotating;
private Quaternion original;


#endregion
void start(){
    original = transform.rotation;
}

void Update()
{
    if(_isRotating)
    {
        // offset
        _mouseOffset = (Input.mousePosition - _mouseReference); // apply rotation
        _rotation.y = -(_mouseOffset.x + _mouseOffset.y) * _sensitivity; // rotate
        gameObject.transform.Rotate(_rotation); // store new mouse position
        _mouseReference = Input.mousePosition;
    }

}

public void OnMouseDown()
{
    // rotating flag
    _isRotating = true;

    // store mouse position
    _mouseReference = Input.mousePosition;
}

public void OnMouseUp()
{
    // rotating flag
    _isRotating = false;
    transform.rotation = original;
}

}

"im trying to rotate a 3d model of a sofa and return to its starting rotation.but if i used this code " whenever if i stopped touching the sofa , it turns to **backside of sofa"** i want it to return to initial rotation. u can see initally this is how the sofa looks like and if i stopped touching it returns to its backside of sofa. i want it to return to its front side again if i stopped rotation

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM