void Update() { if (!Player || !DebugText) { return; //This script will not update if there is no Player or Text assigned. } //Now that we're sure we have the necessary components, we debug Sonic's current state using its type. string StateDebug = "Current State: " + PlayerActions.currentState.GetType().ToString(); //Next we split Sonic's velocity to planar and vertical so we can debug our ground speed and air speed. HedgeMath.SplitPlanarVector(Player.rigidBody.velocity, Player.GroundNormal, out Vector3 GroundSpeed, out Vector3 AirSpeed); string GroundDebug = "Ground Speed: " + GroundSpeed.magnitude.ToString("F2") + "m/s"; string AirDebug = "Air Speed: " + AirSpeed.magnitude.ToString("F2") + "m/s"; //Set the debug text DebugText.text = StateDebug + "\n" + GroundDebug + "\n" + AirDebug; }
// Update is called once per frame void Update() { //Here we'll be setting all the main animator values. HedgeMath.SplitPlanarVector(player.rigidBody.velocity, player.GroundNormal, out Vector3 Ground, out Vector3 Air); Vector3 LocalAirVelocity = transform.InverseTransformDirection(Air); animator.SetInteger("State", Actions.StateIndex); animator.SetBool("Grounded", player.Grounded); animator.SetFloat("AirSpeed", LocalAirVelocity.y); animator.SetBool("Crouching", player.Crouching); animator.SetBool("Braking", player.Skidding); animator.SetBool("Dead", PlayerHealth.IsDead); SpinBall.GetComponent <Animator>().SetFloat("Speed", Ground.magnitude); SpinBall.SetActive(PlayerActions.currentState is ActionJump); if (PlayerActions.currentState is ActionSpindash) { animator.SetFloat("GroundSpeed", Actions.spinDashState.SpinDashCharge); if (!SpinDashParticles.isPlaying) { SpinDashParticles.Play(); } } else { animator.SetFloat("GroundSpeed", Ground.magnitude); if (SpinDashParticles.isPlaying) { SpinDashParticles.Stop(); } } //Set turn amount ///To get the turn amount, we first get which direction Sonic is turning by using a dot product between Sonic's right vector and Sonic's velocity. ///If the dot is positive, he is turning right. If it is negative, he is turning left. ///Once we have the turning amount, we set the actual Turn float. If the rotation amount is above the dead zone, we lerp Turning to the turn amount. ///If it is under the dead zone, we lerp it to 0. DeltaRotation = Vector3.Dot(transform.right, player.rigidBody.velocity.normalized) * 10; DeltaRotation = HedgeMath.ClampFloat(DeltaRotation, -1, 1); if (DeltaRotation > RotationDeadZone || DeltaRotation < -RotationDeadZone) { Turning = Mathf.Lerp(Turning, DeltaRotation, Time.deltaTime * TurnSmoothing); } else { Turning = Mathf.Lerp(Turning, 0f, Time.deltaTime * TurnSmoothing); } animator.SetFloat("HorizontalInput", Turning); ///Here we're simply playing and stopping the speed line particles depending on whether or not Sonic is up to speed ///and whether or not the particles are already playing. if (player.rigidBody.velocity.magnitude >= ParticleStartSpeed && !SpeedParticles.isPlaying) { SpeedParticles.Play(); } else if (player.rigidBody.velocity.magnitude < ParticleStartSpeed && SpeedParticles.isPlaying) { SpeedParticles.Stop(); } }