Collisions, Bools, and states... What am I doing wrong here?

So at this point in the game the player has two hands they control and needs to get them onto coins to collect them. That part is easy and works. However, some coins are double coins and connected to each other and you have to touch them at the same time with each hand. But the script isn’t working as intended…

And sorry, “I dont code” I hack together BS from tutorials I find while pounding my head on the keyboard. I know its horrible!

using UnityEngine;

public class DoubleCoin : MonoBehaviour
{
    public float floatSpeed = 4f;  // Speed of the floating animation
    public float floatHeight = 0.06f;  // Height of the floating animation
    public float floatOffset = 1f;  // Time offset for the starting position of float animation
    public float collectHeight = 0.8f;  // Height the coin reaches on collection
    public float collectDuration = 0.23f;  // Duration of the collection animation
    public GameObject coin1;
    public GameObject coin2;

    private float initialY;  // Initial Y position for offset

 // Track if both hands have touched the coins
     private bool coin1HandDetected = false;
    private bool coin2HandDetected = false;

    void Start()
    {
        // Store the initial Y position
        initialY = transform.position.y;
    }

    void Update()
    {
        // Add a floating animation to the coin
        FloatAnimation();
    }

     void OnTriggerEnter(Collider other)
    {
        // Check if the entering object has the "Hand" tag
        if (other.CompareTag("Hand"))
        {
            // Check if coin was triggered
            if (other.gameObject == coin1)
            {
                coin1HandDetected = true;
            }
            if (other.gameObject == coin2)
            {
                coin2HandDetected = true;
            }
        

            // If both coins are touched by hands, collect them
            if (coin1HandDetected && coin2HandDetected)
            {
                CollectCoin();
            }
        }
    }

    void OnTriggerExit(Collider other)
    {
        // If a hand leaves the coin area, reset detection
        if (other.CompareTag("Hand"))
        {
            if (other.gameObject == coin1)
            {
                coin1HandDetected = false;
            }
            if (other.gameObject == coin2)
            {
                coin2HandDetected = false;
            }
        }
    }

        void CollectCoin()
    {
        // Start the collection animation
        StartCoroutine(CollectionAnimation());
    }

    void FloatAnimation()
    {
        // Add a simple up and down floating animation with offset
        float offsetTime = Time.time + floatOffset;
        transform.position = new Vector3(
            transform.position.x,
            initialY + Mathf.Sin(offsetTime * floatSpeed) * floatHeight,
            transform.position.z
        );
    }

    System.Collections.IEnumerator CollectionAnimation()
    {
        // Move the coin upwards on collection
        float startY = transform.position.y;
        float targetY = initialY + collectHeight;
        float startTime = Time.time;

        while (Time.time < startTime + collectDuration)
        {
            float t = (Time.time - startTime) / collectDuration;
            transform.position = new Vector3(transform.position.x, Mathf.Lerp(startY, targetY, t), transform.position.z);
            yield return null;
        }

        // Destroy both coins after the collection animation
        Destroy(coin1);
        Destroy(coin2);
        Destroy(gameObject);
    }
}

Coding is optional. Debugging is not optional. :slight_smile:

As you’ve seen from me before… it just …

Sounds like you wrote a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.