Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ItemScript : MonoBehaviour {
  6.  
  7. public float distance = 1; // The trigger distance for the animation to start
  8. RectTransform rect;
  9. Animation anim;
  10.  
  11. void Start() {
  12. // Initialize components automatically
  13. rect = GetComponent<RectTransform>();
  14. anim = GetComponent<Animation>();
  15. }
  16.  
  17. void Update() {
  18. // Calculate the cursor distance to target object
  19. Vector3 target = rect.position;
  20. Vector3 mouse = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  21. float d = Vector2.Distance(new Vector2(mouse.x, mouse.y), new Vector2(target.x, target.y));
  22. if (anim.isPlaying) {
  23. // If too far away, stop playing the animation
  24. if (d > distance) {
  25. anim.Stop();
  26. Debug.Log("Stop");
  27. }
  28. } else {
  29. // If close enough, start the animation
  30. if (d < distance) {
  31. anim.Play();
  32. Debug.Log("Play");
  33. if (anim.isActiveAndEnabled && anim.gameObject.activeSelf) {
  34. Debug.Log("Play success");
  35. } else {
  36. Debug.Log("Play failure");
  37. }
  38. }
  39. }
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement