统一旋转后如何将物体带回原来的位置?

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

我创建了一个项目,当我们触摸它时立方体会旋转。当用户停止触摸立方体时,我希望立方体 return 回到原来的位置。下面我添加了旋转立方体的源代码:

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;
    }

}

编辑代码:- 使用统一引擎; 使用 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 " 每当我停止触摸沙发时,它就会转向沙发的**背面”** 我希望它 return 初始旋转。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

我无法准确判断您遇到的问题是哪一部分,但您可以简单地在 StartAwake 函数中获取 GameObject 的位置,然后设置 transform.position 调用 OnMouseUp 时的那个值。

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;
}

编辑:

对于轮换,也是一样的道理。只需使用 Quaterniontransform.rotation 而不是 Vector3transform.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;
}

您仍然需要将其合并到您答案的原始代码中。如果这是您做不到的事情,请考虑观看 Unity 的脚本教程 here.