Ich habe 10 Stunden damit verbracht, ein eigentlich einfaches 2D-Spiel in Unity zu erstellen, aber ich habe ständig Probleme mit der Kollisionserkennung. Obwohl ich die Tutorials befolgt und die Dokumentation überprüft habe, werden meine Kollisionen nicht wie erwartet erkannt. Ich habe verschiedene Ansätze ausprobiert, einschließlich der Verwendung von OnCollisionEnter2D und OnTriggerEnter2D, aber nichts scheint zu funktionieren. Ich habe auch die Collider, Rigidbody2D-Komponenten und Ebeneneinstellungen überprüft, aber ich stecke immer noch fest.
Das Problem besteht darin, dass sich ein Körper mit einem Collider von 70 x 70 buchstäblich vollständig in der Mitte von 10 befindet Der mal größere Collider Unity sieht nichts. Außerdem gibt es keine anderen Collider, die mit den kleineren kollidieren können.
Ich habe ein Skript implementiert, das Kollisionen zwischen einem Spielerobjekt und mehreren Zielobjekten (Löchern) erkennen soll. Ich habe erwartet, dass die OnCollisionEnter2D-Methode aufgerufen wird, wenn der Spieler mit diesen Zielen kollidiert, und dass entsprechend Punkte vergeben werden. Allerdings löst die Kollisionserkennung überhaupt nicht aus. Ich habe Debug-Protokolle hinzugefügt, um den Fluss des Codes zu verfolgen, aber die Protokolle zeigen an, dass die Kollisionsmethoden nie ausgelöst werden.
Ich habe auch eine einfache Testszene mit grundlegenden Formen erstellt Isolieren Sie das Problem, aber selbst das funktioniert nicht. Ich habe viel Zeit damit verbracht, dieses Problem zu beheben, und weiß nicht, was ich als Nächstes tun soll. Jede Hilfe wäre sehr dankbar! Hier ist mein Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class Chapter1Manager : MonoBehaviour
{
public Image crosshair; // Crosshair for aiming
public Image[] holes = new Image[10]; // Array to hold hole prefabs
public Canvas canvas; // Reference to the Canvas
public int bullets = 10; // Number of bullets available
public TextMeshProUGUI bulletsCount; // UI element to display bullet count
public Image blue; // Reference to the blue object
public int points = 0; // Player's score
public TextMeshProUGUI pointsText; // UI element to display points
public int toSend = 0; // Index to track which hole to send next
void Start()
{
// Initialize the bullet count display
bulletsCount.text = bullets.ToString();
// Find all GameObjects with the tag "hole" and store them in the holes array
GameObject[] foundHoles = GameObject.FindGameObjectsWithTag("hole");
Debug.Log("Found " + foundHoles.Length + " holes.");
// Assign found hole objects to the holes array
for (int i = 0; i < foundHoles.Length && i < holes.Length; i++)
{
holes = foundHoles.GetComponent();
Debug.Log("Assigned hole: " + holes.gameObject.name);
}
}
void Update()
{
// Update the points display
pointsText.text = "Points: " + points.ToString();
// Check if there are bullets left
if (bullets > 0)
{
// Check for space key press to shoot
if (Input.GetKeyDown(KeyCode.Space))
{
// Ensure we have holes left to send
if (toSend < holes.Length) // Check if toSend is in range
{
// Set the position of the current hole to the position of the crosshair
holes[toSend].gameObject.transform.position = crosshair.transform.position;
// Increment the index for the next hole and decrement the bullet count
toSend++;
bullets--;
bulletsCount.text = bullets.ToString(); // Update the bullet count display
Debug.Log("Shot fired! Bullets left: " + bullets);
}
else
{
Debug.Log("No more holes to send.");
}
}
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
// Log the name of the colliding object
Debug.Log("Collision detected with: " + collision.gameObject.name);
// Check if the collision is with the last hole sent or the blue object
if (toSend > 0 && (collision.gameObject == holes[toSend - 1].gameObject || collision.gameObject == blue.gameObject))
{
points += 10; // Increment points for a successful hit
Debug.Log("Hit! Points: " + points);
}
}
}
Unity sieht keine Kollisionen ⇐ C#
-
- Similar Topics
- Replies
- Views
- Last post