Advertisement
Guest User

allahuaaakbar

a guest
Jul 28th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. public class Movement : MonoBehaviour {
  7.  
  8. public bool Ragdoll = false;
  9. public int RagdollReset = 0;
  10. public Rigidbody ChildRB;
  11. public Collider ChildCD;
  12. public List<Rigidbody> ChildRBS;
  13. public Animator Animator;
  14. public Rigidbody Body;
  15. public List<Collider> ChildCDS;
  16. public int Change = 0;
  17.  
  18. [Header("Parameters")]
  19. public float ForwardSpeed;
  20. public float LateralSpeed;
  21. public float BackwardSpeed;
  22.  
  23. // Use this for initialization
  24. void Start () {
  25.  
  26. List<Collider> ChildCDS = new List<Collider>(GetComponentsInChildren<Collider>());
  27. List<Rigidbody> ChildRBS = new List<Rigidbody> (GetComponentsInChildren<Rigidbody> ());
  28.  
  29. ChildCDS.Remove (this.GetComponent<Collider>());
  30. ChildRBS.Remove (this.GetComponent<Rigidbody>());
  31.  
  32. Animator = GetComponent<Animator> ();
  33. Body = GetComponent<Rigidbody>();
  34.  
  35. }
  36.  
  37. public virtual void MoveForward() {
  38. Body.MovePosition(transform.position + transform.forward * ForwardSpeed);
  39. }
  40.  
  41. public virtual void MoveBackward() {
  42. Body.MovePosition(transform.position - transform.forward * BackwardSpeed);
  43. }
  44.  
  45. public virtual void MoveLeft() {
  46. this.transform.Rotate (0, LateralSpeed * -1, 0);
  47. }
  48.  
  49. public virtual void MoveRight() {
  50. this.transform.Rotate (0, LateralSpeed * 1, 0);
  51. }
  52.  
  53. // Update is called once per frame
  54. void Update () {
  55.  
  56. List<Collider> ChildCDS = new List<Collider>(GetComponentsInChildren<Collider>());
  57. List<Rigidbody> ChildRBS= new List<Rigidbody> (GetComponentsInChildren<Rigidbody>());
  58.  
  59. if (Input.GetKey(KeyCode.W) && Ragdoll == false) {
  60. MoveForward ();
  61. }
  62.  
  63. if (Input.GetKey(KeyCode.S) && Ragdoll == false) {
  64. MoveBackward ();
  65. }
  66.  
  67. float Move = Input.GetAxis ("Vertical");
  68. Animator.SetFloat ("Speed", Move);
  69.  
  70. RagdollReset++;
  71.  
  72. if (Input.GetKeyDown (KeyCode.E) && RagdollReset > 1 && Ragdoll == true) {
  73. Ragdoll = false;
  74. RagdollReset = 0;
  75. Change = 0;
  76. }
  77.  
  78. if (Input.GetKeyDown (KeyCode.E) && RagdollReset > 1 && Ragdoll == false || Input.GetKeyDown (KeyCode.Space) && RagdollReset > 1 && Ragdoll == false) {
  79. Ragdoll = true;
  80. RagdollReset = 0;
  81. Change = 0;
  82. }
  83.  
  84. if (Ragdoll == false && Change < 1) {
  85.  
  86. Change++;
  87.  
  88. foreach (Rigidbody ChildRB in ChildRBS) {
  89. ChildRB.isKinematic = true;
  90. }
  91.  
  92. foreach (Collider ChildCD in ChildCDS) {
  93. ChildCD.enabled = false;
  94. }
  95.  
  96. Animator = this.GetComponent<Animator> ();
  97.  
  98. Animator.enabled = true;
  99.  
  100. } else if (Change < 1) {
  101.  
  102. Change++;
  103.  
  104. foreach (Rigidbody ChildRB in ChildRBS) {
  105. ChildRB.isKinematic = false;
  106. }
  107.  
  108. foreach (Collider ChildCD in ChildCDS) {
  109. ChildCD.enabled = true;
  110. }
  111.  
  112. Animator.enabled = false;
  113.  
  114. }
  115. }
  116. }
  117.  
  118. // TODO : @Mopzilla
  119. // 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.
  120. // Make sure you keep current velocity and velocity from last frame.
  121. // When you send your guy into ragdoll mode, do this:
  122. // acceleration = (this frame's velocity - last frame's velocity) / time.DeltaTime
  123. // body_.AddForce(mass of force * acceleration, ForceMode.Impulse);
  124. // 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.
  125. // 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.
  126. // 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.
  127. // Message me if you need any help!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement