//movement on the ground void MoveGrounded() { //do nothing when landing if (playerState.currentState == UNITSTATE.LAND) { return; } //set rigidbody velocity if (rb != null && (inputDirection.sqrMagnitude > 0 && !WallInFront())) { rb.velocity = new Vector3(inputDirection.x * -walkSpeed, rb.velocity.y + Physics.gravity.y * Time.fixedDeltaTime, inputDirection.y * -ZSpeed); if (animator) { animator.SetAnimatorFloat("MovementSpeed", rb.velocity.magnitude); } playerState.SetState(UNITSTATE.WALK); } else { rb.velocity = new Vector3(0, rb.velocity.y + Physics.gravity.y * Time.fixedDeltaTime, 0); if (animator) { animator.SetAnimatorFloat("MovementSpeed", 0); } playerState.SetState(UNITSTATE.IDLE); } }
//movement on the ground void MoveGrounded() { //do nothing when landing if (playerState.currentState == UNITSTATE.LAND) { return; } //move when there is no wall in front of us and input is detected if (rb != null && (inputDirection.sqrMagnitude > 0 && !WallInFront())) { //set movement speed to run speed or walk speed depending on the current state float movementSpeed = playerState.currentState == UNITSTATE.RUN? runSpeed : walkSpeed; rb.velocity = new Vector3(inputDirection.x * -movementSpeed, rb.velocity.y + Physics.gravity.y * Time.fixedDeltaTime, inputDirection.y * -ZSpeed); if (animator) { animator.SetAnimatorFloat("MovementSpeed", rb.velocity.magnitude); } } else { //stop moving, but still apply gravity rb.velocity = new Vector3(0, rb.velocity.y + Physics.gravity.y * Time.fixedDeltaTime, 0); if (animator) { animator.SetAnimatorFloat("MovementSpeed", 0); } playerState.SetState(UNITSTATE.IDLE); } //sets the run state in the animator to true or false animator.SetAnimatorBool("Run", playerState.currentState == UNITSTATE.RUN); }
//jump IEnumerator doJump() { //set jump state jumpInProgress = true; playerState.SetState(UNITSTATE.JUMPING); //play animation animator.SetAnimatorBool("JumpInProgress", true); animator.SetAnimatorTrigger("JumpUp"); animator.ShowDustEffectJump(); //play sfx if (jumpUpVoice != "") { GlobalAudioPlayer.PlaySFXAtPosition(jumpUpVoice, transform.position); } //set state yield return(new WaitForFixedUpdate()); //start jump while (isGrounded) { SetVelocity(Vector3.up * JumpForce); yield return(new WaitForFixedUpdate()); } //continue until we hit the ground while (!isGrounded) { yield return(new WaitForFixedUpdate()); } //land playerState.SetState(UNITSTATE.LAND); SetVelocity(Vector3.zero); animator.SetAnimatorFloat("MovementSpeed", 0f); animator.SetAnimatorBool("JumpInProgress", false); animator.SetAnimatorBool("JumpKickActive", false); animator.SetAnimatorBool("JumpPunchActive", false); //LETHAL FORCES - Adding Jump Punch animator.ShowDustEffectLand(); //sfx GlobalAudioPlayer.PlaySFX("FootStep"); if (jumpLandVoice != "") { GlobalAudioPlayer.PlaySFXAtPosition(jumpLandVoice, transform.position); } jumpInProgress = false; if (playerState.currentState == UNITSTATE.LAND) { yield return(new WaitForSeconds(landRecoveryTime)); setPlayerState(UNITSTATE.IDLE); } }
public void WalkTo(float proximityRange, float movementMargin) { Vector3 dirToTarget; LookAtTarget(target.transform); enemyState = UNITSTATE.WALK; if (enemyTactic == ENEMYTACTIC.ENGAGE) { dirToTarget = target.transform.position - (transform.position + new Vector3(0, 0, Mathf.Clamp(ZSpread, 0, attackRangeDistance))); } else { dirToTarget = target.transform.position - (transform.position + new Vector3(0, 0, ZSpread)); } //距离玩家太远了,靠近点 if (distance >= proximityRange) { moveDirection = new Vector3(dirToTarget.x, 0, dirToTarget.z); if (IsGrounded()) { Move(moveDirection.normalized, walkSpeed); anim.SetAnimatorFloat("MovementSpeed", rb.velocity.sqrMagnitude); return; } } //距离玩家太近了,走远点 if (distance <= proximityRange - movementMargin) { moveDirection = new Vector3(-dirToTarget.x, 0, 0); if (IsGrounded()) { Move(moveDirection.normalized, walkBackwardSpeed); anim.SetAnimatorFloat("MovementSpeed", -rb.velocity.sqrMagnitude); return; } } }
private void MoveGrounded() { if (rb != null && inputDirection.sqrMagnitude > 0) { SetVelocity(new Vector3(inputDirection.x * -walkSpeed, rb.velocity.y + Physics.gravity.y * Time.fixedDeltaTime, inputDirection.y * -zSpeed)); SetPlayerState(UNITSTATE.WALK); } else { SetVelocity(new Vector3(0, rb.velocity.y + Physics.gravity.y * Time.fixedDeltaTime, 0)); SetPlayerState(UNITSTATE.IDLE); } //set current direction based on the input vector.(ignore up and down by using 'mathf.sign' because we want the player to stay int current direction when moving up/down) int dir = Mathf.RoundToInt(Mathf.Sign((float)-inputDirection.x)); if (Mathf.Abs(inputDirection.x) > 0) { currentDirection = (DIRECTION)dir; } LookToDir(currentDirection); animator.SetAnimatorFloat("MovementSpeed", rb.velocity.magnitude); }
//knockDown sequence IEnumerator KnockDownSequence(GameObject inflictor) { enemyState = UNITSTATE.KNOCKDOWN; yield return(new WaitForFixedUpdate()); //look towards the direction of the incoming attack int dir = 1; if (inflictor != null) { dir = inflictor.transform.position.x > transform.position.x? 1 : -1; } currentDirection = (DIRECTION)dir; animator.SetDirection(currentDirection); TurnToDir(currentDirection); //add knockback force animator.SetAnimatorTrigger("KnockDown_Up"); while (IsGrounded()) { SetVelocity(new Vector3(KnockbackForce * -dir, KnockdownUpForce, 0)); yield return(new WaitForFixedUpdate()); } //going up... while (rb.velocity.y >= 0) { yield return(new WaitForFixedUpdate()); } //going down animator.SetAnimatorTrigger("KnockDown_Down"); while (!IsGrounded()) { yield return(new WaitForFixedUpdate()); } //hit ground animator.SetAnimatorTrigger("KnockDown_End"); GlobalAudioPlayer.PlaySFXAtPosition("Drop", transform.position); animator.SetAnimatorFloat("MovementSpeed", 0f); animator.ShowDustEffectLand(); enemyState = UNITSTATE.KNOCKDOWNGROUNDED; Move(Vector3.zero, 0f); //cam shake CamShake camShake = Camera.main.GetComponent <CamShake>(); if (camShake != null) { camShake.Shake(.3f); } //dust effect animator.ShowDustEffectLand(); //stop sliding float t = 0; float speed = 2; Vector3 fromVelocity = rb.velocity; while (t < 1) { SetVelocity(Vector3.Lerp(new Vector3(fromVelocity.x, rb.velocity.y + Physics.gravity.y * Time.fixedDeltaTime, fromVelocity.z), new Vector3(0, rb.velocity.y, 0), t)); t += Time.deltaTime * speed; yield return(new WaitForFixedUpdate()); } //knockDown Timeout Move(Vector3.zero, 0f); yield return(new WaitForSeconds(KnockdownTimeout)); //stand up enemyState = UNITSTATE.STANDUP; animator.SetAnimatorTrigger("StandUp"); Invoke("Ready", standUpTime); }