using System.Collections; using System.Collections.Generic; using UnityEngine; public…
- User
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BarrelController : MonoBehaviour { private bool canPhase = true; private Rigidbody2D rb;
private void Start() { rb = GetComponent<Rigidbody2D>(); } private void Update() { // Check if the barrel’s Y position falls below -10 if (transform.position.y < -10) { Destroy(gameObject); // Destroy the barrel GameObject } } private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("BarrelChute") && canPhase) { // Move down -1 unit transform.position = new Vector2(transform.position.x, transform.position.y - 1f); } if (collision.gameObject.layer == LayerMask.NameToLayer("MidBarrelChute") && canPhase) { float randomChance = Random.Range(0f, 1f); // Get a random value between 0 and 1 if (randomChance <= 0.5f) // 50% chance of not colliding { Physics2D.IgnoreCollision(collision.collider, GetComponent<Collider2D>(), true); // Ignore collision } else { // Move down -1 unit transform.position = new Vector2(transform.position.x, transform.position.y - 1f); } } }} edit this script so when the barrels collide with a "platform" the velocity is increased by 5 units
- Assistant
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BarrelController : MonoBehaviour { private bool canPhase = true; private Rigidbody2D rb;
private void Start() { rb = GetComponent<Rigidbody2D>(); } private void Update() { // Check if the barrel’s Y position falls below -10 if (transform.position.y < -10) { Destroy(gameObject); // Destroy the barrel GameObject } } private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("BarrelChute") && canPhase) { // Move down -1 unit transform.position = new Vector2(transform.position.x, transform.position.y - 1f); } if (collision.gameObject.layer == LayerMask.NameToLayer("MidBarrelChute") && canPhase) { float randomChance = Random.Range(0f, 1f); // Get a random value between 0 and 1 if (randomChance <= 0.5f) // 50% chance of not colliding { Physics2D.IgnoreCollision(collision.collider, GetComponent<Collider2D>(), true); // Ignore collision } else { // Move down -1 unit transform.position = new Vector2(transform.position.x, transform.position.y - 1f); } } if (collision.gameObject.CompareTag("Platform") && canPhase) { rb.velocity += new Vector2(0f, 5f); // Increase velocity by 5 units in the upward direction } }}
- User
increase it in either in the positive x direction or negative x direction based on what side the platform is facing
- Assistant
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BarrelController : MonoBehaviour { private bool canPhase = true; private Rigidbody2D rb;
private void Start() { rb = GetComponent<Rigidbody2D>(); } private void Update() { // Check if the barrel's Y position falls below -10 if (transform.position.y < -10) { Destroy(gameObject); // Destroy the barrel GameObject } } private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("BarrelChute") && canPhase) { // Move down -1 unit transform.position = new Vector2(transform.position.x, transform.position.y - 1f); } if (collision.gameObject.layer == LayerMask.NameToLayer("MidBarrelChute") && canPhase) { float randomChance = Random.Range(0f, 1f); // Get a random value between 0 and 1 if (randomChance <= 0.5f) // 50% chance of not colliding { Physics2D.IgnoreCollision(collision.collider, GetComponent<Collider2D>(), true); // Ignore collision } else { // Move down -1 unit transform.position = new Vector2(transform.position.x, transform.position.y - 1f); } } if (collision.gameObject.CompareTag("Platform") && canPhase) { float platformXPos = collision.gameObject.transform.position.x; if (platformXPos < transform.position.x) { // Increase velocity by 5 units in the negative x direction rb.velocity += new Vector2(-5f, 0f); } else { // Increase velocity by 5 units in the positive x direction rb.velocity += new Vector2(5f, 0f); } } }}