一个 if 语句可以有多个这样的条件吗?如果是,我怎样才能将它们分开? [关闭]

问题描述 投票:0回答:1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class soundspPlay : MonoBehaviour
{
    public AudioSource firstsound;
    public AudioSource secondsound;
    public bool alreadyPlayed = false;

    void OnTriggerEnter(Collider collider) 
    {
        if (collider.gameObject.tag == "Player" , !alreadyPlayed) 
        {
            firstsound.Play();
            secondsound.Play();
            alreadyPlayed = true; 
        }
    }
}

我需要两种声音在触发器上播放,但我只想播放一次,并且只有在带有“播放器”标签的对象进入时才播放。

c# unity3d
1个回答
2
投票

不,语法是:

if (collider.gameObject.tag == "Player" && !alreadyPlayed) 

if (collider.gameObject.tag == "Player" || !alreadyPlayed) 

&&
要求两个操作数都为真,
||
只需要一个操作数为真。

 true &&  true == true
 true && false == false
false && false == false
 true ||  true == true
 true || false == true
false || false == false
© www.soinside.com 2019 - 2024. All rights reserved.