将游戏对象旋转到特定点

在我的项目中我有一座桥,此刻我与它旋转的桥并且不停止旋转,我想要的是通过特定点旋转该桥并停止旋转,该桥是在我的游戏对象内pivotPoint一般我旋转pivotPoint而不是桥,所以我这样做:

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 (); if (other.gameObject.tag == "Player" && ball.transform.localScale == new Vector3(2.0f,2.0f,2.0f)) { Debug.Log("ENTER"); colided = true; } } } 

我究竟做错了什么?? colision检测工作完美,但在更新方法上它永远不会停止旋转:S

应该用coroutine做到这一点。 调用coroutine函数并传入GameOjbect进行旋转,并在调用OnCollisionEnter时旋转角度。 您可以在此处阅读此function的工作原理。

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

 public class fallBridge : MonoBehaviour { private Rigidbody ball; public GameObject bridgePivot; bool rotating = false; void OnCollisionEnter(Collision other) { ball = GameObject.FindWithTag("Player").GetComponent(); 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; } 

错误在这一行:

 bridgePivot.transform.eulerAngles = Vector3.Lerp (bridgePivot.transform.rotation.eulerAngles, to, Time.deltaTime); 

当你使用Lerp时,你通常有一个固定的开始和结束,并且你使最后一个参数(t)从0变为1.在你的情况下,端点是固定的,t参数也是固定的,你可以改变你的开始。 这意味着每次更新都会朝着您的目标迈出一小步,但这一步是百分比。 所以每次更新都会越来越近,下一步就会变小。

你应该做的是记住你开始时的位置,以及你何时开始。 您还需要为桥梁定义速度。 然后你可以从头到尾移动桥,你可以控制速度。

 public class fallBridge : MonoBehaviour { private Rigidbody ball; public GameObject bridgePivot; public float rotateDuration = 2; // Time in seconds // When you begin rotating you fill these three variables private float startTime; private Vector3 from; private bool rotating = false; // If the target doesn't change you might as well define it beforehand private Vector3 to = new Vector3 (0, 0, -85); void Update () { if (rotating) { float t = (Time.time - startTime) / rotateDuration; if (t < 1) { bridgePivot.transform.eulerAngles = Vector3.Lerp (from, to, t); } else { bridgePivot.transform.eulerAngles = to; rotating = false; } } } void OnCollisionEnter(Collision other) { ball = GameObject.FindWithTag ("Player").GetComponent (); if (other.gameObject.tag == "Player" && ball.transform.localScale == new Vector3(2.0f,2.0f,2.0f)) { Debug.Log("ENTER"); startTime = Time.time; // Begin now from = bridgePivot.transform.eulerAngles; // Remember the start position rotating = true; // Signal to start rotating } } }