Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using DG.Tweening;
  4.  
  5. public class movement_controller : MonoBehaviour {
  6.  
  7. [HideInInspector]
  8. public float distanceToGround;
  9. public float skinThickness = 0.1f;
  10. public float curMovementSpeed;
  11. public float maxVelocity;
  12. public bool isGrounded = false;
  13. [HideInInspector]
  14. public bool wasGrounded = false;
  15. public bool touchDown = false;
  16. public bool isJumping = false;
  17. public bool isMoving = false;
  18.  
  19. public bool canMove = true;
  20. public Vector3 velocity;
  21. public Rigidbody rb;
  22. public Character_Animation ca;
  23. public CapsuleCollider collider;
  24.  
  25. public Transform currentGround;
  26.  
  27. public float lastRotation;
  28.  
  29. public void Start()
  30. {
  31. collider = this.GetComponent<CapsuleCollider>();
  32. rb = this.GetComponent<Rigidbody>();
  33. distanceToGround = this.GetComponent<Collider>().bounds.extents.y;
  34. }
  35.  
  36. public void Update()
  37. {
  38. velocity = rb.velocity;
  39.  
  40. Vector3 velo = rb.velocity;
  41.  
  42. velo.x = Mathf.Clamp(velo.x, -maxVelocity, maxVelocity);
  43. velo.z = Mathf.Clamp(velo.z, -maxVelocity, maxVelocity);
  44. rb.velocity = velo;
  45.  
  46. wasGrounded = isGrounded;
  47. Ray groundedRay = new Ray(transform.position, -Vector3.up);
  48. RaycastHit groundedHit;
  49. isGrounded = Physics.Raycast(groundedRay, out groundedHit, distanceToGround + skinThickness);
  50. if (groundedHit.transform != null && groundedHit.transform.gameObject.layer == LayerMask.NameToLayer("ground"))
  51. currentGround = groundedHit.transform;
  52. else
  53. currentGround = null;
  54. }
  55.  
  56. public void AddForce(Vector3 _force)
  57. {
  58. rb.AddForce(_force);
  59. }
  60.  
  61. public void SetRotation(float _rot)
  62. {
  63. _rot = Mathf.Round(_rot / 45) * 45;
  64. lastRotation = _rot;
  65. if (canMove)
  66. rb.rotation = (Quaternion.Euler(new Vector3(0, _rot, 0)));
  67. else
  68. {
  69. Vector3 rotation = this.transform.rotation.eulerAngles;
  70. rotation.y = _rot;
  71. this.transform.rotation = Quaternion.Euler(rotation);
  72. }
  73. }
  74.  
  75. public void SetCanMove(bool _canMove)
  76. {
  77. canMove = _canMove;
  78. rb.isKinematic = !_canMove;
  79. }
  80.  
  81. public void SetPosition(Vector3 _pos, float _rot, float _time)
  82. {
  83. StartCoroutine(SetPositionLoop(_pos, _rot, _time));
  84. }
  85.  
  86. public IEnumerator SetPositionLoop(Vector3 _pos, float _rot, float _time)
  87. {
  88. isMoving = true;
  89. this.transform.DOMove(_pos, _time);
  90. SetRotation(GetAngleBetweenPoints(this.transform.position, _pos));
  91. yield return new WaitForSeconds(_time);
  92. SetRotation(_rot);
  93. isMoving = false;
  94. }
  95.  
  96. public float GetAngleBetweenPoints(Vector3 _pos1, Vector3 _pos2)
  97. {
  98. float deltaZ = _pos1.z - _pos2.z;
  99. float deltaX = _pos1.x - _pos2.x;
  100. return (float)Mathf.Atan2(deltaZ, deltaX) * Mathf.Rad2Deg;
  101. }
  102.  
  103. public void TouchDown(float _time = 0.25f)
  104. {
  105. StartCoroutine(TouchDownLoop(_time));
  106. }
  107.  
  108. public IEnumerator TouchDownLoop(float _time)
  109. {
  110. touchDown = true;
  111. yield return new WaitForSeconds(_time);
  112. touchDown = false;
  113. }
  114.  
  115. /// <summary>
  116. /// Sets the collider of the character.
  117. /// </summary>
  118. /// <param name="_collider"></param>
  119. public void SetCollider(PhysicMaterial _collider)
  120. {
  121. collider.material = _collider;
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement