如何在统一旋转后将物体恢复到原来的位置?

时间:2017-05-23 10:56:33

标签: c# unity3d rotation mouseevent rotatetransform

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

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

}

编辑的代码: - 使用UnityEngine; 使用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;
}

}

“即时试图旋转沙发的3D模型并返回其开始旋转。但如果我使用此代码”,无论何时我停止触摸沙发,它都会转向**背面沙发”**  我希望它返回初始轮换。http://myfaces.apache.org/core22/

1 个答案:

答案 0 :(得分:7)

  

我希望立方体在用户时返回其原始位置   停止触摸立方体

我无法确切地说出你正在努力解决这个问题的哪一部分,但你可以简单地在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