private void OnJumpMovement(Transform transform, AnimationEventManager animationEventManager, Animator animator, Rigidbody rigidbody, Entity entity, Actor actor, ref ActorInput actorInput, ActorCharacter actorCharacter) { //Save Jump Data if (!jumpIntervals.ContainsKey(entity.Index)) { jumpIntervals.Add(entity.Index, jumpInterval); } //Decrease jump interval if goruned if (isGrounded && rigidbody.velocity.y <= 0) { jumpIntervals[entity.Index] -= 1 * dt; actorInput.isJumping = 0; } //If were not grounded reset jump interval else { jumpIntervals[entity.Index] = jumpInterval; } //Do Jump if (actorInput.action == 0 && actorInput.actionToDo == 2 && isGrounded && actorCharacter.jumpForce != 0 && isHeadFree && jumpIntervals[entity.Index] <= 0) { var velocity = rigidbody.velocity; velocity.y = actorCharacter.jumpForce; rigidbody.velocity = velocity; animator.SetTrigger("jump"); actorInput.isJumping = 1; if (actorInput.crouch == 1) { ActorUtilities.UpdateCollider(actor, transform, "standing"); } if (actorInput.movement.magnitude != 0) { transform.forward = actorInput.movement; } } //Sound { if (animationEventManager.RequestEvent("jumpGrunt") && actorCharacter.jumpGruntAudioEvent != null) { actorCharacter.jumpGruntAudioEvent.Play(transform.position); } else if (animationEventManager.RequestEvent("jumpStart") && actorCharacter.jumpStartAudioEvent != null) { actorCharacter.jumpStartAudioEvent.Play(transform.position); } else if (animationEventManager.RequestEvent("jumpLand") && actorCharacter.jumpLandAudioEvent != null) { actorCharacter.jumpLandAudioEvent.Play(transform.position); } } }
private void OnWallHugMovement(Transform transform, AnimationEventManager animationEventManager, Animator animator, Rigidbody rigidbody, Entity entity, Actor actor, ref ActorInput actorInput, ActorCharacterWallHug actorCharacterWallHug) { // start wall hugging if (actorInput.actionToDo == 1 && actorInput.crouch == 0 && actorInput.action == 0) { // get all surrounding walls | Get first hit as our main hit var hits = Physics.SphereCastAll(transform.position, 0.5f, Vector3.one * 0.5f, 0.5f, actorCharacterWallHug.wallHugMask); if (hits.Length >= 1) { var hit = hits[0]; var directionToWall = (hit.transform.position - transform.position).normalized; var distanceToWall = Vector3.Distance(hit.transform.position, transform.position); RaycastHit wallHit; if (Physics.Raycast(transform.position + new Vector3(0, 0.5f, 0), directionToWall, out wallHit, distanceToWall, actorCharacterWallHug.wallHugMask)) { wallHuggingDirections[entity.Index] = Quaternion.LookRotation(GetMeshColliderNormal(wallHit)).eulerAngles; Debug.DrawLine(transform.position + new Vector3(0, 0.5f, 0), wallHit.point, Color.red, 1); Debug.DrawRay(wallHit.point, GetMeshColliderNormal(wallHit) * 5, Color.red, 1); actorInput.actionToDo = 0; actorInput.action = 99; } } } //Stop wall hugging if (actorInput.actionToDo == 1 && actorInput.action == 99) { actorInput.actionToDo = 0; actorInput.action = 0; } //Update Wall hugging if (actorInput.action == 99) { bool forceStop = false; //Move if (actorInput.movement.magnitude >= deadPoint) { //Movement var movement = actorInput.movement; var movementToRightDotProduct = Vector3.Dot(movement, transform.right); rigidbody.velocity = transform.right * movementToRightDotProduct * actorCharacterWallHug.speed; //Check if there is wall ahead of where we are going if not than force a stop { //Do it var raycastPoint = transform.position; raycastPoint += Vector3.up * 0.1f; raycastPoint += (transform.right * movementToRightDotProduct).normalized * 0.5f; forceStop = !Physics.Raycast(raycastPoint, -transform.forward, 0.5f, actorCharacterWallHug.wallHugMask); //Debug Debug.DrawRay(raycastPoint, -transform.forward * 0.5f, forceStop ? Color.red : Color.green, 0); } //Animation if (!forceStop) { animator.SetFloat("movementX", movementToRightDotProduct * deadPoint, animationTransitionRate, dt); animator.SetFloat("movementY", 0, animationTransitionRate, dt); } } //Stop Move if (actorInput.movement.magnitude <= deadPoint || forceStop) { //Movement rigidbody.velocity = Vector3.zero; //Animation animator.SetFloat("movementX", 0, animationTransitionRate, dt); animator.SetFloat("movementY", 0, animationTransitionRate, dt); } //Rotate to face wall normal transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, wallHuggingDirections[entity.Index], Time.deltaTime * 6); //Sound if (animationEventManager.RequestEvent("footStep") && actorCharacterWallHug.footStepAudioEvent != null && actorInput.movement.magnitude != 0) { actorCharacterWallHug.footStepAudioEvent.Play(transform.position); } } }
private void OnGroundMovement(Transform transform, AnimationEventManager animationEventManager, Animator animator, Rigidbody rigidbody, Entity entity, Actor actor, ref ActorInput actorInput, ActorCharacter actorCharacter) { //Return if doing diffrent action | Not Grounded if (actorInput.action != 0 || !isGrounded || actorInput.isJumping == 1) { return; } //Get movement type { if (!isHeadFree && actorInput.crouchPreviousFrame == 1) { actorInput.crouch = 1; } if (actorInput.movement.magnitude == 0 || actorInput.crouch == 1 && !isHeadFree) { actorInput.sprint = 0; } if (actorInput.sprint == 1) { actorInput.crouch = 0; } if (actorInput.walk == 1 && actorInput.sprint == 0 && actorInput.crouch == 0) { actorInput.movement *= deadPoint; } } //Move { //Set Velocity var velocity = actorInput.movement; velocity *= actorInput.sprint == 1 ? actorCharacter.sprintSpeed : actorInput.crouch == 1 ? actorCharacter.crouchSpeed : actorCharacter.runSpeed; velocity.y = -1; rigidbody.velocity = velocity; } //Collider { //Set Collider if (actorInput.crouch == 1 && actorInput.crouchPreviousFrame == 0) { ActorUtilities.UpdateCollider(actor, transform, "Crouching"); } else if (actorInput.crouch == 0 && actorInput.crouchPreviousFrame == 1) { ActorUtilities.UpdateCollider(actor, transform, "Standing"); } } //Rotate { if (actorInput.movement.magnitude != 0 && actorInput.strafe == 0 || actorInput.movement.magnitude != 0 && actorInput.sprint == 1) { var lookRotation = Quaternion.LookRotation(actorInput.movement); transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, dt * actorCharacter.rotationSpeed); } } //Animate { //Is Sprinting if (actorInput.sprint == 1) { actorInput.movement = new float3(0, 0, 2f); } //Strafing else if (actorInput.strafe == 1) { actorInput.movement = transform.InverseTransformDirection(actorInput.movement); } //Not Strafing else { actorInput.movement = new float3(0, 0, Mathf.Abs(actorInput.movement.magnitude)); } //Set Animator Properties animator.SetBool("crouch", actorInput.crouch == 1 && actorInput.sprint == 0); animator.SetFloat("movementX", actorInput.movement.x, animationTransitionRate, dt); animator.SetFloat("movementY", actorInput.movement.z, animationTransitionRate, dt); animator.SetFloat("movementAmount", actorInput.movement.magnitude, animationTransitionRate, dt); } //Sound if (animationEventManager.RequestEvent("footStep") && actorCharacter.footStepAudioEvent != null && actorInput.movement.magnitude != 0) { actorCharacter.footStepAudioEvent.Play(transform.position); } }