Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class ItemScript : MonoBehaviour {
- public float distance = 1; // The trigger distance for the animation to start
- RectTransform rect;
- Animation anim;
- void Start() {
- // Initialize components automatically
- rect = GetComponent<RectTransform>();
- anim = GetComponent<Animation>();
- }
- void Update() {
- // Calculate the cursor distance to target object
- Vector3 target = rect.position;
- Vector3 mouse = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- float d = Vector2.Distance(new Vector2(mouse.x, mouse.y), new Vector2(target.x, target.y));
- if (anim.isPlaying) {
- // If too far away, stop playing the animation
- if (d > distance) {
- anim.Stop();
- Debug.Log("Stop");
- }
- } else {
- // If close enough, start the animation
- if (d < distance) {
- anim.Play();
- Debug.Log("Play");
- if (anim.isActiveAndEnabled && anim.gameObject.activeSelf) {
- Debug.Log("Play success");
- } else {
- Debug.Log("Play failure");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement