123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Collectable_Script : MonoBehaviour
- {
- public int scoreValue = 100;
- public int healthValue = 0;
- public int lifeValue = 0;
- public bool floating = true;
- private float startY;
- void Start()
- {
- startY = gameObject.transform.position.y;
- }
- // Update is called once per frame
- void Update()
- {
- Rigidbody2D rigidbody = gameObject.GetComponent<Rigidbody2D>();
- if (gameObject.transform.position.y > startY + 10)
- {
- Destroy(gameObject);
- }
- if (Vector2.Distance(gameObject.transform.position, GameObject.FindWithTag("Player").transform.position) < 15)
- {
- if (floating)
- {
- if (gameObject.transform.position.y < startY + 0.333)
- {
- gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.up * Random.Range(0.1f, 1.0f));
- }
- else if (gameObject.transform.position.y > startY - 0.333)
- {
- gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.down * Random.Range(0.1f, 1.0f));
- }
- }
- }
- else if (gameObject.GetComponent<Collider2D>().enabled)
- {
- gameObject.transform.position = new Vector2(gameObject.transform.position.x, startY);
- }
- }
- }
|