如何使特定的预制件在按下按钮时旋转?

时间:2018-12-12 00:15:34

标签: unity3d

我正在设计一个游戏,在我目前正在设计的场景中,大约有100个预制件。 enter image description here

  

上图显示了对象通常的外观。

我想要做的是当我按下红色按钮时,我希望中间的汽车模型旋转。 通过debug.Log,我了解到当我按下F(use)时,我希望该特定对象旋转。

到目前为止,我在代码方面所做的是:

        private void SearchForObject()
        {
            if (!m_ItemCoolDown)
            {
                if (m_Target != null)
                {
                    if (m_Target.tag == m_InteractWithObjectsTag)
                    {
                        if (m_WeaponUI != null)
                            m_WeaponUI.ShowPickupMessage("PRESS <color=#FF9200FF>" + m_UseKey + "</color> TO INTERACT WITH THE OBJECT");

                        if (InputManager.GetButtonDown("Use"))
                        {


                            Debug.Log("Button pressed");
                        }
                    }
                }
            }
        }

但是我如何使代码意识到我想要旋转该对象?

  

还有大约100个这样的对象,当我转到它们并按F时,我也希望它们旋转。

我当时想创建一个脚本,并将其附加到按钮上,然后将汽车模型附加到该脚本上,如下所示: enter image description here

当我按F时,我希望脚本转到其他脚本,抓住特定的模型然后旋转。但是我不确定该怎么做,有人知道吗?

谢谢

1 个答案:

答案 0 :(得分:0)

我认为您的脚本基本上可以完成它的工作。

我会接受您已经提到的内容,然后让按钮本身存储对其目标对象的引用并处理旋转。

对于轮换,您可以使用简单的Coroutine

public class ButtonScript : MonoBehaviour
{
    // The object that should rotate
    public Transform ObjectToTransform;

    // How long it should take to rotate in seconds
    public float RotationDuration = 1;

    // flag for making sure you can start rotating only if not already rotating
    private bool _isRotating;

    public void StartRotating()
    {
        // if already rotating do nothing
        if(_isRotating) return;

        StartCoroutine(Rotate());
    }

    // Rotates the ObjectToTransform 360° around its world up vector
    // in RotationDuration seconds
    private IEnumerator Rotate()
    {
        // set the flag so rotation can not be started multiple times
        _isRotating= true;

        // store the original rotation the target object currently has
        var initialRotation = ObjectToTransform.rotation;

        // we can already calculate the rotation speed in angles / second
        var rotationspeed = 360 / RotationDuration;

        // Rotate the object until the given duration is reached
        var timePassed = 0;    
        while(timePassed < RotationDuration)
        {
            // Time.deltaTime gives you the time passed since the last frame in seconds 
            // So every frame turn the object around world Y axis a little
            ObjectToTransform.Rotate(0, rotationspeed  * Time.deltaTime, 0, Space.World);

            // add the time passed since the last frame to the timePassed            
            timePassed += Time.deltaTime;

            // yield makes the Courutine interupt here so the frame can be rendered 
            // but the it "remembers" at which point it left
            // so in the next frame it goes on executing the code from the next line
            yield return null;
        }

        // Just to be sure we end with a clean rotation reset to the initial rotation in the end
        // since +360°  
        ObjectToTransform.rotation = initialRotation;

        // reset the flag so rotation can be started again
        _isRotating= false;
    }
}

所以我还假设m_Target是您要按下的特定“按钮”,而现在剩下的就是在该按钮上启动协同程序:

private void SearchForObject()
{
    if (!m_ItemCoolDown)
    {
        if (m_Target != null)
        {
            if (m_Target.tag == m_InteractWithObjectsTag)
            {
                if (m_WeaponUI != null)
                    m_WeaponUI.ShowPickupMessage("PRESS <color=#FF9200FF>" + m_UseKey + "</color> TO INTERACT WITH THE OBJECT");

                if (InputManager.GetButtonDown("Use"))
                {
                    Debug.Log("Button pressed");

                    // if m_Target is not the button you'll have to get a reference
                    // to the button somehow
                    m_Target.GetComponent<ButtonScript>.StartRotating();
                }
            }
        }
    }
}

然后在检查器中将targetObject的引用设置为应该旋转的对象。或者,如果它是初始化的预制件,则可以执行类似的操作

var obj = Initialize(prefab /*, parameters if you have some*/);
// ButtonObject is the reference to your button GameObject
ButtonObject.GetComponent<ButtonScript>().targetObject = obj.transform;