TeHArGiS10

Untitled

Jun 29th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.50 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.  
  413.  
  414. private function ApplyInputVelocityChange (velocity : Vector3) {
  415. if (!canControl) movement.maxBackwardsSpeed
  416. inputMoveDirection = Vector3.zero;
  417.  
  418. // Find desired velocity
  419. var desiredVelocity : Vector3;
  420. if (grounded && TooSteep()) {
  421. // The direction we're sliding in
  422. desiredVelocity = Vector3(groundNormal.x, 0, groundNormal.z).normalized;
  423. // Find the input movement direction projected onto the sliding direction
  424. var projectedMoveDir = Vector3.Project(inputMoveDirection, desiredVelocity);
  425. // Add the sliding direction, the spped control, and the sideways control vectors
  426. desiredVelocity = desiredVelocity + projectedMoveDir * sliding.speedControl + (inputMoveDirection - projectedMoveDir) * sliding.sidewaysControl;
  427. // Multiply with the sliding speed
  428. desiredVelocity *= sliding.slidingSpeed;
  429. }
  430. else
  431. desiredVelocity = GetDesiredHorizontalVelocity();
  432.  
  433. if (movingPlatform.enabled && movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer) {
  434. desiredVelocity += movement.frameVelocity;
  435. desiredVelocity.y = 0;
  436. }
  437.  
  438. if (grounded)
  439. desiredVelocity = AdjustGroundVelocityToNormal(desiredVelocity, groundNormal);
  440. else
  441. velocity.y = 0;
  442.  
  443. // Enforce max velocity change
  444. var maxVelocityChange : float = GetMaxAcceleration(grounded) * Time.deltaTime;
  445. var velocityChangeVector : Vector3 = (desiredVelocity - velocity);
  446. if (velocityChangeVector.sqrMagnitude > maxVelocityChange * maxVelocityChange) {
  447. velocityChangeVector = velocityChangeVector.normalized * maxVelocityChange;
  448. }
  449. // If we're in the air and don't have control, don't apply any velocity change at all.
  450. // If we're on the ground and don't have control we do apply it - it will correspond to friction.
  451. if (grounded || canControl)
  452. velocity += velocityChangeVector;
  453.  
  454. if (grounded) {
  455. // When going uphill, the CharacterController will automatically move up by the needed amount.
  456. // Not moving it upwards manually prevent risk of lifting off from the ground.
  457. // When going downhill, DO move down manually, as gravity is not enough on steep hills.
  458. velocity.y = Mathf.Min(velocity.y, 0);
  459. }
  460.  
  461. return velocity;
  462. }
  463.  
  464. private function ApplyGravityAndJumping (velocity : Vector3) {
  465.  
  466. if (!inputJump || !canControl) {
  467. jumping.holdingJumpButton = false;
  468. jumping.lastButtonDownTime = -100;
  469. }
  470.  
  471. if (inputJump && jumping.lastButtonDownTime < 0 && canControl)
  472. jumping.lastButtonDownTime = Time.time;
  473.  
  474. if (grounded)
  475. velocity.y = Mathf.Min(0, velocity.y) - movement.gravity * Time.deltaTime;
  476. else {
  477. velocity.y = movement.velocity.y - movement.gravity * Time.deltaTime;
  478.  
  479. // When jumping up we don't apply gravity for some time when the user is holding the jump button.
  480. // This gives more control over jump height by pressing the button longer.
  481. if (jumping.jumping && jumping.holdingJumpButton) {
  482. // Calculate the duration that the extra jump force should have effect.
  483. // If we're still less than that duration after the jumping time, apply the force.
  484. if (Time.time < jumping.lastStartTime + jumping.extraHeight / CalculateJumpVerticalSpeed(jumping.baseHeight)) {
  485. // Negate the gravity we just applied, except we push in jumpDir rather than jump upwards.
  486. velocity += jumping.jumpDir * movement.gravity * Time.deltaTime;
  487. }
  488. }
  489.  
  490. // Make sure we don't fall any faster than maxFallSpeed. This gives our character a terminal velocity.
  491. velocity.y = Mathf.Max (velocity.y, -movement.maxFallSpeed);
  492. }
  493.  
  494. if (grounded) {
  495. // Jump only if the jump button was pressed down in the last 0.2 seconds.
  496. // We use this check instead of checking if it's pressed down right now
  497. // because players will often try to jump in the exact moment when hitting the ground after a jump
  498. // and if they hit the button a fraction of a second too soon and no new jump happens as a consequence,
  499. // it's confusing and it feels like the game is buggy.
  500. if (jumping.enabled && canControl && (Time.time - jumping.lastButtonDownTime < 0.2)) {
  501. grounded = false;
  502. jumping.jumping = true;
  503. jumping.lastStartTime = Time.time;
  504. jumping.lastButtonDownTime = -100;
  505. jumping.holdingJumpButton = true;
  506.  
  507. // Calculate the jumping direction
  508. if (TooSteep())
  509. jumping.jumpDir = Vector3.Slerp(Vector3.up, groundNormal, jumping.steepPerpAmount);
  510. else
  511. jumping.jumpDir = Vector3.Slerp(Vector3.up, groundNormal, jumping.perpAmount);
  512.  
  513. // Apply the jumping force to the velocity. Cancel any vertical velocity first.
  514. velocity.y = 0;
  515. velocity += jumping.jumpDir * CalculateJumpVerticalSpeed (jumping.baseHeight);
  516.  
  517. // Apply inertia from platform
  518. if (movingPlatform.enabled &&
  519. (movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer ||
  520. movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer)
  521. ) {
  522. movement.frameVelocity = movingPlatform.platformVelocity;
  523. velocity += movingPlatform.platformVelocity;
  524. }
  525.  
  526. SendMessage("OnJump", SendMessageOptions.DontRequireReceiver);
  527. }
  528. else {
  529. jumping.holdingJumpButton = false;
  530. }
  531. }
  532.  
  533. return velocity;
  534. }
  535.  
  536. function OnControllerColliderHit (hit : ControllerColliderHit) {
  537. if (hit.normal.y > 0 && hit.normal.y > groundNormal.y && hit.moveDirection.y < 0) {
  538. if ((hit.point - movement.lastHitPoint).sqrMagnitude > 0.001 || lastGroundNormal == Vector3.zero)
  539. groundNormal = hit.normal;
  540. else
  541. groundNormal = lastGroundNormal;
  542.  
  543. movingPlatform.hitPlatform = hit.collider.transform;
  544. movement.hitPoint = hit.point;
  545. movement.frameVelocity = Vector3.zero;
  546. }
  547. }
  548.  
  549. private function SubtractNewPlatformVelocity () {
  550. // When landing, subtract the velocity of the new ground from the character's velocity
  551. // since movement in ground is relative to the movement of the ground.
  552. if (movingPlatform.enabled &&
  553. (movingPlatform.movementTransfer == MovementTransferOnJump.InitTransfer ||
  554. movingPlatform.movementTransfer == MovementTransferOnJump.PermaTransfer)
  555. ) {
  556. // If we landed on a new platform, we have to wait for two FixedUpdates
  557. // before we know the velocity of the platform under the character
  558. if (movingPlatform.newPlatform) {
  559. var platform : Transform = movingPlatform.activePlatform;
  560. yield WaitForFixedUpdate();
  561. yield WaitForFixedUpdate();
  562. if (grounded && platform == movingPlatform.activePlatform)
  563. yield 1;
  564. }
  565. movement.velocity -= movingPlatform.platformVelocity;
  566. }
  567. }
  568.  
  569. private function MoveWithPlatform () : boolean {
  570. return (
  571. movingPlatform.enabled
  572. && (grounded || movingPlatform.movementTransfer == MovementTransferOnJump.PermaLocked)
  573. && movingPlatform.activePlatform != null
  574. );
  575. }
  576.  
  577. private function GetDesiredHorizontalVelocity () {
  578. // Find desired velocity
  579. var desiredLocalDirection : Vector3 = tr.InverseTransformDirection(inputMoveDirection);
  580. var maxSpeed : float = MaxSpeedInDirection(desiredLocalDirection);
  581. if (grounded) {
  582. // Modify max speed on slopes based on slope speed multiplier curve
  583. var movementSlopeAngle = Mathf.Asin(movement.velocity.normalized.y) * Mathf.Rad2Deg;
  584. maxSpeed *= movement.slopeSpeedMultiplier.Evaluate(movementSlopeAngle);
  585. }
  586. return tr.TransformDirection(desiredLocalDirection * maxSpeed);
  587. }
  588.  
  589. private function AdjustGroundVelocityToNormal (hVelocity : Vector3, groundNormal : Vector3) : Vector3 {
  590. var sideways : Vector3 = Vector3.Cross(Vector3.up, hVelocity);
  591. return Vector3.Cross(sideways, groundNormal).normalized * hVelocity.magnitude;
  592. }
  593.  
  594. private function IsGroundedTest () {
  595. return (groundNormal.y > 0.01);
  596. }
  597.  
  598. function GetMaxAcceleration (grounded : boolean) : float {
  599. // Maximum acceleration on ground and in air
  600. if (grounded)
  601. return movement.maxGroundAcceleration;
  602. else
  603. return movement.maxAirAcceleration;
  604. }
  605.  
  606. function CalculateJumpVerticalSpeed (targetJumpHeight : float) {
  607. // From the jump height and gravity we deduce the upwards speed
  608. // for the character to reach at the apex.
  609. return Mathf.Sqrt (2 * targetJumpHeight * movement.gravity);
  610. }
  611.  
  612. function IsJumping () {
  613. return jumping.jumping;
  614. }
  615.  
  616. function IsSliding () {
  617. return (grounded && sliding.enabled && TooSteep());
  618. }
  619.  
  620. function IsTouchingCeiling () {
  621. return (movement.collisionFlags & CollisionFlags.CollidedAbove) != 0;
  622. }
  623.  
  624. function IsGrounded () {
  625. return grounded;
  626. }
  627.  
  628. function TooSteep () {
  629. return (groundNormal.y <= Mathf.Cos(controller.slopeLimit * Mathf.Deg2Rad));
  630. }
  631.  
  632. function GetDirection () {
  633. return inputMoveDirection;
  634. }
  635.  
  636. function SetControllable (controllable : boolean) {
  637. canControl = controllable;
  638. }
  639.  
  640. // Project a direction onto elliptical quater segments based on forward, sideways, and backwards speed.
  641. // The function returns the length of the resulting vector.
  642. function MaxSpeedInDirection (desiredMovementDirection : Vector3) : float {
  643. if (desiredMovementDirection == Vector3.zero)
  644. return 0;
  645. else {
  646. var zAxisEllipseMultiplier : float = (desiredMovementDirection.z > 0 ? movement.maxForwardSpeed : movement.maxBackwardsSpeed) / movement.maxSidewaysSpeed;
  647. var temp : Vector3 = new Vector3(desiredMovementDirection.x, 0, desiredMovementDirection.z / zAxisEllipseMultiplier).normalized;
  648. var length : float = new Vector3(temp.x, 0, temp.z * zAxisEllipseMultiplier).magnitude * movement.maxSidewaysSpeed;
  649. return length;
  650. }
  651. }
  652.  
  653. function SetVelocity (velocity : Vector3) {
  654. grounded = false;
  655. movement.velocity = velocity;
  656. movement.frameVelocity = Vector3.zero;
  657. SendMessage("OnExternalVelocity");
  658. }
  659.  
  660. function Run () {
  661. maxForwardSpeed = 10.0;
  662. maxSidewaysSpeed = 10.0;
  663. maxBackwardsSpeed = 10.0;
  664. }
  665.  
  666.  
  667. // Require a character controller to be attached to the same game object
  668. @script RequireComponent (CharacterController)
  669. @script AddComponentMenu ("Character/Character Motor")
  670.  
  671. function Start () {
  672. player.animation.Stop("prone");
  673. player.animation.Stop("prone up");
  674. player.animation.Stop("crouch");
  675. player.animation.Stop("crouch up");
  676. }
Advertisement
Add Comment
Please, Sign In to add comment