protected virtual void CrouchHandler() { float capsuleHeight = colliderHeight; float capsuleVelociy = wasColliderVelocityCenter; if (TPCInput.GetButtonDown("Crouch") && !isCrouching) { isCrouching = !isCrouching; } else if (TPCInput.GetButtonDown("Crouch") && isCrouching) { if (!Physics.Raycast(transform.position, Vector3.up, colliderHeight)) { isCrouching = !isCrouching; } } if (isCrouching) { capsuleHeight = colliderHeight * crouchHeight; capsuleVelociy = wasColliderVelocityCenter * crouchHeight; } float lastCapsuleHeight = capsuleCollider.height; capsuleCollider.height = Mathf.Lerp(capsuleCollider.height, capsuleHeight, crouchSmooth * Time.deltaTime); float fixedVelocity = Mathf.Lerp(capsuleCollider.center.y, capsuleVelociy, crouchSmooth * Time.deltaTime); capsuleCollider.center = new Vector3(0, fixedVelocity, 0); float fixedVerticalPosition = Mathf.Lerp(transform.position.y, transform.position.y + (capsuleCollider.height - lastCapsuleHeight) / 2.0f, crouchSmooth * Time.deltaTime); transform.position = new Vector3(transform.position.x, fixedVerticalPosition, transform.position.z); }
private void JumpHandler() { if (TPCInput.GetButtonDown("Jump")) { Jump(); } }
private void SprintHandler() { isSprinting = TPCInput.GetButton("Sprint"); if (MoveAmount > 0 && isSprinting) { if (speed > 0) { speed = 2; } else if (speed < 0) { speed = -2; } if (direction > 0) { direction = 2; } else if (direction < 0) { direction = -2; } } }
public virtual void ControlSpeed(float velocity) { if (Time.deltaTime == 0) { return; } this.velocity = velocity; if (useRootMotion) { Vector3 v = (animator.deltaPosition * (velocity > 0 ? velocity : 1f)) / Time.deltaTime; v.y = rigidbody.velocity.y; rigidbody.velocity = Vector3.Lerp(rigidbody.velocity, v, 20f * Time.deltaTime); } else { Vector3 velY = transform.forward * velocity * Mathf.Clamp(speed, -1, 1); velY.y = rigidbody.velocity.y; Vector3 velX = transform.right * velocity * Mathf.Clamp(direction, -1, 1); velX.x = rigidbody.velocity.x; if (isStrafing) { Vector3 v = (transform.TransformDirection(new Vector3(TPCInput.GetAxis("Horizontal"), 0, TPCInput.GetAxis("Vertical"))) * (velocity > 0 ? velocity : 1f)); v.y = rigidbody.velocity.y; rigidbody.velocity = Vector3.Lerp(rigidbody.velocity, v, 20f * Time.deltaTime); } else { rigidbody.velocity = velY; rigidbody.AddForce(transform.forward * (velocity * Mathf.Clamp(speed, -1, 1)) * Time.deltaTime, ForceMode.VelocityChange); } } }
/// <summary> /// Update is called every frame, if the MonoBehaviour is enabled. /// </summary> /// <summary> protected virtual void Update() { if (TPCInput.GetButtonDown("Grab") && !isGrabbing) { RaycastHit raycastHit; if (Physics.Raycast(_camera.position, _camera.forward, out raycastHit, grabRange, layerMask)) { grabObject = raycastHit.transform.GetComponent <GrabObject>(); if (raycastHit.rigidbody != null && !raycastHit.rigidbody.isKinematic && grabObject != null) { grabObject.transform.rotation = Quaternion.LookRotation(character.transform.forward); springJoint.transform.position = raycastHit.point; springJoint.anchor = Vector3.zero; springJoint.spring = spring; springJoint.damper = damper; springJoint.maxDistance = distance; springJoint.connectedBody = raycastHit.rigidbody; isGrabbing = true; StartCoroutine(DragObject(grabObject.Range)); } } } else if ((TPCInput.GetButtonDown("Grab") && isGrabbing) || !characterHealth.IsAlive) { inverseKinematics.SetHandIKWeight(0); grabObject = null; isGrabbing = false; } }
/// <summary> /// Change distance between camera and character by scrolling mouse wheel /// </summary> protected virtual void ScrollingCameraDistance() { if (!scrollCameraDistance) { return; } float mouseWheelAxis = TPCInput.GetAxis("Mouse Wheel") * scrollSensitivity; float scrolledDistance = defaultDistance + (mouseWheelAxis * Time.deltaTime); defaultDistance = Mathf.Clamp(scrolledDistance, minDistance, maxDistance); }
/// <summary> /// Cursor processing coroutine /// </summary> /// <returns>yield</returns> protected System.Collections.IEnumerator CursorProcess() { Cursor.visible = false; Cursor.lockState = CursorLockMode.Locked; while (true) { if (TPCInput.GetButtonDown("Menu") && !Cursor.visible) { Cursor.lockState = CursorLockMode.None; Cursor.visible = true; } else if (TPCInput.GetButtonDown("Menu") && Cursor.visible) { Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; } yield return(null); } }
/// <summary> /// Update the targetDirection variable using referenceTransform or just input.Rotate by word the referenceDirection /// </summary> /// <param name="referenceTransform"></param> public virtual void UpdateTargetDirection(Transform referenceTransform = null) { if (referenceTransform) { Vector3 forward = keepDirection ? referenceTransform.forward : referenceTransform.TransformDirection(Vector3.forward); forward.y = 0; forward = keepDirection ? forward : referenceTransform.TransformDirection(Vector3.forward); forward.y = 0; //set to 0 because of referenceTransform rotation on the X axis //get the right-facing direction of the referenceTransform Vector3 right = keepDirection ? referenceTransform.right : referenceTransform.TransformDirection(Vector3.right); // determine the direction the player will face based on input and the referenceTransform's right and forward directions targetDirection = TPCInput.GetAxis("Horizontal") * right + TPCInput.GetAxis("Vertical") * forward; } else { targetDirection = keepDirection ? targetDirection : new Vector3(TPCInput.GetAxis("Horizontal"), 0, TPCInput.GetAxis("Vertical")); } }
protected virtual void CameraHandle() { float vertical = TPCInput.GetAxis("Mouse X"); float horizontal = TPCInput.GetAxis("Mouse Y"); RotateCamera(vertical, horizontal); if (characterMotor.LockMovement) { return; } // tranform Character direction from camera if not KeepDirection if (!characterMotor.KeepDirection) { characterMotor.UpdateTargetDirection(transform); } // rotate the character with the camera while strafing if (characterMotor.IsStrafing && characterMotor.MoveAmount > 0) { characterMotor.RotateWithAnotherTransform(transform); } }
private bool StepOffset() { if (Mathf.Sqrt(MoveAmount) < 0.1 || !isGrounded) { return(false); } RaycastHit _hit = new RaycastHit(); Vector3 _movementDirection = isStrafing && MoveAmount > 0 ? (transform.right * TPCInput.GetAxis("Horizontal") + transform.forward * TPCInput.GetAxis("Vertical")).normalized : transform.forward; Ray rayStep = new Ray((transform.position + new Vector3(0, stepOffsetEnd, 0) + _movementDirection * ((capsuleCollider).radius + 0.05f)), Vector3.down); if (Physics.Raycast(rayStep, out _hit, stepOffsetEnd - stepOffsetStart, groundLayer) && !_hit.collider.isTrigger) { if (_hit.point.y >= (transform.position.y) && _hit.point.y <= (transform.position.y + stepOffsetEnd)) { float _speed = isStrafing ? Mathf.Clamp(MoveAmount, 0, 1) : Mathf.Clamp(speed, -1, 1); Vector3 velocityDirection = isStrafing ? (_hit.point - transform.position) : (_hit.point - transform.position).normalized; rigidbody.velocity = velocityDirection * stepSmooth * (_speed * (velocity > 1 ? velocity : 1)); return(true); } } return(false); }
public virtual void StrafeMovement() { speed = Mathf.Clamp(TPCInput.GetAxis("Vertical"), -1, 1); direction = Mathf.Clamp(TPCInput.GetAxis("Horizontal"), -1, 1); }