unity3d 脚本private void OnCollisionEnter(Collision other) { if (other.gameObject.tag.Equals("Score")) { Destroy(other.gameObject); if (GetScore != null) { GetScore(1); } } }
时间: 2024-02-15 21:28:28 浏览: 75
这是一个碰撞检测的函数,当物体发生碰撞时会执行该函数。其中,other表示与当前物体碰撞的物体。如果碰撞的物体的Tag为“Score”,那么就销毁该物体,并且调用GetScore函数,该函数会将得分增加1分。
具体来说,这段代码会执行以下操作:
1. 判断碰撞的物体的Tag是否为“Score”。
2. 如果是,“销毁”该物体,即将其从场景中移除。
3. 调用GetScore函数,并将得分增加1分。
注意,这段代码中的GetScore函数并没有实现,需要在代码中定义该函数,并在其他地方调用它。
相关问题
public void OnCollisionEnter2D(Collision2D collision) { if(collision.gameObject.tag == "GoldCoin") { Destroy(collision.gameObject); sun = sun + 1; if (!audioSource.isPlaying) { audioSource.clip = shique; audioSource.loop = true; audioSource.Play(); } } else { audioSource.Stop(); } //判断是否遭受攻击 if (collision.gameObject.tag == "zhi") { Destroy(collision.gameObject); hp = hp - 1; } // 检测碰撞物体的标签 if (collision.gameObject.tag == "zhi2") { // 忽略该碰撞 Physics2D.IgnoreCollision(collision.collider, GetComponent<Collider2D>()); } }
这是一个在2D游戏中检测碰撞的函数。当游戏对象与另一个游戏对象发生碰撞时,该函数会被调用。在函数中,首先判断碰撞的游戏对象的标签是否为“GoldCoin”,如果是,则销毁该游戏对象,增加得分,并播放声音。如果不是,则停止播放声音。然后判断碰撞的游戏对象的标签是否为“zhi”,如果是,则销毁该游戏对象并减少血量。最后,如果碰撞的游戏对象的标签是“zhi2”,则忽略该碰撞。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class gameover : MonoBehaviour { GameObject overUI; // Start is called before the first frame update void Start() { overUI = GameObject.Find("over"); overUI.SetActive(false); } // Update is called once per frame void Update() { } private void OnTriggerEnter(Collider other) { if (other.name == "player") { Debug.Log("失败"); overUI.SetActive(true); Time.timeScale = 0; } } private void OnCollisionEnter(Collision collision) { if (collision.gameObject.name == "player") { //Debug.Log("碰到了Cube (2)"); //Destroy(collision.gameObject); Time.timeScale = 0; Debug.Log("碰到我了"); } } }
这是一个Unity游戏中的脚本代码,当游戏对象碰撞或触发某个物体时,该脚本会检测到,并执行相应的操作。在该脚本中,当玩家对象碰撞到某个物体时,游戏界面会显示"失败",并且暂停游戏。另外,当玩家对象进入某个触发器范围内时,也会执行类似的操作。
阅读全文
相关推荐













