11using UnityEngine ;
22using System . Collections ;
3- using System . Collections . Generic ;
43
54public class CloudController : MonoBehaviour
65{
7- public Transform playerTransform ;
6+ public Transform playerTransform ;
87 public Transform cloudMeshTransform ;
98 public Transform floraPrefab ;
109 public LayerMask floraRaycastMask = - 1 ;
1110 public Material happyCloudMat ;
1211 public Material rainyCloudMat ;
12+ public float moveVelocity = 100 ;
13+ public float moveAwayForce = 1 ;
14+ public float lookAtPlayerForce = 1 ;
15+ public float pullVelocity = 10000 ;
16+ public float minCloudScale = 0.1f ;
17+ public float minStartScale = 0.75f ;
18+ public float maxStartScale = 1f ;
19+ public float minShrinkSpeed = 0.2f ;
20+ public float maxShrinkSpeed = 0.5f ;
1321
1422 [ HideInInspector ]
15- public Transform cloudColliderCenter ;
23+ private float shrinkSpeed ;
1624 [ HideInInspector ]
17- public float runThreshold ;
25+ private float cloudScale ;
1826 [ HideInInspector ]
19- public float runVelocity ;
20- [ HideInInspector ]
21- public float normalXVelocity ;
22- [ HideInInspector ]
23- public float normalZVelocity ;
24- [ HideInInspector ]
25- public float pullVelocity ;
26-
27- [ HideInInspector ]
28- public static List < CloudController > rainingClouds = new List < CloudController > ( ) ;
27+ private Transform cloudColliderCenter ;
2928
3029 public enum CloudState
3130 {
@@ -37,35 +36,42 @@ public enum CloudState
3736 [ HideInInspector ]
3837 public CloudState state ;
3938
40- private int flip ;
41-
4239 // Use this for initialization
4340 void Start ( )
4441 {
45- particleSystem . enableEmission = false ;
4642 state = CloudState . Normal ;
47- flip = 1 ;
43+ particleSystem . enableEmission = false ;
44+ ResetVelocity ( ) ;
45+
46+ shrinkSpeed = Random . Range ( minShrinkSpeed , maxShrinkSpeed ) ;
47+ cloudScale = Random . Range ( minStartScale , maxStartScale ) ;
48+ transform . localScale = new Vector3 ( cloudScale , cloudScale , cloudScale ) ;
49+ Quaternion newRot = Quaternion . Euler ( 0 , Random . Range ( 0 , 360f ) , 0 ) ;
50+ transform . rotation = newRot ;
4851 }
52+
53+ void ResetVelocity ( )
54+ {
55+ rigidbody . velocity = new Vector3 ( Random . Range ( - moveVelocity , moveVelocity ) , 0 , Random . Range ( - moveVelocity , moveVelocity ) ) ;
56+ }
4957
5058 // Update is called once per frame
5159 void Update ( )
5260 {
53- if ( state == CloudState . Normal )
54- {
55- Vector3 deltaPosition = transform . position - playerTransform . position ;
56- if ( deltaPosition . magnitude < runThreshold )
57- {
58- deltaPosition . Normalize ( ) ;
59- deltaPosition . y = 0.0f ;
60- rigidbody . velocity = deltaPosition * runVelocity ;
61- }
62- else
63- {
64-
65- rigidbody . velocity = new Vector3 ( flip * normalXVelocity , 0 , flip * normalZVelocity ) ;
66- }
67- }
68- else if ( state == CloudState . Rain && cloudColliderCenter != null )
61+ if ( GameController . state != GameController . GameState . play ) return ;
62+
63+ if ( state == CloudState . Normal )
64+ {
65+ Quaternion rotation = Quaternion . LookRotation ( playerTransform . position - transform . position ) ;
66+ rotation . x = 0 ;
67+ rotation . z = 0 ;
68+ transform . rotation = Quaternion . Slerp ( transform . rotation , rotation , Time . deltaTime * lookAtPlayerForce ) ;
69+
70+
71+ Vector3 playerDir = transform . position - playerTransform . position ;
72+ rigidbody . AddForce ( playerDir . normalized * moveAwayForce ) ;
73+ }
74+ else if ( state == CloudState . Rain )
6975 {
7076 // Add force to keep cloud within cloudCollider
7177 float colliderRadius = cloudColliderCenter . localScale . x ;
@@ -90,6 +96,18 @@ void Update()
9096 GameController . score ++ ;
9197 }
9298 }
99+
100+ // Shrink cloud
101+ if ( cloudScale > minCloudScale )
102+ {
103+ cloudScale -= Time . deltaTime * shrinkSpeed ;
104+ transform . localScale = new Vector3 ( cloudScale , cloudScale , cloudScale ) ;
105+ }
106+ else
107+ {
108+ CloudManager . rainingClouds . Remove ( this ) ;
109+ Destroy ( gameObject ) ;
110+ }
93111 }
94112 }
95113
@@ -111,9 +129,9 @@ public void SetDeadState()
111129
112130 void OnCollisionEnter ( Collision collision )
113131 {
114- if ( collision . gameObject . name . Equals ( "WallPlane " ) )
132+ if ( collision . gameObject . tag . Equals ( "WallTag " ) )
115133 {
116- flip *= - 1 ;
134+ ResetVelocity ( ) ;
117135 }
118136 }
119137
@@ -127,7 +145,7 @@ void OnTriggerEnter(Collider collision)
127145 cloudMeshTransform . GetComponent < SkinnedMeshRenderer > ( ) . material = rainyCloudMat ;
128146 particleSystem . enableEmission = true ;
129147 cloudColliderCenter = collision . transform ;
130- rainingClouds . Add ( this ) ;
148+ CloudManager . rainingClouds . Add ( this ) ;
131149 }
132150 }
133151 }
@@ -138,8 +156,8 @@ void OnTriggerExit(Collider collision)
138156 {
139157 if ( state == CloudState . Rain )
140158 {
141- SetDeadState ( ) ;
142- rainingClouds . Remove ( this ) ;
159+ SetNormalState ( ) ;
160+ CloudManager . rainingClouds . Remove ( this ) ;
143161 }
144162 }
145163 }
0 commit comments