protected virtual void UpdateMovement() { var movement = new CharacterMovement(); var direction = Input.GetAxis("Horizontal") * Vector3.right + Input.GetAxis("Vertical") * Vector3.forward; var lookAngle = Util.AngleOfVector(_controller.LookTargetInput - _motor.transform.position); if (direction.magnitude > float.Epsilon) { movement.Direction = Quaternion.Euler(0, lookAngle, 0) * direction.normalized; } if (_controller.ZoomInput) { movement.Magnitude = 0.5f; } else { if (_motor.Gun != null) { if (Input.GetButton("Run") && !_motor.IsCrouching) { movement.Magnitude = 2.0f; } else { movement.Magnitude = 1.0f; } } else { if (Input.GetButton("Run")) { movement.Magnitude = 1.0f; } else { movement.Magnitude = 0.5f; } } } _controller.MovementInput = movement; }
protected virtual void UpdateMovement() { var local = Input.GetAxis("Horizontal") * Vector3.right + Input.GetAxis("Vertical") * Vector3.forward; var movement = new CharacterMovement(); movement.Direction = getMovementDirection(local); if (WalkWhenZooming && _controller.ZoomInput) { movement.Magnitude = 0.5f; movement.IsSlowedDown = true; } else { if ((_motor.ActiveWeapon.Gun != null || _motor.ActiveWeapon.HasMelee) && FastMovement) { if (Input.GetButton("Run") && !_motor.IsCrouching) { movement.Magnitude = 2.0f; } else { movement.Magnitude = 1.0f; } } else { if (Input.GetButton("Run")) { movement.Magnitude = 1.0f; } else { movement.Magnitude = 0.5f; } } } _controller.MovementInput = movement; }
private void Update() { updateGrenadeAimAndPreview(); if (_motor.PotentialCover != null) { _motor.InputTakeCover(); } if (AlwaysAim) { if (_motor.IsInCover) { _motor.InputAimWhenLeavingCover(); } else { _motor.InputAim(); } } if (AutoReload && _motor.Gun != null && _motor.Gun.Clip <= 0) { _motor.InputReload(); } var hasMovement = HasMovement && Movement.sqrMagnitude > float.Epsilon; var hasAiming = HasAiming && Aiming.sqrMagnitude > float.Epsilon; if (hasMovement) { var movement = new CharacterMovement(); var right = Vector3.Cross(Vector3.up, Forward); movement.Direction = Movement.x * right + Movement.y * Forward; switch (Speed) { case CharacterSpeed.Walk: movement.Magnitude = 0.4f; break; case CharacterSpeed.Run: movement.Magnitude = 1.0f; break; case CharacterSpeed.Sprint: movement.Magnitude = 2.0f; break; } if (_motor.IsInCover) { if (_coverTimer < 0.5f && (movement.Direction.magnitude < 0.1f || Vector3.Dot(movement.Direction, _motor.Cover.Forward) > -0.1f)) { movement.Direction = (movement.Direction + _motor.Cover.Forward).normalized; } } _motor.InputMovement(movement); if (!hasAiming && !_willFireOrThrow && !_hasJustFiredOrThrown) { var target = _motor.transform.position + movement.Direction * 100; _motor.SetBodyLookTarget(target); _motor.SetLookTarget(target); _motor.SetFireTarget(target); } } else if (_motor.IsInCover && _coverTimer < 0.5f) { var movement = new CharacterMovement(); movement.Magnitude = 1.0f; movement.Direction = _motor.Cover.Forward; _motor.InputMovement(movement); } if (_motor.HasGrenadeInHand) { if (hasAiming) { var right = Vector3.Cross(Vector3.up, Forward); var direction = Aiming.x * right + Aiming.y * Forward; var target = transform.position + direction * 100 + Vector3.up; _motor.SetBodyLookTarget(target); _motor.SetLookTarget(target); _motor.SetFireTarget(target); _wasAiming = true; } else if (_wasAiming) { _willFireOrThrow = IsAllowedToFire; _wasAiming = false; } } else { if (hasAiming) { findTarget(); var forward = _motor.LookTarget - _motor.transform.position; forward.y = 0; forward.Normalize(); if (Vector3.Dot(forward, _motor.transform.forward) > 0.25f || _motor.IsInCover) { if (IsAllowedToFire && !(Speed == CharacterSpeed.Sprint && HasMovement)) { _motor.InputAim(); _wasAiming = true; } } if (_motor.Gun != null && _motor.Gun.FireOnMobileAim && IsAllowedToFire) { _willFireOrThrow = true; } } else if (_wasAiming) { if (IsAllowedToFire) { findTarget(); _willFireOrThrow = true; } _wasAiming = false; } } if (_hasJustFiredOrThrown) { _postAimWait += Time.deltaTime; if (_postAimWait >= PostFireDelay) { _hasJustFiredOrThrown = false; _postAimWait = 0; } } if (_willFireOrThrow) { if (_motor.HasGrenadeInHand) { if (_hasGrenadePath) { _motor.InputThrowGrenade(_grenadePath, _grenadePathLength, _motor.Grenade.Step); } _willFireOrThrow = false; _hasJustFiredOrThrown = true; _postAimWait = 0; } else { var delta = Mathf.Abs(Mathf.DeltaAngle(transform.eulerAngles.y, _fireAngle)); if (delta < FirePrecision) { _motor.InputFire(); _willFireOrThrow = false; _hasJustFiredOrThrown = true; _postAimWait = 0; } } } if (_motor.IsInCover) { if (_motor.IsInCover) { if (!_wasInCover) { _wasInCover = true; _coverTimer = 0; } else { _coverTimer += Time.deltaTime; } } else { _wasInCover = false; } } else if (_motor.PotentialCover != null) { _motor.InputTakeCover(); } if (IsCrouching) { _motor.InputCrouch(); } }
private void updateMovement() { CharacterMovement movement = new CharacterMovement(); var isForward = Input.GetKey(KeyCode.W); var isBackward = Input.GetKey(KeyCode.S); var isRight = Input.GetKey(KeyCode.D); var isLeft = Input.GetKey(KeyCode.A); Vector3 direction = Vector3.zero; if (isForward) { direction.z += 1; } if (isBackward) { direction.z -= 1; } if (isRight) { direction.x += 1; } if (isLeft) { direction.x -= 1; } if (isForward && isBackward) { direction.z = _lastDirection.z; } if (isRight && isLeft) { direction.x = _lastDirection.x; } _lastDirection = direction; var lookAngle = Util.AngleOfVector(LookTargetInput - _motor.transform.position); if (direction.magnitude > float.Epsilon) { movement.Direction = Quaternion.Euler(0, lookAngle, 0) * direction.normalized; } if (_isZooming) { movement.Magnitude = 0.5f; } else { if (_motor.Gun != null) { if (Input.GetKey(KeyCode.LeftShift)) { movement.Magnitude = 2.0f; } else { movement.Magnitude = 1.0f; } } else { if (Input.GetKey(KeyCode.LeftShift)) { movement.Magnitude = 1.0f; } else { movement.Magnitude = 0.5f; } } } if (_motor.IsInCover || _motor.IsAimingBackFromCover) { if (!_wasInCover) { _wasInCover = true; _coverTimer = 0; } else { _coverTimer += Time.deltaTime; } if (_coverTimer < 0.5f && (movement.Direction.magnitude < 0.1f || Vector3.Dot(movement.Direction, _motor.Cover.Forward) > -0.1f)) { movement.Direction = (movement.Direction + _motor.Cover.Forward).normalized; } } else { _wasInCover = false; } _isSprinting = false; if (movement.IsMoving) { _isActivelyFacing = true; // Smooth sprinting turns if (movement.Magnitude > 1.1f && !_motor.IsInCover) { // Don't allow sprinting backwards if (Mathf.Abs(Mathf.DeltaAngle(lookAngle, Util.AngleOfVector(movement.Direction))) < 100) { var wantedAngle = Util.AngleOfVector(movement.Direction); var bodyAngle = _motor.transform.eulerAngles.y; var delta = Mathf.DeltaAngle(bodyAngle, wantedAngle); const float MaxSprintTurn = 60; if (delta > MaxSprintTurn) { movement.Direction = Quaternion.AngleAxis(bodyAngle + MaxSprintTurn, Vector3.up) * Vector3.forward; } else if (delta < -MaxSprintTurn) { movement.Direction = Quaternion.AngleAxis(bodyAngle - MaxSprintTurn, Vector3.up) * Vector3.forward; } _motor.SetBodyLookTarget(_motor.transform.position + movement.Direction * 100); _motor.InputPossibleImmediateTurn(false); _postSprintNoAutoAim = 0.3f; _isSprinting = true; } else { movement.Magnitude = 1.0f; } } } _motor.InputMovement(movement); }