Vector3 CalculateMoveVelocityTarget(FpsControllerInput input) { // input movement Vector3 moveDesired = new Vector3(input.moveX, 0, input.moveZ); // normalize input to prevent diagonal speed up if (moveDesired.sqrMagnitude > 1f) { moveDesired = moveDesired.normalized; } return(transform.rotation * moveDesired * moveSpeed * weaponMoveMult); }
public FpsControllerInput ProvideInput() { FpsControllerInput input = new FpsControllerInput(); input.lookX = Input.GetAxisRaw("Mouse X") * mouseSensitivity; input.lookY = -Input.GetAxisRaw("Mouse Y") * mouseSensitivity * DsLib.Math.BoolToSign(!mouseInverted); input.moveX = Input.GetAxisRaw("Horizontal"); input.moveZ = Input.GetAxisRaw("Vertical"); input.jump = Input.GetKey(KeyCode.Space); input.fire = Input.GetKey(KeyCode.Mouse0); input.aim = Input.GetKey(KeyCode.Mouse1); return(input); }
void Update() { FpsControllerInput input = new FpsControllerInput(); if (getInput != null) { input = getInput(); // mouselook X transform.Rotate(Vector3.up, input.lookX); // mouselook Y pitch = Mathf.Clamp(pitch + input.lookY, -pitchLimit, pitchLimit); headCamera.localEulerAngles = Vector3.right * pitch; Cursor.lockState = CursorLockMode.Locked; } }
// Update is called once per frame void FixedUpdate() { if (scrFpsController.getInput == null) { return; } FpsControllerInput input = scrFpsController.getInput(); if (input.aim) // always shoulder weapon when aim is pressed { scrFpsWeaponPivot.shouldering = true; headCamera.fieldOfView = Mathf.Lerp(headCamera.fieldOfView, headCameraOrigFov * zoomFovMult, zoomLerp); weaponCamera.fieldOfView = Mathf.Lerp(weaponCamera.fieldOfView, weaponCameraOrigFov * zoomFovMult, zoomLerp); scrFpsController.weaponMoveMult = moveAimMultiplier; } else { scrFpsWeaponPivot.shouldering = false; headCamera.fieldOfView = Mathf.Lerp(headCamera.fieldOfView, headCameraOrigFov, zoomLerp); weaponCamera.fieldOfView = Mathf.Lerp(weaponCamera.fieldOfView, weaponCameraOrigFov, zoomLerp); scrFpsController.weaponMoveMult = moveMultiplier; } if (input.fire) { Fire(); } else { fireReleased = true; } fireTimer.Update(Time.fixedDeltaTime); reloadTimer.Update(Time.fixedDeltaTime); }
void FixedUpdate() { groundInfo = GroundRaycast(transform.position); // determine move input FpsControllerInput input = new FpsControllerInput(); if (getInput != null) { input = getInput(); } // reset jump input if (grounded && input.jump == false) { jumpReleased = true; } // determine walking velocity to move toward Vector3 moveVelocityTarget = CalculateMoveVelocityTarget(input); // determine how to interact with ground (either stand on it, land on it, step up/down or fall) if (Mathf.Abs(groundInfo.distance) <= stepHeight && groundInfo.upwardAngle <= slopeMaxAngle && body.velocity.y <= jumpSpeed * jumpSpeedLandPercentage) // check if grounded { if (!grounded) { // land if (body.velocity.y <= -minLandSfxSpeed) { sfxLand.Play(scrListener.personalEffects); vibLand.Play(scrListener.personalEffects); } EnterGrounded(); } //step transform.position = Vector3.Lerp( transform.position, transform.position + Vector3.down * groundInfo.distance, stepLerp); } if (grounded) { // enter free fall if not touching any ground if (groundInfo.distance == Mathf.Infinity) { EnterFreefall(); } // if touching steep slope, start falling if there is no level ground nearby else if (groundInfo.upwardAngle > slopeMaxAngle && groundInfo.upwardAngle < 90) { SurfaceInfo checkForFooting = GroundRaycast(transform.position + Math.OnlyHorizontal(groundInfo.normal).normalized *radius); if (checkForFooting.distance == Mathf.Infinity || checkForFooting.upwardAngle > slopeMaxAngle) { EnterFreefall(); } } } if (!grounded) { // fall body.velocity = body.velocity + (Vector3.down * gravity); // extend feet to avoid clipping into slopes when falling capsule.height = Mathf.Lerp(capsule.height, height, jumpFootExtensionLerp); capsule.center = Vector3.Lerp( capsule.center, new Vector3(0f, height / 2f, 0f), jumpFootExtensionLerp); } // jump conditions are checked if (input.jump && jumpReleased) { Jump(); } if (grounded || groundInfo.upwardAngle <= slopeMaxAngle) // no friction during sliding only when grounded or in air { if (Vector3.Dot(moveVelocityTarget, body.velocity) <= 0f) // if walking opposite direction or input 0 apply friction { body.velocity = Math.HorizontalOverride(body.velocity, body.velocity * moveDamping); } if (moveVelocityTarget.sqrMagnitude != 0f) // if move input move { body.velocity = Math.HorizontalOverride(body.velocity, Vector3.MoveTowards(body.velocity, moveVelocityTarget, moveAccel * Time.fixedDeltaTime)); } } }
void FixedUpdate() { // get input if (scrFpsController.getInput == null) { return; } FpsControllerInput input = scrFpsController.getInput(); // lerp toward shouldered or unshouldered position float intensityMultiplier = 1f; if (shouldering) { currentRotation = Quaternion.Lerp(currentRotation, Quaternion.Euler(0f, 0f, 0f), shoulderLerp); currentPosition = Vector3.Lerp(currentPosition, shoulderingPosition, shoulderLerp); if (scrFpsController.grounded) // only have steady weapon if shouldering and grounded { intensityMultiplier = shoulderMovementIntensityMult; } } else { currentRotation = Quaternion.Lerp(currentRotation, originalRotation, unshoulderLerp); currentPosition = Vector3.Lerp(currentPosition, originalPosition, unshoulderLerp); } // movement bob or vertical move inertia float movementBob = 0f; if (scrFpsController.grounded) { // vertical movement Bob movementBob = movementBobIntensity * intensityMultiplier * scrFpsController.GetMoveSpeedPercent() * Mathf.Sin(movementBobTime); movementBobTime += Time.fixedDeltaTime * Mathf.Lerp(movementBobMinFrequency, movementBobMaxFrequency, scrFpsController.GetMoveSpeedPercent()); } else { movementBobTime = 0f; // jump inertia moveInertiaSpring.AddForce(Vector3.down * -scrFpsController.body.velocity.y * moveInertiaMagnitude.y * intensityMultiplier * Time.fixedDeltaTime); } // look inertia lookInertiaSpring.AddSoftForce(new Vector3( input.lookY * -lookInertiaMagnitude.y * intensityMultiplier, // Look Vertical input.lookX * -lookInertiaMagnitude.x * intensityMultiplier, // Look Horizontal input.moveX * -moveBankMagnitude * intensityMultiplier), // Movement Bank lookInertiaDistributeFrames); Vector3 rotatedVelocity = transform.InverseTransformDirection(scrFpsController.body.velocity); // move inertia moveInertiaSpring.AddForce( new Vector3( -rotatedVelocity.x * moveInertiaMagnitude.x * Math.BoolToFloat(!(shouldering && scrFpsController.grounded)), movementBob, -rotatedVelocity.z * moveInertiaMagnitude.z * intensityMultiplier) * Time.fixedDeltaTime); // apply spring transformations transform.localRotation = currentRotation * Quaternion.Euler(lookInertiaSpring.UpdateState()) * Quaternion.Euler(recoilUpwardSpring.UpdateState()); transform.localPosition = currentPosition + moveInertiaSpring.UpdateState() + recoilBackwardSpring.UpdateState(); }