PlayerMovement.cs (11628B)
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 //namespace Slimecing.Slime { } 6 [RequireComponent(typeof(Rigidbody))] 7 public class PlayerMovement : MonoBehaviour { 8 Rigidbody rb; //Rigidbody for the slime 9 [HideInInspector] 10 public Animator SlimeAnim; //Animation Controller for slime 11 12 public float speed; //Movement speed of slime 13 public float rotSpeed; //Rotation speed of slime 14 public float dashForce; //Amount of force applied forward on slime when dashing (Dashing is AddForce) 15 public float dashLength; //Time of dash. How long the dash goes until velocity is set to zero 16 public float dashCooldown; //Time until can dash again 17 public float thrustDebuffTime; //How long thrusting debuf will last after a thrust 18 public float hitTime; 19 public float hitSpeed = 100f; 20 public float fallRange = 0.2f; 21 public float slowSpeed = 0.1f; 22 23 public Coroutine killMovement; 24 public Coroutine getHit; 25 26 private Vector3 movement; //Handling movement of character 27 private Vector3 hitDirection; 28 private Vector3 lastSlowPos; 29 30 public Vector3 blockCenter; 31 32 private float charge = 1; //Dash charge (Mostly here for held dash) 33 //private float previousY; 34 35 private bool isDashing = false; //When you dash this is set to true for when the dash button is down (Dash process started) 36 private bool dashCooldownSwitch = true; //When this is false you are unable to dash. Set true when thrustdebuffTime has expired 37 //private bool isInBounds = true; 38 39 private SwordScript swordScript; //Holder for the sword script 40 private WinChecker winChecker; 41 public ParticleSystem SlimeTrail; //SlimeTrail particlesystem 42 43 private float timer = 0; 44 45 [HideInInspector] 46 public float currentSpeed; //Current speed of player used to change speed without losing set speed. Used by SwordScript 47 [HideInInspector] 48 public bool thrustDebuff; //Is true when the player is experiencing a debuf. Used by SwordScript 49 [HideInInspector] 50 public bool isHit; 51 [HideInInspector] 52 public bool isSlowing; 53 [HideInInspector] 54 public bool dashProcess = false; //On when the player starts a dash and false when dash ends. For swordscript to change offset of sword. 55 56 public bool MouseInput; 57 public string inputDash; 58 public string inputVer; 59 public string inputHori; 60 public string inputHoriSw; 61 public string inputVerSw; 62 public string inputThru; 63 public string emoteOne; 64 65 public bool isAlive; 66 67 private float h; 68 private float v; 69 70 public float slimePercentage = 0; 71 public float minPercent = -2; 72 public float maxPercent = 100; 73 74 public bool inv; 75 76 public float health; 77 public GameObject Sword; 78 public GameObject Hat; 79 private void Start() 80 { 81 isAlive = true; 82 83 inv = true; 84 85 currentSpeed = speed; //Set current speed to set speed 86 rb = GetComponent<Rigidbody>(); //Get rigidbody of slime character 87 SlimeAnim = transform.GetChild(0).GetComponent<Animator>(); //Get the animator of slime (Located as child as of test model) 88 89 swordScript = Sword.GetComponent<SwordScript>(); //Find the sword and the attached script 90 91 GameObject dataH = GameObject.Find("DataHandler"); 92 winChecker = dataH.GetComponent<WinChecker>(); 93 health = PublicStatHandler.GetInstance().health; 94 95 StartCoroutine(IFrames(0.4f)); 96 } 97 private void Update () { 98 99 movement = Vector3.zero; //Set movement to zero to be changed later. Makes for accurate movement by frame 100 101 //Input for movement 102 h = Input.GetAxis(inputHori); 103 v = Input.GetAxis(inputVer); 104 //Set to a vector 105 movement = new Vector3(h, 0, v); 106 107 //Setting rotation of player. Only is set when movement is not zero (Not always rotating). Under thrust debuff rotation does not happen. 108 if (movement != Vector3.zero && !thrustDebuff) 109 { 110 //Slerp rotation to movement vector. Slerp just makes the rotation smooth. 111 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), rotSpeed); 112 } 113 114 //Dash starter. Can't dash when dash button not pressed, dash button is already pressed, dash is under cooldown 115 if(Input.GetAxis(inputDash) != 0 && !isDashing && dashCooldownSwitch) 116 { 117 //You are preparing to dash 118 isDashing = true; 119 //Dash cooldown has started 120 dashCooldownSwitch = false; 121 122 //Charge functionality 123 /*charge += 0.5f; 124 125 if (charge > 100) 126 { 127 charge = 100; 128 }*/ 129 } 130 131 //Dash button is released 132 if(Input.GetAxis(inputDash) == 0 && isDashing) 133 { 134 //dashing no longer pressed 135 isDashing = false; 136 //Start dash 137 Dash(); 138 } 139 //previousY = transform.position.y; 140 141 HealthCheck(); 142 } 143 144 //Handels adding the force and starting the necessary coroutines for a dash 145 private void Dash() 146 { 147 148 if (AudioManager.getInstance() != null) 149 { 150 AudioManager.getInstance().Find("dash").source.Play(); 151 } 152 //You have started your dash process 153 dashProcess = true; 154 155 //Start a dash animation 156 SlimeAnim.Play("SLIDE"); 157 158 //Add the dash force to the player 159 rb.AddForce(transform.forward * (dashForce * charge)); 160 //Start the dash length coroutine 161 StartCoroutine(StopDash(dashLength)); 162 //Start the dash cooldown 163 StartCoroutine(DashCoolDown(dashCooldown)); 164 //charge = 0; 165 } 166 167 public void startHitSequence(float forceAddOn, float percent) 168 { 169 slimePercentage += percent; 170 if (slimePercentage > maxPercent) 171 { 172 slimePercentage = maxPercent; 173 } 174 else if (slimePercentage < minPercent) 175 { 176 slimePercentage = minPercent; 177 } 178 isHit = true; 179 hitDirection = transform.position - blockCenter; 180 hitDirection = new Vector3(hitDirection.x, 0f, hitDirection.z); 181 if (!inv) 182 { 183 rb.AddForce(hitDirection * /*hitSpeed **/ (forceAddOn + slimePercentage)); 184 } 185 Debug.Log(this.name + " Force: " + rb.velocity.magnitude); 186 isHit = false; 187 getHit = StartCoroutine(gotHit(hitTime)); 188 } 189 190 public void startHitSequence(float forceAddOn, float percent, float mod) 191 { 192 slimePercentage += percent; 193 if (slimePercentage > maxPercent) 194 { 195 slimePercentage = maxPercent; 196 } 197 else if (slimePercentage < minPercent) 198 { 199 slimePercentage = minPercent; 200 } 201 isHit = true; 202 hitDirection = transform.position - blockCenter; 203 hitDirection = new Vector3(hitDirection.x, 0f, hitDirection.z); 204 if (!inv) 205 { 206 rb.AddForce(hitDirection * /*hitSpeed **/ (forceAddOn + (slimePercentage * mod))); 207 } 208 Debug.Log(this.name + " Force: " + rb.velocity.magnitude); 209 isHit = false; 210 getHit = StartCoroutine(gotHit(hitTime)); 211 } 212 213 private void HealthCheck() 214 { 215 if (health <= 0 && isAlive) 216 { 217 Debug.Log("Dead " + this.name); 218 isAlive = false; 219 winChecker.Dead(this.gameObject); 220 } 221 } 222 //Basically swiches on a switch when cooldown is done so player can dash again 223 private IEnumerator DashCoolDown(float waitTime) 224 { 225 yield return new WaitForSeconds(waitTime); 226 dashCooldownSwitch = true; 227 } 228 229 //No longer in dash process, velocity is set to zero to stop huge dash force. 230 private IEnumerator StopDash(float waitTime) 231 { 232 yield return new WaitForSeconds(waitTime); 233 dashProcess = false; 234 rb.velocity = Vector3.zero; 235 } 236 237 private IEnumerator IFrames (float waitTime) 238 { 239 yield return new WaitForSeconds(waitTime); 240 inv = false; 241 } 242 243 //Started after thrust changes speed back to normal and kills the debuff switch 244 public IEnumerator KillMovement(float waitTime) 245 { 246 yield return new WaitForSeconds(waitTime); 247 currentSpeed = speed; 248 thrustDebuff = false; 249 } 250 251 public IEnumerator gotHit(float hitTime) 252 { 253 hitDirection = transform.position - blockCenter; 254 yield return new WaitForSeconds(hitTime); 255 isHit = false; 256 isSlowing = true; 257 } 258 259 //Handeling stuff like movement and dash that will depend heavily on frame rate 260 private void FixedUpdate() 261 { 262 //move to movement vector 263 if (!isHit) 264 { 265 rb.MovePosition(rb.position + movement * currentSpeed * Time.fixedDeltaTime); 266 } 267 268 //Set sword to behind player when dash is started 269 if (dashProcess) 270 { 271 swordScript.transform.position = transform.position + swordScript.Radius * -transform.forward.normalized; 272 } 273 274 timer += Time.deltaTime; 275 276 if (movement != Vector3.zero && timer >= 0.1f) 277 { 278 SlimeTrail.Play(); 279 timer = 0; 280 } 281 282 /*if ((Input.GetKey("w") || Input.GetKey("d") || Input.GetKey("s") || Input.GetKey("a")) && timer >= 0.1) //If any inputs are inputted it plays the SlimeTrail particle system 283 { 284 SlimeTrail.Play(); 285 timer = 0; 286 }*/ 287 288 } 289 290 private void OnTriggerEnter(Collider other) 291 { 292 if (other.tag == "LevelBounds") 293 { 294 CameraController camController = Camera.main.GetComponent<CameraController>(); 295 296 Transform existingSlime = ExistsInCamList(camController.players, this.gameObject); 297 if (existingSlime == null) 298 { 299 Debug.Log("Slime In Bounds!"); 300 camController.players.Add(transform); 301 } 302 } 303 } 304 305 private void OnTriggerExit(Collider other) 306 { 307 if (other.tag == "LevelBounds") 308 { 309 CameraController camController = Camera.main.GetComponent<CameraController>(); 310 311 Transform existingSlime = ExistsInCamList(camController.players, this.gameObject); 312 if (existingSlime != null) 313 { 314 Debug.Log("Slime Out Of Bounds!"); 315 camController.players.Remove(existingSlime); 316 } 317 318 } 319 } 320 321 private Transform ExistsInCamList(List<Transform> pList, GameObject slime) 322 { 323 Transform existingPlayer = null; 324 foreach (Transform slimeTransform in pList) 325 { 326 if(slimeTransform == slime.transform) 327 { 328 existingPlayer = slimeTransform; 329 } 330 } 331 return existingPlayer; 332 } 333 334 public void stopCoroutines() 335 { 336 StopCoroutine(killMovement); 337 } 338 339 public void setUpControls(int i) 340 { 341 //int currentController = PublicStatHandler.GetInstance().controllers[i]; 342 343 MouseInput = PublicStatHandler.GetInstance().Inputs[i].mouseInput; 344 inputDash = PublicStatHandler.GetInstance().Inputs[i].dashInput; 345 inputVer = PublicStatHandler.GetInstance().Inputs[i].yMoveAxis; 346 inputHori = PublicStatHandler.GetInstance().Inputs[i].xMoveAxis; 347 inputHoriSw = PublicStatHandler.GetInstance().Inputs[i].xSwordAxis; 348 inputVerSw = PublicStatHandler.GetInstance().Inputs[i].ySwordAxis; 349 inputThru = PublicStatHandler.GetInstance().Inputs[i].ThrustInput; 350 emoteOne = PublicStatHandler.GetInstance().Inputs[i].emoteOne; 351 } 352 }