//set defence on/off private void Defend(bool defend) { animator.SetAnimatorBool("Defend", defend); if (defend) { //keep current direction while defending if (!canTurnWhileDefending) { int rot = Mathf.RoundToInt(transform.rotation.eulerAngles.y); if (rot >= 180 && rot <= 256) { currentDirection = DIRECTION_P2.Left; } else { currentDirection = DIRECTION_P2.Right; } } TurnToDir(currentDirection); SetVelocity(Vector3.zero); playerState.SetState(UNITSTATE.DEFEND); } else { playerState.SetState(UNITSTATE.IDLE); } }
//awake void Awake() { if (animator == null) { animator = GetComponent <Animator>(); } isplayer = transform.parent.CompareTag("Player"); currentDirection = DIRECTION_P2.Right; }
//movement input event void MovementInputEvent(Vector2 inputVector) { int dir = Mathf.RoundToInt(Mathf.Sign((float)-inputVector.x)); if (Mathf.Abs(inputVector.x) > 0) { currentDirection = (DIRECTION_P2)dir; } }
//adds small force over time IEnumerator AddForceCoroutine(float force) { DIRECTION_P2 startDir = currentDirection; Rigidbody rb = transform.parent.GetComponent <Rigidbody>(); float speed = 8f; float t = 0; while (t < 1) { yield return(new WaitForFixedUpdate()); rb.velocity = Vector2.right * (int)startDir * Mathf.Lerp(force, rb.velocity.y, MathUtilities.Sinerp(0, 1, t)); t += Time.fixedDeltaTime * speed; yield return(null); } }
//move while on the ground void MoveGrounded() { //don't move while landing from a jump if (playerState.currentState == UNITSTATE.LAND) { return; } //set rigidbody velocity if (rb != null && (inputDirection.sqrMagnitude > 0) && !WallInFront() && PlayerInsideCamViewArea()) { 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); } //allow up/down movement when the player is at the edge of the screen if (!PlayerInsideCamViewArea() && Mathf.Abs(inputDirection.y) > 0) { Vector3 dirToCam = (transform.position - Camera.main.transform.position) * inputDirection.y; SetVelocity(new Vector3(dirToCam.x, rb.velocity.y + Physics.gravity.y * Time.fixedDeltaTime, dirToCam.z)); } //set current direction based on the input vector. (ignore up and down by using 'mathf.sign' because we want the player to stay in the current direction when moving up/down) int dir = Mathf.RoundToInt(Mathf.Sign((float)-inputDirection.x)); if (Mathf.Abs(inputDirection.x) > 0) { currentDirection = (DIRECTION_P2)dir; } //send movement speed to animator if (animator) { animator.SetAnimatorFloat("MovementSpeed", rb.velocity.magnitude); } //look towards traveling direction LookToDir(currentDirection); }
//look towards a direction public void LookToDir(DIRECTION_P2 dir) { Vector3 newDir = Vector3.zero; if (dir == DIRECTION_P2.Right || dir == DIRECTION_P2.Left) { if (isGrounded) { newDir = Vector3.RotateTowards(transform.forward, Vector3.forward * -(int)dir, rotationSpeed * Time.deltaTime, 0.0f); } else { newDir = Vector3.RotateTowards(transform.forward, Vector3.forward * -(int)dir, jumpRotationSpeed * Time.deltaTime, 0.0f); } transform.rotation = Quaternion.LookRotation(newDir); currentDirection = dir; } }
private void doAttack(DamageObject d, UNITSTATE state, INPUTACTION_2 inputAction) { animator.SetAnimatorTrigger(d.animTrigger); playerState.SetState(state); lastAttack = d; lastAttack.inflictor = gameObject; lastAttackTime = Time.time; lastAttackInput = inputAction; lastAttackDirection = currentDirection; TurnToDir(currentDirection); SetVelocity(Vector3.zero); if (state == UNITSTATE.JUMPKICK) { return; } if (state == UNITSTATE.JUMPPUNCH) { return; //LETHAL FORCES - adding Jump Punch } Invoke("Ready", d.duration); }
//use the currently equipped weapon void useCurrentWeapon() { playerState.SetState(UNITSTATE.USEWEAPON); TurnToDir(currentDirection); SetVelocity(Vector3.zero); lastAttackInput = INPUTACTION_2.WEAPONATTACK; lastAttackTime = Time.time; lastAttack = currentWeapon.damageObject; lastAttack.inflictor = gameObject; lastAttackDirection = currentDirection; if (!string.IsNullOrEmpty(currentWeapon.damageObject.animTrigger)) { animator.SetAnimatorTrigger(currentWeapon.damageObject.animTrigger); } if (!string.IsNullOrEmpty(currentWeapon.useSound)) { GlobalAudioPlayer.PlaySFX(currentWeapon.useSound); } Invoke("Ready", currentWeapon.damageObject.duration); if (currentWeapon.degenerateType == DEGENERATETYPE.DEGENERATEONUSE) { currentWeapon.useWeapon(); } //on last use if (currentWeapon.degenerateType == DEGENERATETYPE.DEGENERATEONUSE && currentWeapon.timesToUse == 0) { StartCoroutine(destroyCurrentWeapon(currentWeapon.damageObject.duration)); } if (currentWeapon.degenerateType == DEGENERATETYPE.DEGENERATEONHIT && currentWeapon.timesToUse == 1) { StartCoroutine(destroyCurrentWeapon(currentWeapon.damageObject.duration)); } }
//set a direction public void SetDirection(DIRECTION_P2 dir) { currentDirection = dir; }
//set current direction public void SetDirection(DIRECTION_P2 dir) { currentDirection = dir; LookToDir(currentDirection); }
//turn towards a direction public void TurnToDir(DIRECTION_P2 dir) { transform.rotation = Quaternion.LookRotation(Vector3.forward * -(int)dir); }
//knockDown sequence public IEnumerator KnockDownSequence(GameObject inflictor) { playerState.SetState(UNITSTATE.KNOCKDOWN); animator.StopAllCoroutines(); yield return(new WaitForFixedUpdate()); //look towards the direction of the incoming attack int dir = inflictor.transform.position.x > transform.position.x ? 1 : -1; currentDirection = (DIRECTION_P2)dir; TurnToDir(currentDirection); //update playermovement var pm = GetComponent <PlayerMovement_P2>(); if (pm != null) { pm.CancelJump(); pm.SetDirection(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"); CamShake camShake = Camera.main.GetComponent <CamShake>(); if (camShake != null) { camShake.Shake(.3f); } animator.ShowDustEffectLand(); //sfx GlobalAudioPlayer.PlaySFXAtPosition("Drop", transform.position); //ground slide 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(null); } //knockDown Timeout SetVelocity(Vector3.zero); yield return(new WaitForSeconds(KnockdownTimeout)); //stand up animator.SetAnimatorTrigger("StandUp"); playerState.currentState = UNITSTATE.STANDUP; yield return(new WaitForSeconds(KnockdownStandUpTime)); playerState.currentState = UNITSTATE.IDLE; }