A 2D Farming RPG
This is just a simple 2D farming RPG game
Loading...
Searching...
No Matches
ItemNudge.cs
Go to the documentation of this file.
1using System.Collections;
2using UnityEngine;
3
8public class ItemNudge : MonoBehaviour
9{
10 private WaitForSeconds m_Pause;
11
12 private bool m_IsAnimating = false;
13
14 private void Awake()
15 {
16 m_Pause = new WaitForSeconds( 0.04f );
17 }
18
23 private void OnTriggerEnter2D( Collider2D collision )
24 {
25 if( m_IsAnimating == false )
26 {
27 if( gameObject.transform.position.x < collision.gameObject.transform.position.x )
28 {
29 StartCoroutine( RotateAntiClock() );
30 }
31 else
32 {
33 StartCoroutine( RotateClock() );
34 }
35 }
36 }
37
42 private void OnTriggerExit2D( Collider2D collision )
43 {
44 if ( m_IsAnimating == false )
45 {
46 if( gameObject.transform.position.x > collision.gameObject.transform.position.x )
47 {
48 StartCoroutine( RotateAntiClock() );
49 }
50 else
51 {
52 StartCoroutine( RotateClock() );
53 }
54 }
55 }
56
64 private IEnumerator RotateAntiClock()
65 {
66 m_IsAnimating = true;
67
68 for( int i = 0; i < 4; i++ )
69 {
70 gameObject.transform.GetChild(0).Rotate( 0f, 0f, 2f );
71 yield return m_Pause;
72 }
73
74 for( int i = 0; i < 5; i++ )
75 {
76 gameObject.transform.GetChild(0).Rotate( 0f, 0f, -2f );
77 yield return m_Pause;
78 }
79
80 gameObject.transform.GetChild(0).Rotate( 0f, 0f, 2f );
81 yield return m_Pause;
82
83 m_IsAnimating = false;
84
85 }
86
94 private IEnumerator RotateClock()
95 {
96 m_IsAnimating = true;
97
98 for( int i = 0; i < 4; i++ )
99 {
100 gameObject.transform.GetChild(0).Rotate( 0f, 0f, -2f );
101 yield return m_Pause;
102 }
103
104 for( int i = 0; i < 5; i++ )
105 {
106 gameObject.transform.GetChild(0).Rotate( 0f, 0f, 2f );
107 yield return m_Pause;
108 }
109
110 gameObject.transform.GetChild(0).Rotate( 0f, 0f, -2f );
111 yield return m_Pause;
112
113 m_IsAnimating = false;
114 }
115}
Create a class that create nudge effect on grass and some other items. When player get near items,...
Definition: ItemNudge.cs:9
bool m_IsAnimating
Definition: ItemNudge.cs:12
IEnumerator RotateAntiClock()
Rotate the gameObject anti clock wise. We rotate it by 2 in 4 steps Then we rotate it in the opposite...
Definition: ItemNudge.cs:64
void OnTriggerExit2D(Collider2D collision)
Detect and starts coroutines when player exit from the collider that this bject is attached to
Definition: ItemNudge.cs:42
void Awake()
Definition: ItemNudge.cs:14
WaitForSeconds m_Pause
Definition: ItemNudge.cs:10
IEnumerator RotateClock()
Rotate the gameObject clock wise. We rotate it by 2 in 4 steps Then we rotate it in the opposite dire...
Definition: ItemNudge.cs:94
void OnTriggerEnter2D(Collider2D collision)
Detect and starts coroutines when player collided with an object that this script is attached to
Definition: ItemNudge.cs:23