为什么当移动玩家从平台上掉下来,而平台却在向上移动时,父母却没有影响
本文关键字:平台 移动 有影响 父母 玩家 为什么 | 更新日期: 2023-09-27 18:17:58
在脚本中,这是父元素在OnTriggerEnter函数中的原始位置:
using UnityEngine;
using System.Collections;
using System.Reflection;
public class DetectPlayer : MonoBehaviour {
GameObject target;
public void ClearLog()
{
var assembly = Assembly.GetAssembly(typeof(UnityEditor.ActiveEditorTracker));
var type = assembly.GetType("UnityEditorInternal.LogEntries");
var method = type.GetMethod("Clear");
method.Invoke(new object(), null);
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Platform")
{
Debug.Log("Touching Platform");
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "OnTop Detector")
{
Debug.Log("On Top of Platform");
target = GameObject.Find("Elevator");
GameObject findGo = GameObject.Find("ThirdPersonController");
GameObject findGo1 = GameObject.Find("Elevator");
findGo.transform.parent = findGo1.transform;
StartCoroutine(playAnim(target));
}
}
IEnumerator playAnim(GameObject target)
{
Animation anim = target.GetComponent<Animation>();
int counter = 0;
foreach (AnimationState clip in anim)
{
// do initialisation or something on clip
clip.speed = 1;
}
while (true)
{
if (counter == 10)
break;
//Play Up Anim
anim.Play("Up");
//Wait until Up is done Playing the play down
while (anim.IsPlaying("Up"))
{
yield return null;
}
//Now Play Down Anim
anim.Play("Down");
//Wait until down is done Playing
while (anim.IsPlaying("Down"))
{
yield return null;
}
yield return null; //Make sure there is no freezing
//Return to the top of the while|true loop
counter++;
}
}
}
这是在我从OnTriggerEnter中删除父部分并将其放入我创建的Start函数之后:
using UnityEngine;
using System.Collections;
using System.Reflection;
public class DetectPlayer : MonoBehaviour {
GameObject target;
void Start()
{
GameObject findGo = GameObject.Find("ThirdPersonController");
GameObject findGo1 = GameObject.Find("Elevator");
findGo.transform.parent = findGo1.transform;
}
public void ClearLog()
{
var assembly = Assembly.GetAssembly(typeof(UnityEditor.ActiveEditorTracker));
var type = assembly.GetType("UnityEditorInternal.LogEntries");
var method = type.GetMethod("Clear");
method.Invoke(new object(), null);
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Platform")
{
Debug.Log("Touching Platform");
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "OnTop Detector")
{
Debug.Log("On Top of Platform");
target = GameObject.Find("Elevator");
StartCoroutine(playAnim(target));
}
}
IEnumerator playAnim(GameObject target)
{
Animation anim = target.GetComponent<Animation>();
int counter = 0;
foreach (AnimationState clip in anim)
{
// do initialisation or something on clip
clip.speed = 1;
}
while (true)
{
if (counter == 10)
break;
//Play Up Anim
anim.Play("Up");
//Wait until Up is done Playing the play down
while (anim.IsPlaying("Up"))
{
yield return null;
}
//Now Play Down Anim
anim.Play("Down");
//Wait until down is done Playing
while (anim.IsPlaying("Down"))
{
yield return null;
}
yield return null; //Make sure there is no freezing
//Return to the top of the while|true loop
counter++;
}
}
}
在这两种情况下,当玩家在平台和"在平台的顶部"时,它都可以正常工作。
问题是,当平台上下移动时,我移动玩家从平台上掉到地上,然后一切都在口吃/摇晃。它只发生在平台上下移动时,当它上下移动时,我将玩家移出平台。
一旦平台完成上下移动10次后,一切都恢复正常。我试图将Parent部分移动到Start函数,但它没有帮助。当玩家离开升降平台/电梯时,仍然会出现口吃/颤抖
这是我录制的一个小视频剪辑,显示了这个问题:
视频片段结巴
解决方案是保留最初的原始代码。然后添加OnTriggerExit函数并取消游戏对象的父属性:
void OnTriggerExit(Collider other)
{
GameObject findGo = GameObject.Find("ThirdPersonController");
findGo.transform.parent = null;
}