29 [Tooltip(
"The game object this object will follow : Main Camera by default")]
32 [Tooltip(
"Auto start? or status")]
33 public bool IsRunning =
false;
35 [Tooltip(
"translation speed : higher is faster")]
36 public float LerpPositionSpeed = 1f;
38 [Tooltip(
"rotation speed : higher is faster")]
39 public float LerpRotationSpeed = 0.5f;
41 [Tooltip(
"Lock the x axis if the object is set to face the reference object")]
42 public bool KeepUpRight =
false;
44 [Tooltip(
"An game object containing an Interactive component to call on air-tap")]
47 [Tooltip(
"Does not center the object to the reference object's transform.forward vector")]
48 public bool KeepStartingOffset =
true;
50 [Tooltip(
"Force the object to always face the reference object")]
51 public bool FaceObject =
true;
53 [Tooltip(
"Force the object to keep relative to the reference object's transform.forward")]
54 public bool KeepInFront =
true;
56 [Tooltip(
"Magnetism speed to move closer to the reference object")]
57 public float Magnetism = 0;
59 [Tooltip(
"Minimum distance to stay away from the reference object if magnetism is used")]
60 public float MagnetismPaddingDistance = 1f;
63 private Vector3 mOffsetDirection;
66 private Vector3 mDirection;
69 private Quaternion mOffsetRotation;
72 private float mOffsetDistance = 0;
75 private float mMagnetismPercentage = 1;
77 private Vector3 mNormalzedOffsetDirection;
84 if (ReferenceObject == null)
94 if (ReferenceObject == null)
97 mOffsetDirection = this.transform.position - ReferenceObject.transform.position;
98 mOffsetDistance = mOffsetDirection.magnitude;
99 mDirection = ReferenceObject.transform.forward.normalized;
100 mNormalzedOffsetDirection = mOffsetDirection.normalized;
101 mOffsetRotation = Quaternion.FromToRotation(mDirection, mNormalzedOffsetDirection);
104 mMagnetismPercentage = 1;
106 if (ReferenceInteractive != null)
120 if (ReferenceInteractive != null)
134 this.transform.position = Vector3.Lerp(this.transform.position, position, LerpPositionSpeed * time);
139 Quaternion forwardRotation = Quaternion.LookRotation(this.transform.position - ReferenceObject.transform.position);
140 this.transform.rotation = Quaternion.Lerp(this.transform.rotation, forwardRotation, LerpRotationSpeed * time);
146 Quaternion upRotation = Quaternion.FromToRotation(this.transform.up, Vector3.up);
147 this.transform.rotation = upRotation * this.transform.rotation;
159 Vector3 newDirection = ReferenceObject.transform.forward;
164 if (KeepStartingOffset)
166 newDirection = Vector3.Normalize(mOffsetRotation * ReferenceObject.transform.forward);
171 newDirection = mNormalzedOffsetDirection;
179 float magnetismDelta = MagnetismPaddingDistance / (mOffsetDistance * mMagnetismPercentage) - 1;
180 if (Mathf.Abs(magnetismDelta * 100) > 0.01f)
182 mMagnetismPercentage += Time.deltaTime * magnetismDelta * Magnetism;
186 Vector3 lerpPosition = ReferenceObject.transform.position + newDirection * mOffsetDistance * mMagnetismPercentage;
188 UpdatePosition(lerpPosition, Time.deltaTime);