我剛開始編碼,所以我還有很多東西要學。Unity 在我玩游戲時不會出現任何錯誤,但當玩家觸摸金幣時也沒有任何反應。我希望金幣在游戲觸及時消失,但我不知道為什么它不起作用。(碰撞部分為最后部分)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private Rigidbody2D myRigidbody;
private Animator anim;
[SerializeField]
private int speed;
private bool lookright;
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
lookright = true;
}
void Update()
{
float horizontal = Input.GetAxis("Horizontal");
movements(horizontal);
changedirection(horizontal);
}
private void movements (float horizontal)
{
anim.SetFloat ("Walk", Mathf.Abs(horizontal));
myRigidbody.velocity = new Vector2 (horizontal*speed, myRigidbody.velocity.y);
}
private void changedirection(float horizontal)
{
if (horizontal > 0 && !lookright || horizontal < 0 && lookright)
{
lookright = !lookright;
Vector3 direction = transform.localScale;
direction.x *= -1;
transform.localScale = direction;
}
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "gold")
{
other.gameObject.SetActive(false);
}
} }
這是黃金的 Unity 檢查員
uj5u.com熱心網友回復:
OnCollisionEnter2D(Collision collision)
IsTrigger
設定為時不會觸發true
。將其更改為OnTriggerEnter2D(Collider2D collider)
,它將起作用。
查看colliders的檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/519993.html
上一篇:Unity2D中的回應式