CharacterMotor.js 21 KB

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