TeHArGiS10

Untitled

Jun 29th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.62 KB | None | 0 0
  1. #pragma strict
  2. #pragma implicit
  3. #pragma downcast
  4.  
  5. var IsWalking = false;
  6. var IsProning = false;
  7. var IsCrouching = false;
  8. var IsRunning = false;
  9. var player : GameObject;
  10. var arms : GameObject;
  11. var gun : GameObject;
  12.  
  13. // Does this script currently respond to input?
  14. var canControl : boolean = true;
  15.  
  16. var useFixedUpdate : boolean = true;
  17.  
  18. // For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.
  19. // Very handy for organization!
  20.  
  21. // The current global direction we want the character to move in.
  22. @System.NonSerialized
  23. var inputMoveDirection : Vector3 = Vector3.zero;
  24.  
  25. // Is the jump button held down? We use this interface instead of checking
  26. // for the jump button directly so this script can also be used by AIs.
  27. @System.NonSerialized
  28. var inputJump : boolean = false;
  29.  
  30. class CharacterMotorMovement {
  31. // The maximum horizontal speed when moving
  32. var maxForwardSpeed : float = 10.0;
  33. var maxSidewaysSpeed : float = 10.0;
  34. var maxBackwardsSpeed : float = 10.0;
  35.  
  36. // Curve for multiplying speed based on slope (negative = downwards)
  37. var slopeSpeedMultiplier : AnimationCurve = AnimationCurve(Keyframe(-90, 1), Keyframe(0, 1), Keyframe(90, 0));
  38.  
  39. // How fast does the character change speeds? Higher is faster.
  40. var maxGroundAcceleration : float = 30.0;
  41. var maxAirAcceleration : float = 20.0;
  42.  
  43. // The gravity for the character
  44. var gravity : float = 10.0;
  45. var maxFallSpeed : float = 20.0;
  46.  
  47. // For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.
  48. // Very handy for organization!
  49.  
  50. // The last collision flags returned from controller.Move
  51. @System.NonSerialized
  52. var collisionFlags : CollisionFlags;
  53.  
  54. // We will keep track of the character's current velocity,
  55. @System.NonSerialized
  56. var velocity : Vector3;
  57.  
  58. // This keeps track of our current velocity while we're not grounded
  59. @System.NonSerialized
  60. var frameVelocity : Vector3 = Vector3.zero;
  61.  
  62. @System.NonSerialized
  63. var hitPoint : Vector3 = Vector3.zero;
  64.  
  65. @System.NonSerialized
  66. var lastHitPoint : Vector3 = Vector3(Mathf.Infinity, 0, 0);
  67. }
  68.  
  69. var movement : CharacterMotorMovement = CharacterMotorMovement();
  70.  
  71. enum MovementTransferOnJump {
  72. None, // The jump is not affected by velocity of floor at all.
  73. InitTransfer, // Jump gets its initial velocity from the floor, then gradualy comes to a stop.
  74. PermaTransfer, // Jump gets its initial velocity from the floor, and keeps that velocity until landing.
  75. PermaLocked // Jump is relative to the movement of the last touched floor and will move together with that floor.
  76. }
  77.  
  78. // We will contain all the jumping related variables in one helper class for clarity.
  79. class CharacterMotorJumping {
  80. // Can the character jump?
  81. var enabled : boolean = true;
  82.  
  83. // How high do we jump when pressing jump and letting go immediately
  84. var baseHeight : float = 1.0;
  85.  
  86. // We add extraHeight units (meters) on top when holding the button down longer while jumping
  87. var extraHeight : float = 4.1;
  88.  
  89. // How much does the character jump out perpendicular to the surface on walkable surfaces?
  90. // 0 means a fully vertical jump and 1 means fully perpendicular.
  91. var perpAmount : float = 0.0;
  92.  
  93. // How much does the character jump out perpendicular to the surface on too steep surfaces?
  94. // 0 means a fully vertical jump and 1 means fully perpendicular.
  95. var steepPerpAmount : float = 0.5;
  96.  
  97. // For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.
  98. // Very handy for organization!
  99.  
  100. // Are we jumping? (Initiated with jump button and not grounded yet)
  101. // To see if we are just in the air (initiated by jumping OR falling) see the grounded variable.
  102. @System.NonSerialized
  103. var jumping : boolean = false;
  104.  
  105. @System.NonSerialized
  106. var holdingJumpButton : boolean = false;
  107.  
  108. // the time we jumped at (Used to determine for how long to apply extra jump power after jumping.)
  109. @System.NonSerialized
  110. var lastStartTime : float = 0.0;
  111.  
  112. @System.NonSerialized
  113. var lastButtonDownTime : float = -100;
  114.  
  115. @System.NonSerialized
  116. var jumpDir : Vector3 = Vector3.up;
  117. }
  118.  
  119. var jumping : CharacterMotorJumping = CharacterMotorJumping();
  120.  
  121. class CharacterMotorMovingPlatform {
  122. var enabled : boolean = true;
  123.  
  124. var movementTransfer : MovementTransferOnJump = MovementTransferOnJump.PermaTransfer;
  125.  
  126. @System.NonSerialized
  127. var hitPlatform : Transform;
  128.  
  129. @System.NonSerialized
  130. var activePlatform : Transform;
  131.  
  132. @System.NonSerialized
  133. var activeLocalPoint : Vector3;
  134.  
  135. @System.NonSerialized
  136. var activeGlobalPoint : Vector3;
  137.  
  138. @System.NonSerialized
  139. var activeLocalRotation : Quaternion;
  140.  
  141. @System.NonSerialized
  142. var activeGlobalRotation : Quaternion;
  143.  
  144. @System.NonSerialized
  145. var lastMatrix : Matrix4x4;
  146.  
  147. @System.NonSerialized
  148. var platformVelocity : Vector3;
  149.  
  150. @System.NonSerialized
  151. var newPlatform : boolean;
  152. }
  153.  
  154. var movingPlatform : CharacterMotorMovingPlatform = CharacterMotorMovingPlatform();
  155.  
  156. class CharacterMotorSliding {
  157. // Does the character slide on too steep surfaces?
  158. var enabled : boolean = true;
  159.  
  160. // How fast does the character slide on steep surfaces?
  161. var slidingSpeed : float = 15;
  162.  
  163. // How much can the player control the sliding direction?
  164. // If the value is 0.5 the player can slide sideways with half the speed of the downwards sliding speed.
  165. var sidewaysControl : float = 1.0;
  166.  
  167. // How much can the player influence the sliding speed?
  168. // If the value is 0.5 the player can speed the sliding up to 150% or slow it down to 50%.
  169. var speedControl : float = 0.4;
  170. }
  171.  
  172. var sliding : CharacterMotorSliding = CharacterMotorSliding();
  173.  
  174. @System.NonSerialized
  175. var grounded : boolean = true;
  176.  
  177. @System.NonSerialized
  178. var groundNormal : Vector3 = Vector3.zero;
  179.  
  180. private var lastGroundNormal : Vector3 = Vector3.zero;
  181.  
  182. private var tr : Transform;
  183.  
  184. private var controller : CharacterController;
  185.  
  186. function Awake () {
  187. controller = GetComponent (CharacterController);
  188. tr = transform;
  189. }
  190.  
  191. private function UpdateFunction () {
  192. // We copy the actual velocity into a temporary variable that we can manipulate.
  193. var velocity : Vector3 = movement.velocity;
  194.  
  195. // Update velocity based on input
  196. velocity = ApplyInputVelocityChange(velocity);
  197.  
  198. // Apply gravity and jumping force
  199. velocity = ApplyGravityAndJumping (velocity);
  200.  
  201. // Moving platform support
  202. var moveDistance : Vector3 = Vector3.zero;
  203. if (MoveWithPlatform()) {
  204. var newGlobalPoint : Vector3 = movingPlatform.activePlatform.TransformPoint(movingPlatform.activeLocalPoint);
  205. moveDistance = (newGlobalPoint - movingPlatform.activeGlobalPoint);
  206. if (moveDistance != Vector3.zero)
  207. controller.Move(moveDistance);
  208.  
  209. // Support moving platform rotation as well:
  210. var newGlobalRotation : Quaternion = movingPlatform.activePlatform.rotation * movingPlatform.activeLocalRotation;
  211. var rotationDiff : Quaternion = newGlobalRotation * Quaternion.Inverse(movingPlatform.activeGlobalRotation);
  212.  
  213. var yRotation = rotationDiff.eulerAngles.y;
  214. if (yRotation != 0) {
  215. // Prevent rotation of the local up vector
  216. tr.Rotate(0, yRotation, 0);
  217. }
  218. }
  219.  
  220. // Save lastPosition for velocity calculation.
  221. var lastPosition : Vector3 = tr.position;
  222.  
  223. // We always want the movement to be framerate independent. Multiplying by Time.deltaTime does this.
  224. var currentMovementOffset : Vector3 = velocity * Time.deltaTime;
  225.  
  226. // Find out how much we need to push towards the ground to avoid loosing grouning
  227. // when walking down a step or over a sharp change in slope.
  228. var pushDownOffset : float = Mathf.Max(controller.stepOffset, Vector3(currentMovementOffset.x, 0, currentMovementOffset.z).magnitude);
  229. if (grounded)
  230. currentMovementOffset -= pushDownOffset * Vector3.up;
  231.  
  232. // Reset variables that will be set by collision function
  233. movingPlatform.hitPlatform = null;
  234. groundNormal = Vector3.zero;
  235.  
  236. // Move our character!
  237. movement.collisionFlags = controller.Move (currentMovementOffset);
  238.  
  239. movement.lastHitPoint = movement.hitPoint;
  240. lastGroundNormal = groundNormal;
  241.  
  242. if (movingPlatform.enabled && movingPlatform.activePlatform != movingPlatform.hitPlatform) {
  243. if (movingPlatform.hitPlatform != null) {
  244. movingPlatform.activePlatform = movingPlatform.hitPlatform;
  245. movingPlatform.lastMatrix = movingPlatform.hitPlatform.localToWorldMatrix;
  246. movingPlatform.newPlatform = true;
  247. }
  248. }
  249.  
  250. // Calculate the velocity based on the current and previous position.
  251. // This means our velocity will only be the amount the character actually moved as a result of collisions.
  252. var oldHVelocity : Vector3 = new Vector3(velocity.x, 0, velocity.z);
  253. movement.velocity = (tr.position - lastPosition) / Time.deltaTime;
  254. var newHVelocity : Vector3 = new Vector3(movement.velocity.x, 0, movement.velocity.z);
  255.  
  256. // The CharacterController can be moved in unwanted directions when colliding with things.
  257. // We want to prevent this from influencing the recorded velocity.
  258. if (oldHVelocity == Vector3.zero) {
  259. movement.velocity = new Vector3(0, movement.velocity.y, 0);
  260. }
  261. else {
  262. var projectedNewVelocity : float = Vector3.Dot(newHVelocity, oldHVelocity) / oldHVelocity.sqrMagnitude;
  263. movement.velocity = oldHVelocity * Mathf.Clamp01(projectedNewVelocity) + movement.velocity.y * Vector3.up;
  264. }
  265.  
  266. if (movement.velocity.y < velocity.y - 0.001) {
  267. if (movement.velocity.y < 0) {
  268. // Something is forcing the CharacterController down faster than it should.
  269. // Ignore this
  270. movement.velocity.y = velocity.y;
  271. }
  272. else {
  273. // The upwards movement of the CharacterController has been blocked.
  274. // This is treated like a ceiling collision - stop further jumping here.
  275. jumping.holdingJumpButton = false;
  276. }
  277. }
  278.  
  279. // We were grounded but just loosed grounding
  280. if (grounded && !IsGroundedTest()) {
  281. grounded = false;
  282.  
  283. // Apply inertia from platform
  284. if (movingPlatform.enabled &&
  285. (movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer ||
  286. movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer)
  287. ) {
  288. movement.frameVelocity = movingPlatform.platformVelocity;
  289. movement.velocity += movingPlatform.platformVelocity;
  290. }
  291.  
  292. SendMessage("OnFall", SendMessageOptions.DontRequireReceiver);
  293. // We pushed the character down to ensure it would stay on the ground if there was any.
  294. // But there wasn't so now we cancel the downwards offset to make the fall smoother.
  295. tr.position += pushDownOffset * Vector3.up;
  296. }
  297. // We were not grounded but just landed on something
  298. else if (!grounded && IsGroundedTest()) {
  299. grounded = true;
  300. jumping.jumping = false;
  301. SubtractNewPlatformVelocity();
  302.  
  303. SendMessage("OnLand", SendMessageOptions.DontRequireReceiver);
  304. }
  305.  
  306. // Moving platforms support
  307. if (MoveWithPlatform()) {
  308. // Use the center of the lower half sphere of the capsule as reference point.
  309. // This works best when the character is standing on moving tilting platforms.
  310. movingPlatform.activeGlobalPoint = tr.position + Vector3.up * (controller.center.y - controller.height*0.5 + controller.radius);
  311. movingPlatform.activeLocalPoint = movingPlatform.activePlatform.InverseTransformPoint(movingPlatform.activeGlobalPoint);
  312.  
  313. // Support moving platform rotation as well:
  314. movingPlatform.activeGlobalRotation = tr.rotation;
  315. movingPlatform.activeLocalRotation = Quaternion.Inverse(movingPlatform.activePlatform.rotation) * movingPlatform.activeGlobalRotation;
  316. }
  317. }
  318.  
  319. function FixedUpdate () {
  320. if (movingPlatform.enabled) {
  321. if (movingPlatform.activePlatform != null) {
  322. if (!movingPlatform.newPlatform) {
  323. var lastVelocity : Vector3 = movingPlatform.platformVelocity;
  324.  
  325. movingPlatform.platformVelocity = (
  326. movingPlatform.activePlatform.localToWorldMatrix.MultiplyPoint3x4(movingPlatform.activeLocalPoint)
  327. - movingPlatform.lastMatrix.MultiplyPoint3x4(movingPlatform.activeLocalPoint)
  328. ) / Time.deltaTime;
  329. }
  330. movingPlatform.lastMatrix = movingPlatform.activePlatform.localToWorldMatrix;
  331. movingPlatform.newPlatform = false;
  332. }
  333. else {
  334. movingPlatform.platformVelocity = Vector3.zero;
  335. }
  336. }
  337.  
  338. if (useFixedUpdate)
  339. UpdateFunction();
  340. }
  341.  
  342. function Update () {
  343. if (!useFixedUpdate)
  344. UpdateFunction();
  345.  
  346. if(Input.GetKeyDown(KeyCode.LeftControl)) {
  347. player.animation.Play("prone");
  348. IsProning = true;
  349. IsCrouching = false;
  350. player.animation.Stop("prone up");
  351. }
  352. if(Input.GetKeyUp(KeyCode.LeftControl)) {
  353. player.animation.Stop("prone");
  354. IsProning = false;
  355. player.animation.Play("prone up");
  356. }
  357.  
  358. if(Input.GetKeyDown(KeyCode.C)) {
  359. player.animation.Play("crouch");
  360. IsCrouching = true;
  361. IsProning = false;
  362. player.animation.Stop("crouch up");
  363. }
  364. if(Input.GetKeyUp(KeyCode.C)) {
  365. player.animation.Stop("crouch");
  366. IsCrouching = false;
  367. player.animation.Play("crouch up");
  368. }
  369.  
  370. if(IsProning && !IsCrouching && !IsRunning) {
  371. movement.maxForwardSpeed = 2;
  372. movement.maxSidewaysSpeed = 2;
  373. movement.maxBackwardsSpeed = 2;
  374. Debug.Log("Is Proning");
  375. }
  376. if(!IsProning && IsCrouching && !IsRunning) {
  377. movement.maxForwardSpeed = 3.5;
  378. movement.maxSidewaysSpeed = 3.5;
  379. movement.maxBackwardsSpeed = 3.5;
  380. Debug.Log("Is Crouching");
  381. }
  382. if(!IsProning && !IsCrouching && !IsRunning) {
  383. movement.maxForwardSpeed = 6;
  384. movement.maxSidewaysSpeed = 6;
  385. movement.maxBackwardsSpeed = 6;
  386. Debug.Log("Is Walking");
  387. }
  388. if(Input.GetKeyDown(KeyCode.LeftShift))
  389. IsRunning = true;
  390. if(IsRunning && !IsProning && !IsCrouching) { // Otan trexei kai den einai oute prone oute crouch
  391. movement.maxForwardSpeed = 8;
  392. movement.maxSidewaysSpeed = 8;
  393. movement.maxBackwardsSpeed = 8;
  394. Debug.Log("Is Running");
  395. }
  396. if(IsRunning && IsProning && !IsCrouching) { // Otan trexei kai einai prone alla oxi crouch
  397. movement.maxForwardSpeed = 3;
  398. movement.maxSidewaysSpeed = 3;
  399. movement.maxBackwardsSpeed = 3;
  400. Debug.Log("Is Running and Is Proning");
  401. }
  402. if(IsRunning && !IsProning && IsCrouching) { // Otan trexei kai einai crouch alla oxi prone
  403. movement.maxForwardSpeed = 4.5;
  404. movement.maxSidewaysSpeed = 4.5;
  405. movement.maxBackwardsSpeed = 4.5;
  406. Debug.Log("Is Running and Is Crouching");
  407. }
  408.  
  409. if(Input.GetKeyUp(KeyCode.LeftShift))
  410. IsRunning = false;
  411.  
  412. if(movement.maxForwardSpeed > 0 || movement.maxSidewaysSpeed > 0 || movement.maxBackwardsSpeed > 0)
  413. IsWalking = true;
  414. else
  415. IsWalking = false;
  416. }
  417.  
  418.  
  419. private function ApplyInputVelocityChange (velocity : Vector3) {
  420. if (!canControl)
  421. inputMoveDirection = Vector3.zero;
  422.  
  423. // Find desired velocity
  424. var desiredVelocity : Vector3;
  425. if (grounded && TooSteep()) {
  426. // The direction we're sliding in
  427. desiredVelocity = Vector3(groundNormal.x, 0, groundNormal.z).normalized;
  428. // Find the input movement direction projected onto the sliding direction
  429. var projectedMoveDir = Vector3.Project(inputMoveDirection, desiredVelocity);
  430. // Add the sliding direction, the spped control, and the sideways control vectors
  431. desiredVelocity = desiredVelocity + projectedMoveDir * sliding.speedControl + (inputMoveDirection - projectedMoveDir) * sliding.sidewaysControl;
  432. // Multiply with the sliding speed
  433. desiredVelocity *= sliding.slidingSpeed;
  434. }
  435. else
  436. desiredVelocity = GetDesiredHorizontalVelocity();
  437.  
  438. if (movingPlatform.enabled && movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer) {
  439. desiredVelocity += movement.frameVelocity;
  440. desiredVelocity.y = 0;
  441. }
  442.  
  443. if (grounded)
  444. desiredVelocity = AdjustGroundVelocityToNormal(desiredVelocity, groundNormal);
  445. else
  446. velocity.y = 0;
  447.  
  448. // Enforce max velocity change
  449. var maxVelocityChange : float = GetMaxAcceleration(grounded) * Time.deltaTime;
  450. var velocityChangeVector : Vector3 = (desiredVelocity - velocity);
  451. if (velocityChangeVector.sqrMagnitude > maxVelocityChange * maxVelocityChange) {
  452. velocityChangeVector = velocityChangeVector.normalized * maxVelocityChange;
  453. }
  454. // If we're in the air and don't have control, don't apply any velocity change at all.
  455. // If we're on the ground and don't have control we do apply it - it will correspond to friction.
  456. if (grounded || canControl)
  457. velocity += velocityChangeVector;
  458.  
  459. if (grounded) {
  460. // When going uphill, the CharacterController will automatically move up by the needed amount.
  461. // Not moving it upwards manually prevent risk of lifting off from the ground.
  462. // When going downhill, DO move down manually, as gravity is not enough on steep hills.
  463. velocity.y = Mathf.Min(velocity.y, 0);
  464. }
  465.  
  466. return velocity;
  467. }
  468.  
  469. private function ApplyGravityAndJumping (velocity : Vector3) {
  470.  
  471. if (!inputJump || !canControl) {
  472. jumping.holdingJumpButton = false;
  473. jumping.lastButtonDownTime = -100;
  474. }
  475.  
  476. if (inputJump && jumping.lastButtonDownTime < 0 && canControl)
  477. jumping.lastButtonDownTime = Time.time;
  478.  
  479. if (grounded)
  480. velocity.y = Mathf.Min(0, velocity.y) - movement.gravity * Time.deltaTime;
  481. else {
  482. velocity.y = movement.velocity.y - movement.gravity * Time.deltaTime;
  483.  
  484. // When jumping up we don't apply gravity for some time when the user is holding the jump button.
  485. // This gives more control over jump height by pressing the button longer.
  486. if (jumping.jumping && jumping.holdingJumpButton) {
  487. // Calculate the duration that the extra jump force should have effect.
  488. // If we're still less than that duration after the jumping time, apply the force.
  489. if (Time.time < jumping.lastStartTime + jumping.extraHeight / CalculateJumpVerticalSpeed(jumping.baseHeight)) {
  490. // Negate the gravity we just applied, except we push in jumpDir rather than jump upwards.
  491. velocity += jumping.jumpDir * movement.gravity * Time.deltaTime;
  492. }
  493. }
  494.  
  495. // Make sure we don't fall any faster than maxFallSpeed. This gives our character a terminal velocity.
  496. velocity.y = Mathf.Max (velocity.y, -movement.maxFallSpeed);
  497. }
  498.  
  499. if (grounded) {
  500. // Jump only if the jump button was pressed down in the last 0.2 seconds.
  501. // We use this check instead of checking if it's pressed down right now
  502. // because players will often try to jump in the exact moment when hitting the ground after a jump
  503. // and if they hit the button a fraction of a second too soon and no new jump happens as a consequence,
  504. // it's confusing and it feels like the game is buggy.
  505. if (jumping.enabled && canControl && (Time.time - jumping.lastButtonDownTime < 0.2)) {
  506. grounded = false;
  507. jumping.jumping = true;
  508. jumping.lastStartTime = Time.time;
  509. jumping.lastButtonDownTime = -100;
  510. jumping.holdingJumpButton = true;
  511.  
  512. // Calculate the jumping direction
  513. if (TooSteep())
  514. jumping.jumpDir = Vector3.Slerp(Vector3.up, groundNormal, jumping.steepPerpAmount);
  515. else
  516. jumping.jumpDir = Vector3.Slerp(Vector3.up, groundNormal, jumping.perpAmount);
  517.  
  518. // Apply the jumping force to the velocity. Cancel any vertical velocity first.
  519. velocity.y = 0;
  520. velocity += jumping.jumpDir * CalculateJumpVerticalSpeed (jumping.baseHeight);
  521.  
  522. // Apply inertia from platform
  523. if (movingPlatform.enabled &&
  524. (movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer ||
  525. movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer)
  526. ) {
  527. movement.frameVelocity = movingPlatform.platformVelocity;
  528. velocity += movingPlatform.platformVelocity;
  529. }
  530.  
  531. SendMessage("OnJump", SendMessageOptions.DontRequireReceiver);
  532. }
  533. else {
  534. jumping.holdingJumpButton = false;
  535. }
  536. }
  537.  
  538. return velocity;
  539. }
  540.  
  541. function OnControllerColliderHit (hit : ControllerColliderHit) {
  542. if (hit.normal.y > 0 && hit.normal.y > groundNormal.y && hit.moveDirection.y < 0) {
  543. if ((hit.point - movement.lastHitPoint).sqrMagnitude > 0.001 || lastGroundNormal == Vector3.zero)
  544. groundNormal = hit.normal;
  545. else
  546. groundNormal = lastGroundNormal;
  547.  
  548. movingPlatform.hitPlatform = hit.collider.transform;
  549. movement.hitPoint = hit.point;
  550. movement.frameVelocity = Vector3.zero;
  551. }
  552. }
  553.  
  554. private function SubtractNewPlatformVelocity () {
  555. // When landing, subtract the velocity of the new ground from the character's velocity
  556. // since movement in ground is relative to the movement of the ground.
  557. if (movingPlatform.enabled &&
  558. (movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer ||
  559. movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer)
  560. ) {
  561. // If we landed on a new platform, we have to wait for two FixedUpdates
  562. // before we know the velocity of the platform under the character
  563. if (movingPlatform.newPlatform) {
  564. var platform : Transform = movingPlatform.activePlatform;
  565. yield WaitForFixedUpdate();
  566. yield WaitForFixedUpdate();
  567. if (grounded && platform == movingPlatform.activePlatform)
  568. yield 1;
  569. }
  570. movement.velocity -= movingPlatform.platformVelocity;
  571. }
  572. }
  573.  
  574. private function MoveWithPlatform () : boolean {
  575. return (
  576. movingPlatform.enabled
  577. && (grounded || movingPlatform.movementTransfer == MovementTransferOnJump.PermaLocked)
  578. && movingPlatform.activePlatform != null
  579. );
  580. }
  581.  
  582. private function GetDesiredHorizontalVelocity () {
  583. // Find desired velocity
  584. var desiredLocalDirection : Vector3 = tr.InverseTransformDirection(inputMoveDirection);
  585. var maxSpeed : float = MaxSpeedInDirection(desiredLocalDirection);
  586. if (grounded) {
  587. // Modify max speed on slopes based on slope speed multiplier curve
  588. var movementSlopeAngle = Mathf.Asin(movement.velocity.normalized.y) * Mathf.Rad2Deg;
  589. maxSpeed *= movement.slopeSpeedMultiplier.Evaluate(movementSlopeAngle);
  590. }
  591. return tr.TransformDirection(desiredLocalDirection * maxSpeed);
  592. }
  593.  
  594. private function AdjustGroundVelocityToNormal (hVelocity : Vector3, groundNormal : Vector3) : Vector3 {
  595. var sideways : Vector3 = Vector3.Cross(Vector3.up, hVelocity);
  596. return Vector3.Cross(sideways, groundNormal).normalized * hVelocity.magnitude;
  597. }
  598.  
  599. private function IsGroundedTest () {
  600. return (groundNormal.y > 0.01);
  601. }
  602.  
  603. function GetMaxAcceleration (grounded : boolean) : float {
  604. // Maximum acceleration on ground and in air
  605. if (grounded)
  606. return movement.maxGroundAcceleration;
  607. else
  608. return movement.maxAirAcceleration;
  609. }
  610.  
  611. function CalculateJumpVerticalSpeed (targetJumpHeight : float) {
  612. // From the jump height and gravity we deduce the upwards speed
  613. // for the character to reach at the apex.
  614. return Mathf.Sqrt (2 * targetJumpHeight * movement.gravity);
  615. }
  616.  
  617. function IsJumping () {
  618. return jumping.jumping;
  619. }
  620.  
  621. function IsSliding () {
  622. return (grounded && sliding.enabled && TooSteep());
  623. }
  624.  
  625. function IsTouchingCeiling () {
  626. return (movement.collisionFlags & CollisionFlags.CollidedAbove) != 0;
  627. }
  628.  
  629. function IsGrounded () {
  630. return grounded;
  631. }
  632.  
  633. function TooSteep () {
  634. return (groundNormal.y <= Mathf.Cos(controller.slopeLimit * Mathf.Deg2Rad));
  635. }
  636.  
  637. function GetDirection () {
  638. return inputMoveDirection;
  639. }
  640.  
  641. function SetControllable (controllable : boolean) {
  642. canControl = controllable;
  643. }
  644.  
  645. // Project a direction onto elliptical quater segments based on forward, sideways, and backwards speed.
  646. // The function returns the length of the resulting vector.
  647. function MaxSpeedInDirection (desiredMovementDirection : Vector3) : float {
  648. if (desiredMovementDirection == Vector3.zero)
  649. return 0;
  650. else {
  651. var zAxisEllipseMultiplier : float = (desiredMovementDirection.z > 0 ? movement.maxForwardSpeed : movement.maxBackwardsSpeed) / movement.maxSidewaysSpeed;
  652. var temp : Vector3 = new Vector3(desiredMovementDirection.x, 0, desiredMovementDirection.z / zAxisEllipseMultiplier).normalized;
  653. var length : float = new Vector3(temp.x, 0, temp.z * zAxisEllipseMultiplier).magnitude * movement.maxSidewaysSpeed;
  654. return length;
  655. }
  656. }
  657.  
  658. function SetVelocity (velocity : Vector3) {
  659. grounded = false;
  660. movement.velocity = velocity;
  661. movement.frameVelocity = Vector3.zero;
  662. SendMessage("OnExternalVelocity");
  663. }
  664.  
  665. function Run () {
  666. maxForwardSpeed = 10.0;
  667. maxSidewaysSpeed = 10.0;
  668. maxBackwardsSpeed = 10.0;
  669. }
  670.  
  671.  
  672. // Require a character controller to be attached to the same game object
  673. @script RequireComponent (CharacterController)
  674. @script AddComponentMenu ("Character/Character Motor")
  675.  
  676. function Start () {
  677. player.animation.Stop("prone");
  678. player.animation.Stop("prone up");
  679. player.animation.Stop("crouch");
  680. player.animation.Stop("crouch up");
  681. }
Advertisement
Add Comment
Please, Sign In to add comment