unity3d Unity 2D中克隆游戏对象的问题

zwghvu4y  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(338)

我试图使碰撞工作的游戏对象克隆,但没有工作
新水瓶我想碰撞的球员一样,原来的水瓶预制,但不工作,新水瓶是产卵完美,但碰撞不工作
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(),我想这是设置活动或得分的问题,但没有。

2w2cym1i

2w2cym1i1#

看起来您试图在代码中检测播放器和水瓶对象之间的冲突。您可以检查以下几项来解决此问题:

  • 确保玩家和水瓶对象都有碰撞器。没有碰撞器,对象将无法检测到彼此的碰撞。
  • 确保玩家和水瓶碰撞器设置在正确的碰撞层上。如果它们在不同的碰撞层上,它们将无法检测到彼此的碰撞。
  • 确保播放器和水瓶碰撞器设置为正确的碰撞检测模式。默认的碰撞检测模式是“离散”,它只检查一帧结束时的碰撞。如果你想连续检测碰撞,你可以将碰撞检测模式设置为“连续”。
  • 请确保玩家和水瓶对象的位置足够接近,以便实际碰撞。如果它们相距太远,碰撞器将不会相交,也不会检测到碰撞。
  • 确保玩家和水瓶对象的移动或旋转方式允许碰撞器相交。如果对象的移动或旋转方式阻止碰撞器相交,则不会检测到碰撞。

我希望这些建议能帮助您解决代码中的冲突检测问题。如果您还有其他问题或需要进一步的帮助,请告诉我。

相关问题