C# 将游戏对象旋转到特定点
在我的项目中,我有一座桥,当我与桥共线时,它会旋转,不会停止旋转,我想要的是通过一个特定的点旋转该桥,然后停止旋转,该桥在一个游戏对象内,这是我的枢轴点,通常我旋转枢轴点,而不是桥,所以我做了以下操作:C# 将游戏对象旋转到特定点,c#,unity3d,rotation,unity5,C#,Unity3d,Rotation,Unity5,在我的项目中,我有一座桥,当我与桥共线时,它会旋转,不会停止旋转,我想要的是通过一个特定的点旋转该桥,然后停止旋转,该桥在一个游戏对象内,这是我的枢轴点,通常我旋转枢轴点,而不是桥,所以我做了以下操作: using UnityEngine; using System.Collections; public class fallBridge : MonoBehaviour { private Rigidbody ball; public GameObject bridgePiv
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;
}
}
}
使用UnityEngine;
使用系统集合;
公共类fallBridge:单一行为{
私人刚体球;
公共游戏对象;
私人厕所;
私有布尔旋转=真;
//每帧调用一次更新
void Start(){
colided=false;
}
无效更新(){
如果(有结肠炎){
如果(旋转){
向量3到=新向量3(0,0,-85);
如果(矢量3.Distance(bridgePivot.transform.eulerAngles,to)>0.01f){
bridgePivot.transform.eulerAngles=Vector3.Lerp(bridgePivot.transform.rotation.eulerAngles,to,Time.deltaTime);
}否则{
bridgePivot.transform.eulerAngles=to;
旋转=假;
}
}
}
}
无效碰撞中心(碰撞其他)
{
ball=GameObject.FindWithTag(“玩家”).GetComponent();
if(other.gameObject.tag==“Player”&&ball.transform.localScale==新矢量3(2.0f、2.0f、2.0f)){
Debug.Log(“输入”);
colided=true;
}
}
}
我做错了什么??colision检测工作得很好,但在update方法中,它从不停止旋转:S您应该使用coroutine来完成此操作。调用coroutine函数,在调用OnCollisionCenter
时,传入要旋转的GameOjbect和要旋转的角度。您可以阅读此函数的工作原理
下面的示例将在3秒内将游戏对象在z轴上旋转-85度。
你可以把它改成任何适合你的
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;
}
public-class-fallBridge:monobhavior
{
私人刚体球;
公共游戏对象;
布尔旋转=假;
无效碰撞中心(碰撞其他)
{
ball=GameObject.FindWithTag(“玩家”).GetComponent();
if(other.gameObject.CompareTag(“Player”)&&ball.transform.localScale==newvector3(2.0f、2.0f、2.0f))
{
Debug.Log(“输入”);
矢量3旋转角度=新矢量3(0,0,-85);
Start例程(RotateObject(bridgePivot,rotationAngle,3f));
}
}
IEnumerator RotateObject(游戏对象gameObjectToMove、矢量3 eulerAngles、浮动持续时间)
{
如果(旋转)
{
屈服断裂;
}
旋转=真;
Vector3 newRot=gameObjectToMove.transform.eulerAngles+eulerAngles;
Vector3 currentRot=gameObjectToMove.transform.EuleRanges;
浮点计数器=0;
while(计数器<持续时间)
{
计数器+=时间增量时间;
gameObjectToMove.transform.eulerAngles=Vector3.Lerp(currentRot、newRot、计数器/持续时间);
收益返回空;
}
旋转=假;
}
错误在这一行:
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<Rigidbody> ();
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
}
}
}
public-class-fallBridge:monobhavior{
私人刚体球;
公共游戏对象;
公共浮点旋转=2;//时间(秒)
//开始旋转时,填充这三个变量
私人浮动开始时间;
来自的私有向量3;
私有布尔旋转=假;
//如果目标没有改变,你最好事先定义它
私有向量3到=新向量3(0,0,-85);
无效更新(){
如果(旋转){
浮动t=(Time.Time-startTime)/旋转;
if(t<1){
bridgePivot.transform.eulerAngles=Vector3.Lerp(from,to,t);
}否则{
bridgePivot.transform.eulerAngles=to;
旋转=假;
}
}
}
无效碰撞中心(碰撞其他)
{
ball=GameObject.FindWithTag(“玩家”).GetComponent();
if(other.gameObject.tag==“Player”&&ball.transform.localScale==新矢量3(2.0f、2.0f、2.0f)){
Debug.Log(“输入”);
startTime=Time.Time;//立即开始
from=bridgePivot.transform.eulerAngles;//记住起始位置
旋转=真;//开始旋转的信号
}
}
}
debug.log并查看为什么条件不满足if(Vector3.Distance(bridgePivot.transform.eulerAngles,to)>0.01f)