Search Unity

Question HELP: 2D colliders and triggers, steep learning curve

Discussion in '2D' started by strangeboltz, Mar 17, 2023.

  1. strangeboltz

    strangeboltz

    Joined:
    Dec 14, 2022
    Posts:
    2

    ^^video of my projects facing issues

    I am new to Unity and having trouble on multiple projects facing the same issue.
    my colliders aren't recognizing one another when scripting them to trigger events.
    for example, my hummingbird is unable to collect the coin in one project.
    And my tightrope walker: the falling rocks pass right through and don't destroy.

    I'm not sure what I'm doing wrong?
    could someone lead me in the right direction / help me trouble shoot?
    ****************************************************************
    Heres the code I'm using for my hummingbird game:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class birdscript : MonoBehaviour
    8. {
    9.  
    10.     public Rigidbody2D myrigidbody;
    11.     public float flapStrength;
    12.  
    13. void Update()
    14.     {
    15.         {
    16.             if (Input.GetKeyDown(KeyCode.UpArrow))
    17.             {
    18.                 myrigidbody.velocity = Vector2.up * flapStrength;
    19.             }
    20.             else if (Input.GetKeyDown(KeyCode.RightArrow))
    21.             {
    22.                 myrigidbody.velocity = Vector2.right * flapStrength;
    23.             }
    24.             else if (Input.GetKeyDown(KeyCode.LeftArrow))
    25.             {
    26.                 myrigidbody.velocity = Vector2.left * flapStrength;
    27.             }
    28.             else if (Input.GetKeyDown(KeyCode.DownArrow))
    29.             {
    30.                 myrigidbody.velocity = Vector2.down * flapStrength;
    31.             }
    32.         }
    33.  
    34.     }
    35.  
    36.     private void OnTriggerEnter2D(Collider2D collision)
    37.     {
    38.         if (collision.gameObject.CompareTag("Coin"))
    39.         {
    40.             // Increase score or perform other actions
    41.             Destroy(collision.gameObject);
    42.         }
    43.     }
    44. }
    45.  
    46.  
    47. ----------------------------------------------------------------------
    48.  
    49. using System.Collections;
    50. using System.Collections.Generic;
    51. using UnityEngine;
    52. using UnityEngine.UI;
    53. using TMPro;
    54.  
    55. public class CoinGenerator : MonoBehaviour
    56. {
    57.     public GameObject coinPrefab; // The prefab for the coin
    58.     public int numCoins = 10; // The number of coins to generate
    59.     public float xMin = -5f; // The minimum x position of the coin
    60.     public float xMax = 5f; // The maximum x position of the coin
    61.     public float yMin = -3f; // The minimum y position of the coin
    62.     public float yMax = 3f; // The maximum y position of the coin
    63.     public float destroyDelay = 5f; // The delay in seconds before the coin is destroyed
    64.  
    65.     private int coinsGenerated = 0; // The number of coins generated so far
    66.  
    67.     void Start()
    68.     {
    69.         GenerateCoin();
    70.     }
    71.  
    72.     private void GenerateCoin()
    73.     {
    74.         if (coinsGenerated < numCoins)
    75.         {
    76.             // Generate a random position for the coin
    77.             float x = Random.Range(xMin, xMax);
    78.             float y = Random.Range(yMin, yMax);
    79.             Vector3 position = new Vector3(x, y, 0f);
    80.  
    81.             // Instantiate the coin prefab at the random position
    82.             GameObject coin = Instantiate(coinPrefab, position, Quaternion.identity);
    83.  
    84.             // Set the tag of the coin to "Coin"
    85.             coin.tag = "Coin";
    86.  
    87.             // Destroy the coin after a delay
    88.             Destroy(coin, destroyDelay);
    89.  
    90.             // Increment the number of coins generated
    91.             coinsGenerated++;
    92.  
    93.             // Call the GenerateCoin() method again after the delay
    94.             Invoke("GenerateCoin", destroyDelay);
    95.         }
    96.     }
    97. }




    *****************************************************************
    Heres the code I'm using for my tightrope game:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FallingRockGenerator : MonoBehaviour
    6. {
    7.     // The rock prefab to generate
    8.     public GameObject rockPrefab;
    9.  
    10.     // The minimum and maximum delay between rock generation
    11.     public float minDelay = 1f;
    12.     public float maxDelay = 3f;
    13.  
    14.     // The minimum and maximum horizontal position of generated rocks
    15.     public float minX = -5f;
    16.     public float maxX = 5f;
    17.  
    18.     // The speed at which rocks fall
    19.     public float fallSpeed = 5f;
    20.  
    21.     // Reference to the player character object
    22.     public GameObject player;
    23.  
    24.     // The duration in seconds before rocks are destroyed
    25.     public float rockDuration = 11f;
    26.  
    27.     void Start()
    28.     {
    29.         // Start generating rocks at random intervals
    30.         Invoke("GenerateRock", Random.Range(minDelay, maxDelay));
    31.     }
    32.  
    33.     void GenerateRock()
    34.     {
    35.         // Generate a new rock at a random horizontal position and set its fall speed
    36.         Vector3 rockPosition = new Vector3(Random.Range(minX, maxX), transform.position.y, 0);
    37.         GameObject newRock = Instantiate(rockPrefab, rockPosition, Quaternion.identity);
    38.         newRock.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -fallSpeed);
    39.  
    40.         // Destroy the rock after the specified duration
    41.         Destroy(newRock, rockDuration);
    42.  
    43.         // Schedule the next rock generation
    44.         Invoke("GenerateRock", Random.Range(minDelay, maxDelay));
    45.     }
    46.  
    47.     void OnTriggerEnter2D(Collider2D other)
    48.     {
    49.         GameObject player = GameObject.FindGameObjectWithTag("Player");
    50.         Debug.Log("Collision detected with " + other.gameObject.name);
    51.         // Check if the player has collided with a falling rock
    52.         if (other.CompareTag ("player"))
    53.         {
    54.             // Destroy the player and reset the game
    55.             Destroy (other.gameObject);
    56.             ResetGame();
    57.         }
    58.     }
    59.  
    60.     void ResetGame()
    61.     {
    62.         // Restart the game by reloading the current scene
    63.         UnityEngine.SceneManagement.SceneManager.LoadScene(UnityEngine.SceneManagement.SceneManager.GetActiveScene().buildIndex);
    64.     }
    65. }
    66.  
    67.  
    68. -------------------------------------------------------------------------------
    69.  
    70. using System.Collections;
    71. using System.Collections.Generic;
    72. using UnityEngine;
    73.  
    74. public class TightRopePlayerController : MonoBehaviour
    75. {
    76.  
    77.     public Rigidbody2D myrigidbody;
    78.     public float JumpStrength;
    79.  
    80.     void Update()
    81.     {
    82.         {
    83.             if (Input.GetKeyDown(KeyCode.UpArrow))
    84.             {
    85.                 myrigidbody.velocity = Vector2.up * JumpStrength;
    86.             }
    87.             else if (Input.GetKeyDown(KeyCode.RightArrow))
    88.             {
    89.                 myrigidbody.velocity = Vector2.right * JumpStrength;
    90.             }
    91.             else if (Input.GetKeyDown(KeyCode.LeftArrow))
    92.             {
    93.                 myrigidbody.velocity = Vector2.left * JumpStrength;
    94.             }
    95.             else if (Input.GetKeyDown(KeyCode.DownArrow))
    96.             {
    97.                 myrigidbody.velocity = Vector2.down * JumpStrength;
    98.             }
    99.         }
    100.  
    101.     }
    102. }
     
  2. strangeboltz

    strangeboltz

    Joined:
    Dec 14, 2022
    Posts:
    2
    I ended up figuring it out using ChatGPT