Liberi
An exergame built for kids with CP!
DirectionUtils.cs
1 using UnityEngine;
2 using System;
3 using Lidgren.Network;
4 using Janus;
5 
9 public static class DirectionUtils
10 {
14  public static Vector3 GetRadialDirection (this Transform transform)
15  {
16  return transform.up;
17  }
18 
22  public static Vector3 GetRadialDirection (this Rigidbody rigidbody)
23  {
24  return rigidbody.transform.GetRadialDirection();
25  }
26 
30  public static Vector3 GetDirection (this Transform transform)
31  {
32  Sync sync = transform.GetComponent<Sync>();
33 
34  if (sync != null)
35  return sync.GetDirection();
36  else return transform.GetRadialDirection();
37  }
38 
42  public static Vector3 GetDirection (this Rigidbody rigidbody)
43  {
44  Sync sync = rigidbody.GetComponent<Sync>();
45 
46  if (sync != null)
47  return sync.GetDirection();
48  else return rigidbody.GetRadialDirection();
49  }
50 
54  public static Vector3 GetFlipDirection (this Transform transform)
55  {
56  return transform.localScale.x < 0 ? Vector3.left : Vector3.right;
57  }
58 
62  public static Vector3 GetFlipDirection (this Rigidbody rigidbody)
63  {
64  return rigidbody.transform.GetFlipDirection();
65  }
66 
70  public static void SetRadialDirection (this Transform transform, Vector3 direction)
71  {
72  transform.rotation = direction.Get2DRotation();
73  }
74 
78  public static void SetRadialDirection (this Rigidbody rigidbody, Vector3 direction)
79  {
80  rigidbody.MoveRotation(direction.Get2DRotation());
81  }
82 
86  public static void SetFlipDirection (this Transform transform, Vector3 direction)
87  {
88  var scale = transform.localScale;
89  scale.x = Mathf.Abs(scale.x) * Mathf.Sign(direction.x);
90 
91  if (scale != transform.localScale)
92  {
93  transform.localScale = scale;
94  // This is to fix a stupid Unity glitch where if an object is flipped, the animation
95  // mixing matrices do not update appropriately. This line forces the update.
96  transform.localScale = transform.localScale;
97  }
98  }
99 
103  public static void SetFlipDirection (this Rigidbody rigidbody, Vector3 direction)
104  {
105  rigidbody.transform.SetFlipDirection(direction);
106  }
107 
111  public static void SetDirection (this Transform transform, Vector3 direction)
112  {
113  Sync sync = transform.GetComponent<Sync>();
114 
115  if (sync != null)
116  sync.SetDirection(direction, true);
117  else transform.SetRadialDirection(direction);
118  }
119 
123  public static void SetDirection (this Rigidbody rigidbody, Vector3 direction)
124  {
125  Sync sync = rigidbody.GetComponent<Sync>();
126 
127  if (sync != null)
128  sync.SetDirection(direction);
129  else rigidbody.SetRadialDirection(direction);
130  }
131 
135  public static void RollToward (this Transform transform, Vector3 direction, float t)
136  {
137  transform.rotation = transform.up.RollToward(direction, t).Get2DRotation();
138  }
139 
143  public static void RollToward (this Rigidbody rigidbody, Vector3 direction, float t)
144  {
145  rigidbody.MoveRotation(rigidbody.transform.up.RollToward(direction, t).Get2DRotation());
146  }
147 }
This class server two main functions: 1) As a MonoBehaviour, it allows for network synchronization of...
Definition: Sync.cs:13