Hello, newbie here.
I’m trying to develop a hyper-casual game, pretty simple, where two spheres rotate around the axis-Z, trying to collect good objects and avoid the bad ones.
I couldn’t make both of the circles rotate around as one, so I created a ‘cube’, stretched it and disabled the mesh renderer in it, however, it works, please see the screenshots.
However, when one of the spheres collides with a ‘bad’ object, I want both to be destroyed, and an explosion particle from both spheres.
The problem is, I couldn’t make both of them destroy AND play the particle animation at the same time. I believe its a problem with them being two different objects as player. So, I’ve tried tagging them “Player” and destroy the objects with the tag “Player” in OnCollisionEnter, but this time, I can see the animation, but only one of them gets destroyed(only the one collides with a bad object). My code is below, so is the video.
gi6ram
using System;
using System.Collections;
using System.Collections.Generic;
using JetBrains.Annotations;
using TMPro;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
using UnityEngine.SocialPlatforms.Impl;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
public float rotationSpeed = 50f; // Speed of rotation
private bool clockwise = true; // Flag to track rotation direction
public AudioClip tapSound;
public AudioClip crashSound;
public AudioClip pointSound;
public ScoreManager scoreManager;
public ParticleSystem explosionParticle;
public ParticleSystem explosionParticle2;
private AudioSource playerAudio;
// Start is called before the first frame update
void Start()
{
playerAudio = GetComponent<AudioSource>();
RotateCube();
}
// Update is called once per frame
void Update()
{
// Check for mouse click
if (Input.GetMouseButtonDown(0))
{
playerAudio.PlayOneShot(tapSound, 1f);
// Change rotation direction
clockwise = !clockwise;
// Rotate the cube
RotateCube();
}
}
void RotateCube()
{
// Determine rotation direction based on the flag
float rotationDirection = clockwise ? 1f : -1f;
// Apply rotation to the cube around its Z-axis
GetComponent<Rigidbody>().angularVelocity = Vector3.forward * rotationSpeed * rotationDirection;
}
public void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Bad"))
{
Destroy(other.gameObject);
explosionParticle.Play();
explosionParticle2.Play();
playerAudio.PlayOneShot(crashSound, 1f);
Debug.Log("Game Over!");
}
else if (other.gameObject.CompareTag("Good"))
{
playerAudio.PlayOneShot(pointSound, 1f);
Destroy(other.gameObject);
scoreManager.UpdateScore();
}
}
}
