I have been banging my head against this one for a while. I am trying to implement a pickup mechanic, using OnTriggerEnter2D, however I cannot get it to work, my player just runs past the coin.
Code:
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using TMPro;
using UnityEngine;
public class gemMovement : MonoBehaviour
{
// variables
private Vector2 startPosition;
private Vector2 endPosition;
private int speed = 4;
private float maxDistance = 0.15f;
private int count = 0;
public TextMeshProUGUI countText;
private void Start()
{
startPosition = transform.position;
endPosition = transform.position;
}
private void Update()
{
endPosition.y = startPosition.y + (maxDistance * Mathf.Sin(Time.time * speed));
transform.position = endPosition;
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Pickup"))
{
Debug.Log("Pickup");
other.gameObject.SetActive(false);
count++;
SetCountText();
}
}
}
A little bit of help would be very much appreciated, thank you.