/* * Physics Update. */ private void FixedUpdate() { speed = input; bool justLanded = becameGrounded && !kinMotor.IsGrounded; bool justFell = (!kinMotor.IsJumping || !jump) && !becameGrounded && kinMotor.IsGrounded; becameGrounded = kinMotor.IsGrounded; //Can check here the landing frame. if (justLanded) { animator.SetBool(JumpFallAnimationState, false); } //Can check here the falling frame. if (justFell) { animator.SetBool(JumpFallAnimationState, true); } /* * If pressed lock, check for enemies close by and lock camera to closest */ if (pressedLocking) { //Reset variables when clicking to lock closestEnemy = null; pressedLocking = false; //Try to lock to the closest enemy isLocked = LockCameraToClosestEnemy(); } /* * If is in locked combat but moves too far away, disengage camera lock */ if (isLocked) { float distance = Vector3.Distance(closestEnemy.transform.position, rb.position); if (distance > 5f) { isLocked = false; closestEnemy = null; myCamera.LockCameraOnTarget(null); } } //Set arms animation to locked animator.SetBool(FightStanceAnimationState, isLocked); /* * Dodge press; */ if (dodge) { dodge = false; isDodging = true; } /* * Check speed magnitude from leg part. * Dodge / Sprint / Normal run */ if (isDodging) { timerDodge += Time.deltaTime; //timerDodge starts as 0 and ends at 200 speed *= (bodyParts.LegPart.FlashStepSpeed - (timerDodge * timerDodge * 10 * 40)); if (timerDodge >= .05f) { isDodging = false; timerDodge = 0f; } } else if (isSprinting) { speed *= bodyParts.LegPart.RunningSpeed; } else { speed *= bodyParts.LegPart.MovementSpeed; } //Set animation type for walking/sprinting if (speed.magnitude > 0) { if (isSprinting) { animator.SetBool(SprintingAnimationState, true); } else { animator.SetBool(WalkingAnimationState, true); animator.SetBool(SprintingAnimationState, false); } } else { animator.SetBool(WalkingAnimationState, false); animator.SetBool(SprintingAnimationState, false); } kinMotor.Move(speed); bodyParts.SetMovementState(speed, kinMotor.IsJumping, isSprinting && speed.magnitude > 0); }
private void FixedUpdate() { float Radius = 1f; int numbOfNearbyCols = Physics.OverlapSphereNonAlloc(transform.position, Radius + 0.1f, nearbyColliders); RaycastHit groundInfo; Vector3 platformSpeed = Vector3.zero; Vector3 walkingVector = transform.forward; Vector3 sideWalkingVector = transform.right; if (numbOfNearbyCols > 1) //if its colliding with something check grounding { //Check grounding if (becameGrounded = CheckGrounding(out groundInfo)) { platformSpeed = AddSpeedFromPlatform(groundInfo.collider); GetWalkingVector(groundInfo, out walkingVector, out sideWalkingVector); Debug.DrawRay(transform.position, walkingVector * 5, Color.magenta); } } else { becameGrounded = false; } speed = (walkingVector * input.z + sideWalkingVector * input.x); if (isSprinting) { speed *= bodyParts.LegPart.RunningSpeed; } else { speed *= bodyParts.LegPart.MovementSpeed; } bool justLanded = becameGrounded && !isGrounded; bool justFell = (!isJumping || !jump) && !becameGrounded && isGrounded; //can check here the landing frame if (justLanded) { Debug.Log("landed"); } if (justFell) { Debug.Log("fell"); } isGrounded = becameGrounded; if (!isGrounded || (isSlopeSliding && !isGrounded)) { gravitySpeed += fallSpeedIncrementer; speed.y -= gravitySpeed; } if (jump) { jump = false; isJumping = true; becameGrounded = false; isGrounded = false; } if (isJumping) { speed.y += JumpSpeed; JumpSpeed = Mathf.Max(0, JumpSpeed - Time.deltaTime * Time.deltaTime * 2000); } Vector3 disp = Vector3.zero; if (numbOfNearbyCols > 1) { disp = DePenetrateCollisions(ref speed, numbOfNearbyCols); } bodyParts.SetMovementState(speed, isJumping, isSprinting); rb.MovePosition(rb.position + disp + (platformSpeed + speed) * Time.deltaTime); }