private void UpdateVelocityX(MovementBoundaries moveBoundaries) { float newVelocityX = this.velocity.x; if (moveBoundaries.NearBarrierRight && moveBoundaries.NearBarrierLeft) { newVelocityX = 0; } else if (moveBoundaries.NearBarrierRight || moveBoundaries.NearBarrierLeft) { float hitDistance = moveBoundaries.NearBarrierRight ? moveBoundaries.BarrierRightDistance : moveBoundaries.BarrierLeftDistance; float directionMultiplier = moveBoundaries.NearBarrierRight ? 1 : -1; if (hitDistance > this.barrierMinDistance) { newVelocityX -= directionMultiplier * this.barrierSlowdownXMultiplier * this.driftSpeedMultiplier; } else { newVelocityX = -directionMultiplier * this.driftSpeedMultiplier; } } else if (moveBoundaries.IsPastCameraLeft || moveBoundaries.IsPastCameraRight) { newVelocityX = (moveBoundaries.IsPastCameraLeft ? 1 : -1) * this.driftSpeedMultiplier; } else if (Mathf.Abs(this.velocity.x) < 0.01) // if almost stopped moving // Start moving again: { newVelocityX = (this.facingRight ? 1 : -1) * this.driftSpeedMultiplier; } this.velocity = new Vector2(newVelocityX, this.velocity.y); }
void Update() { if (this.facingRight && this.spriteRenderer.flipX) { this.spriteRenderer.flipX = false; } else if (!this.facingRight && !this.spriteRenderer.flipX) { this.spriteRenderer.flipX = true; } MovementBoundaries moveBoundaries = this.CalculateMovementBoundaries(); this.UpdateVelocityX(moveBoundaries); this.UpdateVelocityY(moveBoundaries); if (this.velocity.x > 0) { this.facingRight = true; } else if (this.velocity.x < 0) { this.facingRight = false; } // If velocity.x == 0, don't change direction. this.movementController.Move(this.velocity * Time.deltaTime); }
private void UpdateVelocityY(MovementBoundaries moveBoundaries) { float newVelocityY = this.velocity.y; if (moveBoundaries.NearBarrierUp && moveBoundaries.NearBarrierDown) { newVelocityY = 0; } else { float velocityYOffset = 0; float waveShiftYVelocity = 0; if (moveBoundaries.NearBarrierUp || moveBoundaries.NearBarrierDown) { this.waveShiftUp = moveBoundaries.NearBarrierDown; float hitDistance = moveBoundaries.NearBarrierUp ? moveBoundaries.BarrierUpDistance : moveBoundaries.BarrierDownDistance; float directionMultiplier = moveBoundaries.NearBarrierUp ? -1 : 1; if (hitDistance > this.barrierMinDistance) { velocityYOffset = directionMultiplier * this.barrierSlowdownYMultiplier * this.driftSpeedMultiplier; } else { this.driftWavePhase = -2 * Mathf.PI * this.driftWaveFrequency * Time.time + (moveBoundaries.NearBarrierUp ? 1.05f : 0.05f) * Mathf.PI; } } else if (moveBoundaries.IsPastCameraTop || moveBoundaries.IsPastCameraBottom) { this.waveShiftUp = moveBoundaries.IsPastCameraBottom; this.driftWavePhase = -2 * Mathf.PI * this.driftWaveFrequency * Time.time + (moveBoundaries.IsPastCameraTop ? 1.05f : 0.05f) * Mathf.PI; } else { waveShiftYVelocity = (this.waveShiftUp ? 1 : -1) * this.driftWaveYShiftMultiplier * this.driftSpeedMultiplier; } // Mod argument to Mathf.Sin by 2*pi, since Mathf.Sin fails for large values: float newVelocityYSin = Mathf.Sin((2 * Mathf.PI * this.driftWaveFrequency * Time.time + this.driftWavePhase) % (2 * Mathf.PI)); newVelocityY = this.driftWaveAmplitude * newVelocityYSin + velocityYOffset + waveShiftYVelocity; } this.velocity = new Vector2(this.velocity.x, newVelocityY); }