1. using UnityEngine;
    2. using System.Collections;
    3. [RequireComponent(typeof(CharacterController))]
    4. [AddComponentMenu("Character/Character Motor")]
    5. public class CharacterMotor : MonoBehaviour {
    6. // Does this script currently respond to input?
    7. public bool canControl = true;
    8. public bool useFixedUpdate = true;
    9. // For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.
    10. // Very handy for organization!
    11. // The current global direction we want the character to move in.
    12. [System.NonSerialized]
    13. public Vector3 inputMoveDirection = Vector3.zero;
    14. // Is the jump button held down? We use this interface instead of checking
    15. // for the jump button directly so this script can also be used by AIs.
    16. [System.NonSerialized]
    17. public bool inputJump = false;
    18. [System.Serializable]
    19. public class CharacterMotorMovement
    20. {
    21. // The maximum horizontal speed when moving
    22. public float maxForwardSpeed = 10.0f;
    23. public float maxSidewaysSpeed = 10.0f;
    24. public float maxBackwardsSpeed = 10.0f;
    25. // Curve for multiplying speed based on slope (negative = downwards)
    26. public AnimationCurve slopeSpeedMultiplier = new AnimationCurve(new Keyframe(-90, 1), new Keyframe(0, 1), new Keyframe(90, 0));
    27. // How fast does the character change speeds? Higher is faster.
    28. public float maxGroundAcceleration = 30.0f;
    29. public float maxAirAcceleration = 20.0f;
    30. // The gravity for the character
    31. public float gravity = 10.0f;
    32. public float maxFallSpeed = 20.0f;
    33. // For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.
    34. // Very handy for organization!
    35. // The last collision flags returned from controller.Move
    36. [System.NonSerialized]
    37. public CollisionFlags collisionFlags;
    38. // We will keep track of the character's current velocity,
    39. [System.NonSerialized]
    40. public Vector3 velocity;
    41. // This keeps track of our current velocity while we're not grounded
    42. [System.NonSerialized]
    43. public Vector3 frameVelocity = Vector3.zero;
    44. [System.NonSerialized]
    45. public Vector3 hitPoint = Vector3.zero;
    46. [System.NonSerialized]
    47. public Vector3 lastHitPoint = new Vector3(Mathf.Infinity, 0, 0);
    48. }
    49. public CharacterMotorMovement movement = new CharacterMotorMovement();
    50. public enum MovementTransferOnJump {
    51. None, // The jump is not affected by velocity of floor at all.
    52. InitTransfer, // Jump gets its initial velocity from the floor, then gradualy comes to a stop.
    53. PermaTransfer, // Jump gets its initial velocity from the floor, and keeps that velocity until landing.
    54. PermaLocked // Jump is relative to the movement of the last touched floor and will move together with that floor.
    55. }
    56. // We will contain all the jumping related variables in one helper class for clarity.
    57. [System.Serializable]
    58. public class CharacterMotorJumping {
    59. // Can the character jump?
    60. public bool enabled = true;
    61. // How high do we jump when pressing jump and letting go immediately
    62. public float baseHeight = 1.0f;
    63. // We add extraHeight units (meters) on top when holding the button down longer while jumping
    64. public float extraHeight = 4.1f;
    65. // How much does the character jump out perpendicular to the surface on walkable surfaces?
    66. // 0 means a fully vertical jump and 1 means fully perpendicular.
    67. public float perpAmount = 0.0f;
    68. // How much does the character jump out perpendicular to the surface on too steep surfaces?
    69. // 0 means a fully vertical jump and 1 means fully perpendicular.
    70. public float steepPerpAmount = 0.5f;
    71. // For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.
    72. // Very handy for organization!
    73. // Are we jumping? (Initiated with jump button and not grounded yet)
    74. // To see if we are just in the air (initiated by jumping OR falling) see the grounded variable.
    75. [System.NonSerialized]
    76. public bool jumping = false;
    77. [System.NonSerialized]
    78. public bool holdingJumpButton = false;
    79. // the time we jumped at (Used to determine for how long to apply extra jump power after jumping.)
    80. [System.NonSerialized]
    81. public float lastStartTime = 0.0f;
    82. [System.NonSerialized]
    83. public float lastButtonDownTime = -100f;
    84. [System.NonSerialized]
    85. public Vector3 jumpDir = Vector3.up;
    86. }
    87. public CharacterMotorJumping jumping = new CharacterMotorJumping();
    88. [System.Serializable]
    89. public class CharacterMotorMovingPlatform {
    90. public bool enabled = true;
    91. public MovementTransferOnJump movementTransfer = MovementTransferOnJump.PermaTransfer;
    92. [System.NonSerialized]
    93. public Transform hitPlatform;
    94. [System.NonSerialized]
    95. public Transform activePlatform;
    96. [System.NonSerialized]
    97. public Vector3 activeLocalPoint;
    98. [System.NonSerialized]
    99. public Vector3 activeGlobalPoint;
    100. [System.NonSerialized]
    101. public Quaternion activeLocalRotation;
    102. [System.NonSerialized]
    103. public Quaternion activeGlobalRotation;
    104. [System.NonSerialized]
    105. public Matrix4x4 lastMatrix;
    106. [System.NonSerialized]
    107. public Vector3 platformVelocity;
    108. [System.NonSerialized]
    109. public bool newPlatform;
    110. }
    111. public CharacterMotorMovingPlatform movingPlatform = new CharacterMotorMovingPlatform();
    112. [System.Serializable]
    113. public class CharacterMotorSliding {
    114. // Does the character slide on too steep surfaces?
    115. public bool enabled = true;
    116. // How fast does the character slide on steep surfaces?
    117. public float slidingSpeed = 15f;
    118. // How much can the player control the sliding direction?
    119. // If the value is 0.5 the player can slide sideways with half the speed of the downwards sliding speed.
    120. public float sidewaysControl = 1.0f;
    121. // How much can the player influence the sliding speed?
    122. // If the value is 0.5 the player can speed the sliding up to 150% or slow it down to 50%.
    123. public float speedControl = 0.4f;
    124. }
    125. public CharacterMotorSliding sliding = new CharacterMotorSliding();
    126. [System.NonSerialized]
    127. public bool grounded = true;
    128. [System.NonSerialized]
    129. public Vector3 groundNormal = Vector3.zero;
    130. private Vector3 lastGroundNormal = Vector3.zero;
    131. private Transform tr;
    132. private CharacterController controller ;
    133. void Awake () {
    134. controller = GetComponent <CharacterController>();
    135. tr = transform;
    136. }
    137. private void UpdateFunction () {
    138. // We copy the actual velocity into a temporary variable that we can manipulate.
    139. Vector3 velocity = movement.velocity;
    140. // Update velocity based on input
    141. velocity = ApplyInputVelocityChange(velocity);
    142. // Apply gravity and jumping force
    143. velocity = ApplyGravityAndJumping (velocity);
    144. // Moving platform support
    145. Vector3 moveDistance = Vector3.zero;
    146. if (MoveWithPlatform()) {
    147. Vector3 newGlobalPoint = movingPlatform.activePlatform.TransformPoint(movingPlatform.activeLocalPoint);
    148. moveDistance = (newGlobalPoint - movingPlatform.activeGlobalPoint);
    149. if (moveDistance != Vector3.zero)
    150. controller.Move(moveDistance);
    151. // Support moving platform rotation as well:
    152. Quaternion newGlobalRotation = movingPlatform.activePlatform.rotation * movingPlatform.activeLocalRotation;
    153. Quaternion rotationDiff = newGlobalRotation * Quaternion.Inverse(movingPlatform.activeGlobalRotation);
    154. var yRotation = rotationDiff.eulerAngles.y;
    155. if (yRotation != 0) {
    156. // Prevent rotation of the local up vector
    157. tr.Rotate(0, yRotation, 0);
    158. }
    159. }
    160. // Save lastPosition for velocity calculation.
    161. Vector3 lastPosition = tr.position;
    162. // We always want the movement to be framerate independent. Multiplying by Time.deltaTime does this.
    163. Vector3 currentMovementOffset = velocity * Time.deltaTime;
    164. // Find out how much we need to push towards the ground to avoid loosing grouning
    165. // when walking down a step or over a sharp change in slope.
    166. float pushDownOffset = Mathf.Max(controller.stepOffset, new Vector3(currentMovementOffset.x, 0, currentMovementOffset.z).magnitude);
    167. if (grounded)
    168. currentMovementOffset -= pushDownOffset * Vector3.up;
    169. // Reset variables that will be set by collision function
    170. movingPlatform.hitPlatform = null;
    171. groundNormal = Vector3.zero;
    172. // Move our character!
    173. movement.collisionFlags = controller.Move (currentMovementOffset);
    174. movement.lastHitPoint = movement.hitPoint;
    175. lastGroundNormal = groundNormal;
    176. if (movingPlatform.enabled && movingPlatform.activePlatform != movingPlatform.hitPlatform) {
    177. if (movingPlatform.hitPlatform != null) {
    178. movingPlatform.activePlatform = movingPlatform.hitPlatform;
    179. movingPlatform.lastMatrix = movingPlatform.hitPlatform.localToWorldMatrix;
    180. movingPlatform.newPlatform = true;
    181. }
    182. }
    183. // Calculate the velocity based on the current and previous position.
    184. // This means our velocity will only be the amount the character actually moved as a result of collisions.
    185. Vector3 oldHVelocity = new Vector3(velocity.x, 0, velocity.z);
    186. movement.velocity = (tr.position - lastPosition) / Time.deltaTime;
    187. Vector3 newHVelocity = new Vector3(movement.velocity.x, 0, movement.velocity.z);
    188. // The CharacterController can be moved in unwanted directions when colliding with things.
    189. // We want to prevent this from influencing the recorded velocity.
    190. if (oldHVelocity == Vector3.zero) {
    191. movement.velocity = new Vector3(0, movement.velocity.y, 0);
    192. }
    193. else {
    194. float projectedNewVelocity = Vector3.Dot(newHVelocity, oldHVelocity) / oldHVelocity.sqrMagnitude;
    195. movement.velocity = oldHVelocity * Mathf.Clamp01(projectedNewVelocity) + movement.velocity.y * Vector3.up;
    196. }
    197. if (movement.velocity.y < velocity.y - 0.001) {
    198. if (movement.velocity.y < 0) {
    199. // Something is forcing the CharacterController down faster than it should.
    200. // Ignore this
    201. movement.velocity.y = velocity.y;
    202. }
    203. else {
    204. // The upwards movement of the CharacterController has been blocked.
    205. // This is treated like a ceiling collision - stop further jumping here.
    206. jumping.holdingJumpButton = false;
    207. }
    208. }
    209. // We were grounded but just loosed grounding
    210. if (grounded && !IsGroundedTest()) {
    211. grounded = false;
    212. // Apply inertia from platform
    213. if (movingPlatform.enabled &&
    214. (movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer ||
    215. movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer)
    216. ) {
    217. movement.frameVelocity = movingPlatform.platformVelocity;
    218. movement.velocity += movingPlatform.platformVelocity;
    219. }
    220. SendMessage("OnFall", SendMessageOptions.DontRequireReceiver);
    221. // We pushed the character down to ensure it would stay on the ground if there was any.
    222. // But there wasn't so now we cancel the downwards offset to make the fall smoother.
    223. tr.position += pushDownOffset * Vector3.up;
    224. }
    225. // We were not grounded but just landed on something
    226. else if (!grounded && IsGroundedTest()) {
    227. grounded = true;
    228. jumping.jumping = false;
    229. SubtractNewPlatformVelocity();
    230. SendMessage("OnLand", SendMessageOptions.DontRequireReceiver);
    231. }
    232. // Moving platforms support
    233. if (MoveWithPlatform()) {
    234. // Use the center of the lower half sphere of the capsule as reference point.
    235. // This works best when the character is standing on moving tilting platforms.
    236. movingPlatform.activeGlobalPoint = tr.position + Vector3.up * (controller.center.y - controller.height*0.5f + controller.radius);
    237. movingPlatform.activeLocalPoint = movingPlatform.activePlatform.InverseTransformPoint(movingPlatform.activeGlobalPoint);
    238. // Support moving platform rotation as well:
    239. movingPlatform.activeGlobalRotation = tr.rotation;
    240. movingPlatform.activeLocalRotation = Quaternion.Inverse(movingPlatform.activePlatform.rotation) * movingPlatform.activeGlobalRotation;
    241. }
    242. }
    243. void FixedUpdate () {
    244. if (movingPlatform.enabled) {
    245. if (movingPlatform.activePlatform != null) {
    246. if (!movingPlatform.newPlatform) {
    247. Vector3 lastVelocity = movingPlatform.platformVelocity;
    248. movingPlatform.platformVelocity = (
    249. movingPlatform.activePlatform.localToWorldMatrix.MultiplyPoint3x4(movingPlatform.activeLocalPoint)
    250. - movingPlatform.lastMatrix.MultiplyPoint3x4(movingPlatform.activeLocalPoint)
    251. ) / Time.deltaTime;
    252. }
    253. movingPlatform.lastMatrix = movingPlatform.activePlatform.localToWorldMatrix;
    254. movingPlatform.newPlatform = false;
    255. }
    256. else {
    257. movingPlatform.platformVelocity = Vector3.zero;
    258. }
    259. }
    260. if (useFixedUpdate)
    261. UpdateFunction();
    262. }
    263. void Update () {
    264. if (!useFixedUpdate)
    265. UpdateFunction();
    266. }
    267. private Vector3 ApplyInputVelocityChange (Vector3 velocity) {
    268. if (!canControl)
    269. inputMoveDirection = Vector3.zero;
    270. // Find desired velocity
    271. Vector3 desiredVelocity;
    272. if (grounded && TooSteep()) {
    273. // The direction we're sliding in
    274. desiredVelocity = new Vector3(groundNormal.x, 0, groundNormal.z).normalized;
    275. // Find the input movement direction projected onto the sliding direction
    276. var projectedMoveDir = Vector3.Project(inputMoveDirection, desiredVelocity);
    277. // Add the sliding direction, the spped control, and the sideways control vectors
    278. desiredVelocity = desiredVelocity + projectedMoveDir * sliding.speedControl + (inputMoveDirection - projectedMoveDir) * sliding.sidewaysControl;
    279. // Multiply with the sliding speed
    280. desiredVelocity *= sliding.slidingSpeed;
    281. }
    282. else
    283. desiredVelocity = GetDesiredHorizontalVelocity();
    284. if (movingPlatform.enabled && movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer) {
    285. desiredVelocity += movement.frameVelocity;
    286. desiredVelocity.y = 0;
    287. }
    288. if (grounded)
    289. desiredVelocity = AdjustGroundVelocityToNormal(desiredVelocity, groundNormal);
    290. else
    291. velocity.y = 0;
    292. // Enforce max velocity change
    293. float maxVelocityChange = GetMaxAcceleration(grounded) * Time.deltaTime;
    294. Vector3 velocityChangeVector = (desiredVelocity - velocity);
    295. if (velocityChangeVector.sqrMagnitude > maxVelocityChange * maxVelocityChange) {
    296. velocityChangeVector = velocityChangeVector.normalized * maxVelocityChange;
    297. }
    298. // If we're in the air and don't have control, don't apply any velocity change at all.
    299. // If we're on the ground and don't have control we do apply it - it will correspond to friction.
    300. if (grounded || canControl)
    301. velocity += velocityChangeVector;
    302. if (grounded) {
    303. // When going uphill, the CharacterController will automatically move up by the needed amount.
    304. // Not moving it upwards manually prevent risk of lifting off from the ground.
    305. // When going downhill, DO move down manually, as gravity is not enough on steep hills.
    306. velocity.y = Mathf.Min(velocity.y, 0);
    307. }
    308. return velocity;
    309. }
    310. private Vector3 ApplyGravityAndJumping (Vector3 velocity) {
    311. if (!inputJump || !canControl) {
    312. jumping.holdingJumpButton = false;
    313. jumping.lastButtonDownTime = -100;
    314. }
    315. if (inputJump && jumping.lastButtonDownTime < 0 && canControl)
    316. jumping.lastButtonDownTime = Time.time;
    317. if (grounded)
    318. velocity.y = Mathf.Min(0, velocity.y) - movement.gravity * Time.deltaTime;
    319. else {
    320. velocity.y = movement.velocity.y - movement.gravity * Time.deltaTime;
    321. // When jumping up we don't apply gravity for some time when the user is holding the jump button.
    322. // This gives more control over jump height by pressing the button longer.
    323. if (jumping.jumping && jumping.holdingJumpButton) {
    324. // Calculate the duration that the extra jump force should have effect.
    325. // If we're still less than that duration after the jumping time, apply the force.
    326. if (Time.time < jumping.lastStartTime + jumping.extraHeight / CalculateJumpVerticalSpeed(jumping.baseHeight)) {
    327. // Negate the gravity we just applied, except we push in jumpDir rather than jump upwards.
    328. velocity += jumping.jumpDir * movement.gravity * Time.deltaTime;
    329. }
    330. }
    331. // Make sure we don't fall any faster than maxFallSpeed. This gives our character a terminal velocity.
    332. velocity.y = Mathf.Max (velocity.y, -movement.maxFallSpeed);
    333. }
    334. if (grounded) {
    335. // Jump only if the jump button was pressed down in the last 0.2 seconds.
    336. // We use this check instead of checking if it's pressed down right now
    337. // because players will often try to jump in the exact moment when hitting the ground after a jump
    338. // and if they hit the button a fraction of a second too soon and no new jump happens as a consequence,
    339. // it's confusing and it feels like the game is buggy.
    340. if (jumping.enabled && canControl && (Time.time - jumping.lastButtonDownTime < 0.2)) {
    341. grounded = false;
    342. jumping.jumping = true;
    343. jumping.lastStartTime = Time.time;
    344. jumping.lastButtonDownTime = -100;
    345. jumping.holdingJumpButton = true;
    346. // Calculate the jumping direction
    347. if (TooSteep())
    348. jumping.jumpDir = Vector3.Slerp(Vector3.up, groundNormal, jumping.steepPerpAmount);
    349. else
    350. jumping.jumpDir = Vector3.Slerp(Vector3.up, groundNormal, jumping.perpAmount);
    351. // Apply the jumping force to the velocity. Cancel any vertical velocity first.
    352. velocity.y = 0;
    353. velocity += jumping.jumpDir * CalculateJumpVerticalSpeed (jumping.baseHeight);
    354. // Apply inertia from platform
    355. if (movingPlatform.enabled &&
    356. (movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer ||
    357. movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer)
    358. ) {
    359. movement.frameVelocity = movingPlatform.platformVelocity;
    360. velocity += movingPlatform.platformVelocity;
    361. }
    362. SendMessage("OnJump", SendMessageOptions.DontRequireReceiver);
    363. }
    364. else {
    365. jumping.holdingJumpButton = false;
    366. }
    367. }
    368. return velocity;
    369. }
    370. void OnControllerColliderHit (ControllerColliderHit hit) {
    371. if (hit.normal.y > 0 && hit.normal.y > groundNormal.y && hit.moveDirection.y < 0) {
    372. if ((hit.point - movement.lastHitPoint).sqrMagnitude > 0.001 || lastGroundNormal == Vector3.zero)
    373. groundNormal = hit.normal;
    374. else
    375. groundNormal = lastGroundNormal;
    376. movingPlatform.hitPlatform = hit.collider.transform;
    377. movement.hitPoint = hit.point;
    378. movement.frameVelocity = Vector3.zero;
    379. }
    380. }
    381. private IEnumerator SubtractNewPlatformVelocity () {
    382. // When landing, subtract the velocity of the new ground from the character's velocity
    383. // since movement in ground is relative to the movement of the ground.
    384. if (movingPlatform.enabled &&
    385. (movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer ||
    386. movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer)
    387. ) {
    388. // If we landed on a new platform, we have to wait for two FixedUpdates
    389. // before we know the velocity of the platform under the character
    390. if (movingPlatform.newPlatform) {
    391. Transform platform = movingPlatform.activePlatform;
    392. yield return new WaitForFixedUpdate();
    393. yield return new WaitForFixedUpdate();
    394. if (grounded && platform == movingPlatform.activePlatform)
    395. yield return 1;
    396. }
    397. movement.velocity -= movingPlatform.platformVelocity;
    398. }
    399. }
    400. private bool MoveWithPlatform () {
    401. return (
    402. movingPlatform.enabled
    403. && (grounded || movingPlatform.movementTransfer == MovementTransferOnJump.PermaLocked)
    404. && movingPlatform.activePlatform != null
    405. );
    406. }
    407. private Vector3 GetDesiredHorizontalVelocity () {
    408. // Find desired velocity
    409. Vector3 desiredLocalDirection = tr.InverseTransformDirection(inputMoveDirection);
    410. float maxSpeed = MaxSpeedInDirection(desiredLocalDirection);
    411. if (grounded) {
    412. // Modify max speed on slopes based on slope speed multiplier curve
    413. var movementSlopeAngle = Mathf.Asin(movement.velocity.normalized.y) * Mathf.Rad2Deg;
    414. maxSpeed *= movement.slopeSpeedMultiplier.Evaluate(movementSlopeAngle);
    415. }
    416. return tr.TransformDirection(desiredLocalDirection * maxSpeed);
    417. }
    418. private Vector3 AdjustGroundVelocityToNormal (Vector3 hVelocity, Vector3 groundNormal) {
    419. Vector3 sideways = Vector3.Cross(Vector3.up, hVelocity);
    420. return Vector3.Cross(sideways, groundNormal).normalized * hVelocity.magnitude;
    421. }
    422. private bool IsGroundedTest () {
    423. return (groundNormal.y > 0.01);
    424. }
    425. float GetMaxAcceleration (bool grounded) {
    426. // Maximum acceleration on ground and in air
    427. if (grounded)
    428. return movement.maxGroundAcceleration;
    429. else
    430. return movement.maxAirAcceleration;
    431. }
    432. float CalculateJumpVerticalSpeed (float targetJumpHeight) {
    433. // From the jump height and gravity we deduce the upwards speed
    434. // for the character to reach at the apex.
    435. return Mathf.Sqrt (2 * targetJumpHeight * movement.gravity);
    436. }
    437. bool IsJumping () {
    438. return jumping.jumping;
    439. }
    440. bool IsSliding () {
    441. return (grounded && sliding.enabled && TooSteep());
    442. }
    443. bool IsTouchingCeiling () {
    444. return (movement.collisionFlags & CollisionFlags.CollidedAbove) != 0;
    445. }
    446. bool IsGrounded () {
    447. return grounded;
    448. }
    449. bool TooSteep () {
    450. return (groundNormal.y <= Mathf.Cos(controller.slopeLimit * Mathf.Deg2Rad));
    451. }
    452. Vector3 GetDirection () {
    453. return inputMoveDirection;
    454. }
    455. void SetControllable (bool controllable) {
    456. canControl = controllable;
    457. }
    458. // Project a direction onto elliptical quater segments based on forward, sideways, and backwards speed.
    459. // The function returns the length of the resulting vector.
    460. float MaxSpeedInDirection (Vector3 desiredMovementDirection) {
    461. if (desiredMovementDirection == Vector3.zero)
    462. return 0;
    463. else {
    464. float zAxisEllipseMultiplier = (desiredMovementDirection.z > 0 ? movement.maxForwardSpeed : movement.maxBackwardsSpeed) / movement.maxSidewaysSpeed;
    465. Vector3 temp = new Vector3(desiredMovementDirection.x, 0, desiredMovementDirection.z / zAxisEllipseMultiplier).normalized;
    466. float length = new Vector3(temp.x, 0, temp.z * zAxisEllipseMultiplier).magnitude * movement.maxSidewaysSpeed;
    467. return length;
    468. }
    469. }
    470. void SetVelocity (Vector3 velocity) {
    471. grounded = false;
    472. movement.velocity = velocity;
    473. movement.frameVelocity = Vector3.zero;
    474. SendMessage("OnExternalVelocity");
    475. }
    476. // Require a character controller to be attached to the same game object
    477. //@script RequireComponent (CharacterController)
    478. //@script AddComponentMenu ("Character/Character Motor")
    479. }