[英]Unity raycast possible bug?
this is my code for raycast in my school project game. 这是我在学校项目游戏中进行raycast的代码。 If I put script on object everything is working just fine. 如果我将脚本放在对象上,那么一切都很好。 But if I close Unity and reopen my project, the value of "jakDaleko" = distance stays locked on 1129.395 instead of changing every frame. 但是,如果我关闭Unity并重新打开项目,则“ jakDaleko” = distance的值将保持锁定在1129.395,而不是更改每个帧。
What should I change so it will work everytime and not just the first time ipress play button. 我应该更改什么,以便它每次都有效,而不仅仅是第一次按下播放按钮。
Here's my code. 这是我的代码。
script 1 = raycast 脚本1 = raycast
public class SmerDivani : MonoBehaviour {
public static float VzdalenostOdCile;
public float VzdalenostOdCileInterni;
// Update is called once per frame
void Update() {
RaycastHit Hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Hit)) {
VzdalenostOdCileInterni = Hit.distance;
VzdalenostOdCile = VzdalenostOdCileInterni;
}
}
} }
Second script 第二脚本
public class TabuleMesto1 : MonoBehaviour
{
public float JakDaleko;
public GameObject AkceTlacitko;
public GameObject AkceText;
public GameObject UIQuest;
public GameObject ThePlayer;
public GameObject NoticeCam;
void Update() {
JakDaleko = SmerDivani.VzdalenostOdCile;
}
void OnMouseOver() {
if (JakDaleko <= 5) {
AkceTlacitko.SetActive(true);
AkceText.SetActive(true);
}
if (JakDaleko > 5)
{
AkceTlacitko.SetActive(false);
AkceText.SetActive(false);
}
if (Input.GetButtonDown("Akce")) {
if (JakDaleko <= 5) {
AkceTlacitko.SetActive(false);
AkceText.SetActive(false);
UIQuest.SetActive(true);
NoticeCam.SetActive(true);
ThePlayer.SetActive(false);
}
}
}
void OnMouseExit() {
AkceTlacitko.SetActive(false);
AkceText.SetActive(false);
}
}
I'm not quite sure what you're trying to achieve? 我不太确定您要达到什么目标? Maybe this should "fix" your problem, you're not clearing the distance if the raycast don't hits.... 也许这应该可以“解决”您的问题,如果光线投射未命中,您并没有清除距离。
void Update() {
RaycastHit Hit;
if (Physics.Raycast(transform.position, transform.forward, out Hit)) {
VzdalenostOdCileInterni = Hit.distance;
}
else {
VzdalenostOdCileInterni = 0.0f;
}
VzdalenostOdCile = VzdalenostOdCileInterni;
}
Additionally I think you should use transform.forward instead of transform.TransformDirection(Vector3.forward) 另外,我认为您应该使用transform.forward而不是transform.TransformDirection(Vector3.forward)
I think that the biggest problem was the name of the file. 我认为最大的问题是文件名。 For some reason, my stupidity included, the solution was to rename the script from table1 to table_1 由于某种原因,包括我的愚蠢在内,解决方案是将脚本从table1重命名为table_1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.