2024-03-13 12:28:28 +01:00

71 lines
1.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CollisionDetection : MonoBehaviour
{
public string particle;
private Atom atomScript;
public GameObject sphere;
public float speed;
private Rigidbody sphereRb;
private bool hasCollided = true;
private Vector3 chasingPosition;
void Start()
{
sphereRb = GetComponent<Rigidbody>();
}
void Update()
{
if (hasCollided)
{
float step = speed * Time.deltaTime; // calculate distance to move
transform.position = Vector3.MoveTowards(transform.position, chasingPosition, step);
// Check if we have reached the target position
if (transform.position == chasingPosition)
{
Debug.Log("Position reached. Position: " + transform.position + " -- CP: "+ chasingPosition);
Destroy(gameObject);
atomScript.AddParticel(particle);
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Cube"))
{
Debug.Log("Collision detected with cube!");
Debug.Log("Particle: " + particle);
hasCollided = true;
atomScript = GameObject.Find("Atom").GetComponent<Atom>();
if (particle == "electron")
chasingPosition = atomScript.getShellPosition();
else
chasingPosition = atomScript.getNucleusPosition();
Debug.Log("Chasing position: " + chasingPosition);
Destroy(sphereRb);
Collider[] colliders = GetComponents<Collider>();
foreach (Collider collider in colliders)
{
Destroy(collider);
}
}
if (collision.gameObject.CompareTag("Ground"))
{
Destroy(sphere);
}
}
}