Advertisement
Downtion

CharacterController BasicMovement

Feb 26th, 2019
1,338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.85 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class C_BasicCC : MonoBehaviour
  6. {
  7. [Header("External Refernces")]
  8. public Transform T_CameraObj; //The Game Camera itself
  9. private CharacterController CC_PlayersCC;
  10.  
  11. [Header("Player Settings")]
  12. public float F_PlayerSpeed; //Custom Player Speed
  13. public float F_Accerlation; // is the change of current speed over time
  14. public float F_PlayerLowestTurnSpeed; // player turn speed of rotation
  15. public float F_PlayerTurnSpeed; // player turn speed of rotation
  16. public float F_PlayerHighestTurnSpeed; // player turn speed of rotation
  17. public float F_PlayerGravity; //Force to push the player back to ground
  18. public float F_OnGroundGravity;
  19. public float F_PlayerJumpHeight; //max jump height
  20.  
  21. [Header("Stored Player Information")]
  22. public float F_CurrentAngle;
  23. public float F_CurrentTurnSpeed;
  24. public bool B_PlayerIsGrounded;
  25. private RaycastHit RCH_AmITouching; //Information that the bottom of the character is grounded or not
  26. public Vector2 V2_Input; //Used to store Player input from the InputManager
  27. public Vector3 V3_IntendedDirection; //Stores intended direction the player is expected to be going
  28. public Vector3 V3_Velocity; //Current Player speed
  29. public Vector3 V3_VelocityXZ; //Current Player speed
  30. public Quaternion Q_PlayerRotation;
  31.  
  32. [Header("Stored Camera Information")]
  33. public Vector3 V3_CamForwardDirection; //used to store our cameras forward direction
  34. public Vector3 V3_CamRightDirection; //used to store our cameras right direction
  35.  
  36. // Start is called before the first frame update
  37. void Start()
  38. {
  39. CC_PlayersCC = GetComponent<CharacterController>();
  40. }
  41.  
  42. // Update is called once per frame
  43. void Update()
  44. {
  45. DoInput();
  46. CalculateCamera();
  47. CalculateGround();
  48. DoMove();
  49. DoGravity();
  50. DoJump();
  51.  
  52. CC_PlayersCC.Move(V3_Velocity * Time.deltaTime); //moves the player with the character Controller
  53. }
  54.  
  55. void DoInput()
  56. {
  57. //Movement Section (1/2)
  58. V2_Input = new Vector2(Input.GetAxis("Horizontal_Keyboard"), Input.GetAxis("Vertical_Keyboard")); //Grabtranform Equal to new Vector2(GetIM_Hori, GetIM_Vert)
  59. V2_Input = Vector2.ClampMagnitude(V2_Input, 1); //Grab Input values and make them equal to a new "clamped" value. This smoothes out movement
  60. }
  61.  
  62. void CalculateCamera()
  63. {
  64. //Camera Section (1/1)
  65. V3_CamForwardDirection = T_CameraObj.forward; //Sets the vector3 CamForDir to the cameras forward direction
  66. V3_CamRightDirection = T_CameraObj.right; //Sets the vector3 CamRighDir to the cameras right direction
  67. V3_CamForwardDirection.y = 0;
  68. V3_CamRightDirection.y = 0;
  69. V3_CamForwardDirection = V3_CamForwardDirection.normalized; //Normalize values
  70. V3_CamRightDirection = V3_CamRightDirection.normalized; //Normalize values
  71. }
  72.  
  73. void OnControllerColliderHit(ControllerColliderHit hit)
  74. {
  75. F_CurrentAngle = Vector3.Angle(Vector3.up, hit.normal); //Calc angle between normal and character
  76. }
  77.  
  78. void CalculateGround()
  79. {
  80. //CC_PlayersCC.isGrounded
  81. //Physics.Raycast(transform.position + Vector3.up * 0.1f, -Vector3.up, out RCH_AmITouching, 0.2f) //Old code for collision detection
  82. if (CC_PlayersCC.isGrounded)
  83. {
  84. B_PlayerIsGrounded = true;
  85. //!!!!!!!!!!!!!!!!!insert slope value detection for preventing player from climbing mountains!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  86. //also include a bool to switch from normal mode to slide mode?
  87. }
  88. else
  89. {
  90. B_PlayerIsGrounded = false;
  91. }
  92. }
  93.  
  94. void DoMove()
  95. {
  96. //Movement Section (2/2)
  97. //Both of these deleted codes have a problem where they move gameobject buts dont allow collision detection in real-time
  98. //transform.position += new Vector3(V2_Input.x, 0, V2_Input.y) * Time.deltaTime * F_Speed; // This moves the object but it isnt camera relative movement
  99. //transform.position += (V3_CamForwardDirection * V2_Input.y + V3_CamRightDirection * V2_Input.x) * Time.deltaTime * F_PlayerSpeed; //Moves object & it's camera relative direction movement
  100. //CC_PlayersCC.Move((V3_CamForwardDirection * V2_Input.y + V3_CamRightDirection * V2_Input.x) * Time.deltaTime * F_PlayerSpeed); //Moves object & it's camera relative direction movement
  101. V3_IntendedDirection = V3_CamForwardDirection * V2_Input.y + V3_CamRightDirection * V2_Input.x; //Compares the players direction and where the camera is looking
  102.  
  103. F_CurrentTurnSpeed = V3_Velocity.magnitude / 5; // Current Speed we are running at
  104. F_PlayerTurnSpeed = Mathf.Lerp(F_PlayerHighestTurnSpeed, F_PlayerLowestTurnSpeed, F_CurrentTurnSpeed); //Caculating Needed Turn Speed
  105.  
  106. if (V2_Input.magnitude > 0) //Only apply these if you actually doing input
  107. {
  108. Q_PlayerRotation = Quaternion.LookRotation(V3_IntendedDirection); //creates a rotation from the intended direction we give it
  109.  
  110. transform.rotation = Quaternion.Lerp(transform.rotation, Q_PlayerRotation, F_PlayerTurnSpeed * Time.deltaTime); //rotates our character into that direction
  111. }
  112.  
  113. V3_VelocityXZ = V3_Velocity; //added to assist vertical movement & to separate velocity into groups for modification
  114. V3_VelocityXZ.y = 0; //added to assist vertical movement & to separate velocity into groups for modification
  115.  
  116. //V3_Velocity = V3_IntendedDirection * F_PlayerSpeed; // This instantly moves the player with no build up
  117. //V3_Velocity = Vector3.Lerp(V3_Velocity, V3_IntendedDirection * F_PlayerSpeed, F_Accerlation * Time.deltaTime); // This slowly moves the player to its speed instead of instantly having it
  118. V3_VelocityXZ = Vector3.Lerp(V3_VelocityXZ, transform.forward * V2_Input.magnitude * F_PlayerSpeed, F_Accerlation * Time.deltaTime);
  119. V3_Velocity = new Vector3(V3_VelocityXZ.x, V3_Velocity.y, V3_VelocityXZ.z); //added to assist vertical movement & to separate velocity into groups for modification
  120. }
  121.  
  122. void DoGravity()
  123. {
  124. if (B_PlayerIsGrounded)
  125. {
  126. V3_Velocity.y = F_OnGroundGravity; //OnGround Gravity
  127. }
  128. else
  129. {
  130. V3_Velocity.y -= F_PlayerGravity * Time.deltaTime; //subtract velocity.y with gravity
  131. V3_Velocity.y = Mathf.Clamp(V3_Velocity.y, -10, 10); //limited the gravity & velocity !!!You can implement a hold jump longer to get higher here
  132. }
  133. }
  134.  
  135. void DoJump()
  136. {
  137. if (B_PlayerIsGrounded == true)
  138. {
  139. if (Input.GetButtonDown("Jump"))
  140. {
  141. V3_Velocity.y = F_PlayerJumpHeight;
  142. }
  143. }
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement