gpt4 book ai didi

c# - 盒子对撞机没有捕捉到鼠标按钮按下事件

转载 作者:太空宇宙 更新时间:2023-11-03 12:41:38 25 4
gpt4 key购买 nike

我有一个带有 2 个碰撞器的橱柜 - 一个用于橱柜,一个用于盒子。当我按下盒子时,我想打开/关闭它。它工作正常,但现在由于某种原因它只在我按下盒子的边缘时才起作用。当点击中心时,它不起作用。

视频:https://youtu.be/OozsAi7KNzs

这是当我按下盒子时播放动画(打开/关闭橱柜)的代码:

public Animation[] animations;
public string[] animationName;
public bool playOneDirection; // should revert animation speed after second playing?
public AudioSource myAudioOpen;
public AudioSource myAudioClose;

private bool isDoorClosed;
private bool isAimationReadyToPlay = true;
private Collider thisCollider;

public void Start()
{
thisCollider = GetComponent<Collider>();
}

void Update ()
{
if (Input.GetButton("Fire1"))
if(DoPlayerLookAtButton() && isAimationReadyToPlay)
OpenCloseDoor();
}

bool DoPlayerLookAtButton()
{
RaycastHit _hit;
Ray _ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
bool isHit = Physics.Raycast(_ray, out _hit, 1.5f);

if (isHit && _hit.collider == thisCollider) return true;
else return false;
}

public void OpenCloseDoor()
{
if (!isDoorClosed) // Play animation with normal speed
{
myAudioOpen.Play();
for (int i = 0; i < animations.Length; i++)
{
animations[i][animationName[i]].speed = 1.0f;
animations[i].Play();
}
}

if(playOneDirection)
return;

if (isDoorClosed) // Play animation with revert speed
{
myAudioClose.Play();
for (int i = 0; i < animations.Length; i++)
{
animations[i][animationName[i]].speed = -1.0f;
animations[i][animationName[i]].time = animations[i][animationName[i]].length;
animations[i].Play();
}
}

StartCoroutine("DelayBetweenAnimations");
isDoorClosed = !isDoorClosed;
}

IEnumerator DelayBetweenAnimations()
{
isAimationReadyToPlay = false;
yield return new WaitForSeconds(0.5f);
isAimationReadyToPlay = true;
}

最佳答案

你的橱柜有 2 个碰撞器,但你只检查其中一个。如果有某种重叠,那么单击正确的重叠可能会很麻烦。如果您只想能够单击游戏对象的任意位置,请像这样更改您的代码...

//From
//if (isHit && _hit.collider == thisCollider) return true;
//To
if (isHit && _hit.transform.gameObject == this.gameObject) return true;

为您的播放器添加图层蒙版并确保您的 Physics.Raycast 排除该图层,以避免转换击中您自己。 See here

关于c# - 盒子对撞机没有捕捉到鼠标按钮按下事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38823428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com