private static float Dampen(float current, float target, float dampening, float elapsed, float minStep = 0.0f) { var factor = D2dHelper.DampenFactor(dampening, elapsed); var maxDelta = Mathf.Abs(target - current) * factor + minStep * elapsed; return(Mathf.MoveTowards(current, target, maxDelta)); }
protected virtual void FixedUpdate() { var currentPoint = (Vector2)transform.position; var vector = targetPoint - currentPoint; if (vector.magnitude <= MinimumDistance) { ChangeTargetPoint(); vector = targetPoint - currentPoint; } // Limit target speed if (vector.magnitude > MaximumSpeed) { vector = vector.normalized * MaximumSpeed; } // Acceleration if (body == null) { body = GetComponent <Rigidbody2D>(); } var factor = D2dHelper.DampenFactor(Acceleration, Time.deltaTime); body.velocity = Vector2.Lerp(body.velocity, vector * SpeedBoost, factor); }
protected virtual void Update() { var localPosition = transform.localPosition; var targetX = (Input.mousePosition.x - Screen.width / 2) * MoveScale; var targetY = (Input.mousePosition.y - Screen.height / 2) * MoveScale; var factor = D2dHelper.DampenFactor(MoveSpeed, Time.deltaTime); localPosition.x = Mathf.Lerp(localPosition.x, targetX, factor); localPosition.y = Mathf.Lerp(localPosition.y, targetY, factor); transform.localPosition = localPosition; // Left click? if (Input.GetMouseButtonDown(0) == true) { var mainCamera = Camera.main; if (MuzzlePrefab != null) { Instantiate(MuzzlePrefab, transform.position, Quaternion.identity); } if (BulletPrefab != null && mainCamera != null) { var position = mainCamera.ScreenToWorldPoint(Input.mousePosition); Instantiate(BulletPrefab, position, Quaternion.identity); } } }
protected virtual void Update() { var factor = D2dHelper.DampenFactor(Dampening, Time.deltaTime); currentThrottle = Mathf.Lerp(currentThrottle, Throttle, factor); transform.localScale = MaxScale * Random.Range(1.0f - Flicker, 1.0f + Flicker) * currentThrottle; }
protected virtual void Update() { // Update flipping if the game is playing if (Application.isPlaying == true) { cooldown -= Time.deltaTime; // Flip? if (cooldown <= 0.0f) { FrontShowing = !FrontShowing; ResetCooldown(); } } // Get target angle based on flip state var targetAngle = FrontShowing == true ? 0.0f : 180.0f; // Slowly rotate to the target angle if the game is playing if (Application.isPlaying == true) { var factor = D2dHelper.DampenFactor(FlipSpeed, Time.deltaTime); angle = Mathf.Lerp(angle, targetAngle, factor); } // Instantly rotate if it's not else { angle = targetAngle; } transform.localRotation = Quaternion.Euler(0.0f, angle, 0.0f); // Make the destructible indestructible if it's past 90 degrees if (Destructible != null) { Destructible.Indestructible = targetAngle >= 90.0f; } // Update movement if (Application.isPlaying == true) { MoveProgress += MoveSpeed * Time.deltaTime; } var moveDistance = (EndPosition - StartPosition).magnitude; if (moveDistance > 0.0f) { var progress01 = Mathf.PingPong(MoveProgress / moveDistance, 1.0f); transform.localPosition = Vector3.Lerp(StartPosition, EndPosition, Mathf.SmoothStep(0.0f, 1.0f, progress01)); } }
protected virtual void LateUpdate() { if (Application.isPlaying == true) { var factor = D2dHelper.DampenFactor(FlashDampening, Time.deltaTime, 0.1f); Flash = Mathf.Lerp(Flash, 0.0f, factor); } cachedCanvasGroup.alpha = Flash > 0.005f ? Flash : 0.0f; }
protected virtual void Update() { var targetAngle = Input.GetAxisRaw("Horizontal") * SteerAngleMax; var factor = D2dHelper.DampenFactor(SteerAngleDampening, Time.deltaTime); currentAngle = Mathf.Lerp(currentAngle, targetAngle, factor); for (var i = 0; i < SteerWheels.Length; i++) { SteerWheels[i].transform.localRotation = Quaternion.Euler(0.0f, 0.0f, -currentAngle); } }
protected virtual void LateUpdate() { var factor = D2dHelper.DampenFactor(ShakeDampening, Time.deltaTime, 0.1f); Shake = Mathf.Lerp(Shake, 0.0f, factor); var shakeStrength = Shake * ShakeScale; var shakeTime = Time.time * ShakeSpeed; var localPosition = transform.localPosition; localPosition.x = Mathf.PerlinNoise(offsetX, shakeTime) * shakeStrength; localPosition.y = Mathf.PerlinNoise(offsetY, shakeTime) * shakeStrength; transform.localPosition = localPosition; }
protected virtual void Update() { // Update input inputManager.Update(KeyCode.None); // Make sure the camera exists var camera = D2dHelper.GetCamera(null); if (camera != null) { // Loop through all non-gui fingers foreach (var finger in inputManager.Fingers) { if (finger.StartedOverGui == false) { var localPosition = transform.localPosition; var targetX = (finger.PositionA.x - Screen.width / 2) * MoveScale; var targetY = (finger.PositionA.y - Screen.height / 2) * MoveScale; var factor = D2dHelper.DampenFactor(MoveSpeed, Time.deltaTime); localPosition.x = Mathf.Lerp(localPosition.x, targetX, factor); localPosition.y = Mathf.Lerp(localPosition.y, targetY, factor); transform.localPosition = localPosition; // Fire? if (finger.Up == true) { if (MuzzlePrefab != null) { Instantiate(MuzzlePrefab, transform.position, Quaternion.identity); } if (BulletPrefab != null) { var position = camera.ScreenToWorldPoint(finger.PositionA); Instantiate(BulletPrefab, position, Quaternion.identity); } } // Skip other fingers break; } } } }
protected virtual void FixedUpdate() { if (Target != null) { // Get the main camera var mainCamera = Camera.main; // Begin dragging if (Input.GetKey(Requires) == true) { var position = D2dHelper.ScreenToWorldPosition(Input.mousePosition, Intercept, mainCamera); var factor = D2dHelper.DampenFactor(Dampening, Time.fixedDeltaTime); Target.velocity += (Vector2)(position - Target.transform.position) * factor; } } }
protected virtual void FixedUpdate() { if (inputManager.Fingers.Count > 0) { // Make sure the camera exists var camera = D2dHelper.GetCamera(null); if (camera != null) { // Make sure the target exists if (Target != null) { // Grab world position and transition there var center = inputManager.GetAveragePosition(true); var position = D2dHelper.ScreenToWorldPosition(center, Intercept, camera); var factor = D2dHelper.DampenFactor(Dampening, Time.fixedDeltaTime); Target.velocity += (Vector2)(position - Target.transform.position) * factor; } } } }
protected virtual void Update() { // Update input inputManager.Update(KeyCode.None); // Calculate control values from fingers/mouse var delta = inputManager.GetAveragePullScaled(true); var targetSteer = Mathf.Clamp(delta.x / 100.0f, -1.0f, 1.0f) * SteerAngleMax; var targetDrive = Mathf.Clamp(delta.y / 100.0f, -1.0f, 1.0f) * DriveTorque; // Smooth to target values var steerFactor = D2dHelper.DampenFactor(SteerAngleDampening, Time.deltaTime); var driveFactor = D2dHelper.DampenFactor(DriveDampening, Time.deltaTime); currentSteer = Mathf.Lerp(currentSteer, targetSteer, steerFactor); currentDrive = Mathf.Lerp(currentDrive, targetDrive, driveFactor); // Apply steering for (var i = 0; i < SteerWheels.Length; i++) { SteerWheels[i].transform.localRotation = Quaternion.Euler(0.0f, 0.0f, -currentSteer); } }