我试图使碰撞工作的游戏对象克隆,但没有工作
新水瓶我想碰撞的球员一样,原来的水瓶预制,但不工作,新水瓶是产卵完美,但碰撞不工作
Game Files
下面是代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using TMPro;
public class GameManager : MonoBehaviour
{
public Image healthBar;
public float healthAmount = 100f;
// get WaterBottle game Tag
public GameObject waterBottlePrefab;
// get player game Tag
public GameObject player;
public GameObject scoreText;
int score = 0;
TextMeshProUGUI scoreText_test;
// Start is called before the first frame update
void Start()
{
scoreText_test=scoreText.GetComponent<TextMeshProUGUI>();
StartCoroutine(SpawnWaterBottles());
}
// Update is called once per frame
void Update()
{
if (healthAmount <= 0){
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
// if player detect collision with waterBottle display in console "You drink water"
if (player.GetComponent<Collider2D>().IsTouching(waterBottlePrefab.GetComponent<Collider2D>()))
{
score += 1;
Debug.Log("You drink water");
Heal();
// set active false the waterBottle
waterBottlePrefab.SetActive(false);
}
else
{
// From the begin of the game, the player will be take automatically 1 damage per second
TakeDamge(5f * Time.deltaTime);
}
scoreText_test.SetText(score.ToString());
}
IEnumerator SpawnWaterBottles()
{
while (true)
{
// Wait for a random interval of time between 10 and 20 seconds
yield return new WaitForSeconds(Random.Range(5, 15));
// Spawn a new water bottle at a random distance between 30 and 40 meters in front of the player
Vector3 spawnPos = player.transform.position + (player.transform.forward * Random.Range(30f, 40f) + new Vector3(40f, 2f, 0));
GameObject newWaterBottle = Instantiate(waterBottlePrefab, spawnPos, Quaternion.identity);
newWaterBottle.SetActive(true);
scoreText_test.SetText(score.ToString());
// Player colision with newWaterBottle
if (player.GetComponent<Collider2D>().IsTouching(newWaterBottle.GetComponent<Collider2D>()))
{
score += 1;
Debug.Log("You drink waterson");
Heal();
// set active false the waterBottle
newWaterBottle.SetActive(false);
}
else
{
// From the begin of the game, the player will be take automatically 1 damage per second
TakeDamge(5f * Time.deltaTime);
}
}
}
// Coroutine to spawn water bottles along the road at ground level
// Method to heal the player
void Heal()
{
// Increase the health amount by 10
healthAmount += 100;
// Clamp the health amount to 100
healthAmount = Mathf.Clamp(healthAmount, 0, 100);
// Update the health bar UI
healthBar.fillAmount = healthAmount / 100f;
}
// Method to take damage from the player
void TakeDamge(float amount)
{
// Decrease the health amount by the amount passed in
healthAmount -= amount;
// Clamp the health amount to 100
healthAmount = Mathf.Clamp(healthAmount, 0, 100);
// Update the health bar UI
healthBar.fillAmount = healthAmount / 100f;
}
}
这就是“克隆人碰撞”发生的地方
if (player.GetComponent<Collider2D>().IsTouching(newWaterBottle.GetComponent<Collider2D>()))
{
score += 1;
Debug.Log("You drink waterson");
Heal();
// set active false the waterBottle
newWaterBottle.SetActive(false);
}
else
{
// From the begin of the game, the player will be take automatically 1 damage per second
TakeDamge(5f * Time.deltaTime);
}
我试过Debug.log(),我想这是设置活动或得分的问题,但没有。
1条答案
按热度按时间2w2cym1i1#
看起来您试图在代码中检测播放器和水瓶对象之间的冲突。您可以检查以下几项来解决此问题:
我希望这些建议能帮助您解决代码中的冲突检测问题。如果您还有其他问题或需要进一步的帮助,请告诉我。