Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.51 KB | None | 0 0
  1.  
  2.    
  3. 1
  4. 2
  5. 3
  6. 4
  7. 5
  8. 6
  9. 7
  10. 8
  11. 9
  12. 10
  13. 11
  14. 12
  15. 13
  16. 14
  17. 15
  18. 16
  19. 17
  20. 18
  21. 19
  22. 20
  23. 21
  24. 22
  25. 23
  26. 24
  27. 25
  28. 26
  29. 27
  30. 28
  31. 29
  32. 30
  33. 31
  34. 32
  35. 33
  36. 34
  37. 35
  38. 36
  39. 37
  40. 38
  41. 39
  42. 40
  43. 41
  44. 42
  45. 43
  46. 44
  47. 45
  48. 46
  49. 47
  50. 48
  51. 49
  52. 50
  53. 51
  54. 52
  55. 53
  56. 54
  57. 55
  58. 56
  59. 57
  60. 58
  61. 59
  62. 60
  63. 61
  64. 62
  65. 63
  66. 64
  67. 65
  68. 66
  69. 67
  70. 68
  71. 69
  72. 70
  73. 71
  74. 72
  75. 73
  76. 74
  77. 75
  78. 76
  79. 77
  80. 78
  81. 79
  82. 80
  83. 81
  84. 82
  85. 83
  86. 84
  87. 85
  88. 86
  89. 87
  90. [RequireComponent(typeof (CapsuleCollider))]
  91. [RequireComponent(typeof (Grounding))]
  92. [RequireComponent(typeof (Rigidbody))]
  93. public class Actor : MonoBehaviour {
  94.  
  95.     [Header("Parameters")]
  96.     public float ForwardSpeed;
  97.     public float LateralSpeed;
  98.     public float BackwardSpeed;
  99.  
  100.     [Header("Controls")]
  101.     public bool Controller;
  102.     public KeyboardMap KeyboardBindings;
  103.     public ControllerMap ControllerBindings;
  104.  
  105.     [Header("Abilities")]
  106.     public Ability PrimaryAbility = new Ability();
  107.     public Ability SecondaryAbility = new Ability();
  108.     public Ability SpecialAbility1 = new Ability();
  109.     public Ability SpecialAbility2 = new Ability();
  110.     public Ability SpecialAbility3 = new Ability();
  111.     public Ability MiscellaneousAbility = new Ability();
  112.  
  113.     private Animator animator_;
  114.     private Grounding grounding_;
  115.     private Rigidbody body_;
  116.  
  117.     protected void Start() {
  118.         animator_ = GetComponent<Animator>();
  119.         grounding_ = GetComponent<Grounding>();
  120.         body_ = GetComponent<Rigidbody>();
  121.     }
  122.  
  123.     public virtual void MoveForward() {
  124.         body_.MovePosition(transform.position + transform.forward * ForwardSpeed);
  125.     }
  126.  
  127.     public virtual void MoveBackward() {
  128.         body_.MovePosition(transform.position - transform.forward * BackwardSpeed);
  129.     }
  130.  
  131.     public virtual void MoveLeft() {
  132.         body_.MovePosition(transform.position - transform.right * LateralSpeed);
  133.     }
  134.  
  135.     public virtual void MoveRight() {
  136.         body_.MovePosition(transform.position + transform.right * LateralSpeed);
  137.     }
  138.  
  139.     public virtual void Jump() {
  140.         if (grounding_.Grounded) {
  141.             body_.AddForce(transform.up * 5, ForceMode.Impulse);
  142.         }
  143.     }
  144.  
  145. }
  146.  
  147. // Grounding class for enabling/disabling jump. Somewhat buggy :,(
  148.  
  149. using UnityEngine;
  150.  
  151. [RequireComponent(typeof(BoxCollider))]
  152. public class Grounding : MonoBehaviour {
  153.  
  154.     public BoxCollider Region;
  155.  
  156.     public bool Grounded { get; private set; }
  157.  
  158.     protected void FixedUpdate() {
  159.         Grounded = false;
  160.     }
  161.  
  162.     // TODO : Layer-based implementation.
  163.     protected void OnTriggerStay(Collider other) {
  164.         Grounded = other.gameObject != gameObject;
  165.     }
  166.  
  167. }
  168.  
  169.  
  170. // TODO : @Mopzilla
  171. // Add velocity to Actor. You'll want it to increase on Update if you're moving forward and decrease if you're not. Use the velocity to scale body_.MovePosition's argument.
  172. // Make sure you keep current velocity and velocity from last frame.
  173. // When you send your guy into ragdoll mode, do this:
  174. // acceleration = (this frame's velocity - last frame's velocity) / time.DeltaTime
  175. // body_.AddForce(mass of force * acceleration, ForceMode.Impulse);
  176. // There are a lot of ways you could do velocity, like sampling a curve, using lerp, etc. I'll leave that for you to experiment.
  177. // To add turning, in the move left/right functions instead of doing what I did by moving their position due west/east, you would continue to move your character forward and a little bit to the left/right.
  178. // You can get really creative with turning by using Lerp and a smoothing function to make him turn a little bit, then gradually more, for realism.
  179. // Message me if you need any help!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement