Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. ng using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerControls : MonoBehaviour
  6. {
  7. public float speed = 3f;
  8. public float forwardSpeed = 5f;
  9.  
  10. public Vector3 velocity;
  11. public float horizontalVelocityCap = 5f;
  12. public float verticalVelocityCap = 6f;
  13. public float gravity = 5f;
  14. public float wallRunningGravity = 2f;
  15. public float jumpStrength = 2f;
  16.  
  17. public HitBox leftWall;
  18. public HitBox rightWall;
  19. public HitBox floor;
  20.  
  21. enum Mode { Floor, LeftWall, RightWall }
  22. private Mode mode = Mode.Floor;
  23. enum Side { Left, Right }
  24. private Side side = Side.Left;
  25.  
  26. void Update()
  27. {
  28. switch(mode)
  29. {
  30. case Mode.Floor:
  31. HandleFloorControls();
  32. break;
  33. case Mode.LeftWall:
  34. case Mode.RightWall:
  35. HandleWallRunningControls();
  36. break;
  37. }
  38.  
  39.  
  40. if (floor.colliding && velocity.y < 0)
  41. velocity.y = 0f;
  42. else if (velocity.y < -verticalVelocityCap)
  43. velocity.y = -verticalVelocityCap;
  44.  
  45. if (velocity.x > horizontalVelocityCap)
  46. velocity.x = horizontalVelocityCap;
  47. else if (velocity.x < -horizontalVelocityCap)
  48. velocity.x = -horizontalVelocityCap;
  49.  
  50.  
  51. velocity.z = forwardSpeed;
  52.  
  53. transform.position += velocity * Time.deltaTime;
  54.  
  55. }
  56.  
  57. private void HandleFloorControls()
  58. {
  59. if (!leftWall.colliding && Input.GetKey(KeyCode.LeftArrow))
  60. {
  61. velocity += Vector3.left * speed;
  62. }
  63. if (!rightWall.colliding && Input.GetKey(KeyCode.RightArrow))
  64. {
  65. velocity = Vector3.right * speed;
  66. }
  67. if (Input.GetKeyDown(KeyCode.Space))
  68. {
  69. if(leftWall.colliding)
  70. mode = Mode.LeftWall;
  71. else if (rightWall.colliding)
  72. mode = Mode.RightWall;
  73. else
  74. {
  75. velocity += jumpStrength * Vector3.up;
  76. }
  77. }
  78.  
  79. velocity += gravity * Vector3.down;
  80. }
  81.  
  82. private void HandleWallRunningControls()
  83. {
  84.  
  85. velocity += wallRunningGravity * Vector3.down;
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement