forked from nope5166/AtomCraft-2.28
105 lines
3.2 KiB
C#
105 lines
3.2 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 = false;
|
|
private Vector3 chasingPosition;
|
|
private GameObject rightHand;
|
|
private ButtonManager buttonManager;
|
|
|
|
void Start()
|
|
{
|
|
sphereRb = GetComponent<Rigidbody>();
|
|
rightHand = GameObject.FindWithTag("RightHand");
|
|
Debug.Log("RightHand = " + rightHand);
|
|
atomScript = GameObject.Find("Atom").GetComponent<Atom>();
|
|
buttonManager = GameObject.Find("ButtonManager").GetComponent<ButtonManager>();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (Input.GetKey(KeyCode.Space)) //grabbing the ball
|
|
{
|
|
Debug.Log("Space pressed");
|
|
|
|
sphereRb.useGravity = false;
|
|
Collider[] colliders = GetComponents<Collider>();
|
|
foreach (Collider collider in colliders)
|
|
{
|
|
collider.enabled = false;
|
|
}
|
|
|
|
|
|
chasingPosition = rightHand.transform.position;
|
|
Debug.Log("Chasing position " + chasingPosition);
|
|
transform.position = Vector3.MoveTowards(transform.position, chasingPosition, speed * Time.deltaTime);
|
|
}
|
|
else if (Input.GetKeyUp(KeyCode.Space))
|
|
{
|
|
// sphereRb.useGravity = true;
|
|
// Collider[] colliders = GetComponents<Collider>();
|
|
// foreach (Collider collider in colliders)
|
|
// {
|
|
// collider.enabled = true;
|
|
// }
|
|
hasCollided = true;
|
|
buttonManager.setEnableSpawn(true);
|
|
}
|
|
|
|
if (hasCollided)
|
|
{
|
|
// Read the target position
|
|
if (particle == "electron")
|
|
chasingPosition = atomScript.getLatestElectronPosition();//.getShellPosition();
|
|
else
|
|
chasingPosition = atomScript.getNucleusPosition();
|
|
|
|
// Debug.Log("Chasing position: " + chasingPosition);
|
|
|
|
transform.position = Vector3.MoveTowards(transform.position, chasingPosition, speed * Time.deltaTime);
|
|
|
|
// Check if we have reached the target position
|
|
if (Vector3.Distance(transform.position,chasingPosition) <= 0.01f)
|
|
{
|
|
// 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;
|
|
|
|
sphereRb.useGravity = false;
|
|
|
|
Collider[] colliders = GetComponents<Collider>();
|
|
foreach (Collider collider in colliders)
|
|
{
|
|
Destroy(collider);
|
|
}
|
|
buttonManager.setEnableSpawn(true);
|
|
}
|
|
|
|
if (collision.gameObject.CompareTag("Ground"))
|
|
{
|
|
Destroy(sphere);
|
|
buttonManager.setEnableSpawn(true);
|
|
}
|
|
}
|
|
}
|