将游戏对象旋转到特定点 [英] Rotating a gameobject to a specific point

查看:137
本文介绍了将游戏对象旋转到特定点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,当我碰到桥梁旋转且不停止旋转的那一刻,我有一座桥梁,我想要的是通过特定点旋转该桥梁并停止旋转,该桥梁位于游戏对象内部通常,这就是我的pivotPoint,我旋转pivotPoint而不是桥,所以我这样做了:

in my project i have a bridge at the moment when i colide with the bridge it rotates and don't stop rotate, what i want is to rotate that bridge trough a specific point and stop rotate, that bridge is inside a gameobject that is my pivotPoint in general i rotate the pivotPoint not the bridge, so i did this:

using UnityEngine;
using System.Collections;

public class fallBridge : MonoBehaviour {

    private Rigidbody ball;
    public GameObject bridgePivot;
    private bool colided; 
    private bool rotating = true;
    // Update is called once per frame

    void Start(){
        colided = false;
    }

    void Update () {
        if (colided) {
            if (rotating) {
                Vector3 to = new Vector3 (0, 0, -85);
                if (Vector3.Distance (bridgePivot.transform.eulerAngles, to) > 0.01f) {
                    bridgePivot.transform.eulerAngles = Vector3.Lerp (bridgePivot.transform.rotation.eulerAngles, to, Time.deltaTime);
                } else {
                    bridgePivot.transform.eulerAngles = to;
                    rotating = false;
                }
            }
        }
    }

    void OnCollisionEnter(Collision other)
    {
        ball = GameObject.FindWithTag ("Player").GetComponent<Rigidbody> ();
        if (other.gameObject.tag == "Player" && ball.transform.localScale == new Vector3(2.0f,2.0f,2.0f)) {
            Debug.Log("ENTER");
            colided = true;
        }
    }
}

我在做什么错?大肠菌检测工作正常,但在更新方法上,它永远不会停止旋转:S

what am i doing wrong?? the colision detection works perfectly but on the update method it never stops rotate :S

推荐答案

应该使用协同程序进行此操作.调用协程函数并传递GameOjbect以旋转,并在调用OnCollisionEnter时旋转到的角度.您可以在此处了解该功能的工作原理.

You should do this with coroutine. Call the coroutine function and pass in the GameOjbect to rotate and the angle to rotate to when OnCollisionEnter is called. You can read how this function work here.

Te示例将在3秒钟内沿z轴旋转GameObject -85度. 您可以将其更改为适合您的任何内容.

Te example below will rotate the GameObject -85 degree in z-axis within 3 seconds. You can change that to whatever suits you.

public class fallBridge : MonoBehaviour
{
    private Rigidbody ball;
    public GameObject bridgePivot;
    bool rotating = false;

    void OnCollisionEnter(Collision other)
    {
        ball = GameObject.FindWithTag("Player").GetComponent<Rigidbody>();
        if (other.gameObject.CompareTag("Player") && ball.transform.localScale == new Vector3(2.0f, 2.0f, 2.0f))
        {
            Debug.Log("ENTER");
            Vector3 rotationAngle = new Vector3(0, 0, -85);
            StartCoroutine(RotateObject(bridgePivot, rotationAngle, 3f));
        }
    }

IEnumerator RotateObject(GameObject gameObjectToMove, Vector3 eulerAngles, float duration)
{
    if (rotating)
    {
        yield break;
    }
    rotating = true;

    Vector3 newRot = gameObjectToMove.transform.eulerAngles + eulerAngles;

    Vector3 currentRot = gameObjectToMove.transform.eulerAngles;

    float counter = 0;
    while (counter < duration)
    {
        counter += Time.deltaTime;
        gameObjectToMove.transform.eulerAngles = Vector3.Lerp(currentRot, newRot, counter / duration);
        yield return null;
    }
    rotating = false;
}

这篇关于将游戏对象旋转到特定点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆