void FixedUpdate() { if (Time.time > nextKeyRandomizationTime) { randomizeKeys(); DrunkMalus currentDrunkMalus = drunkMaluses[GameManager.DrunkLevel]; nextKeyRandomizationTime = Time.time + GaussianDistribution.Generate(currentDrunkMalus.keyRandomizationMeanTime, currentDrunkMalus.keyRandomizationVarianceTime); } move(); respectBounds(); }
void Start() { deactivatePlayerCollisions(); uiManager = FindObjectOfType <UIManager>(); transform.position = GameManager.PlayerSpawnPosition; drunkMaluses = JSONManager.Load <Dictionary <GameManager.Drunkness, DrunkMalus> >("DrunkMaluses"); DrunkMalus currentDrunkMalus = drunkMaluses[GameManager.DrunkLevel]; nextKeyRandomizationTime = Time.time + GaussianDistribution.Generate(currentDrunkMalus.keyRandomizationMeanTime, currentDrunkMalus.keyRandomizationVarianceTime); }
private void move() { Vector3 direction = Vector3.zero; if (Input.GetKey(upKey)) { direction.z++; } if (Input.GetKey(downKey)) { direction.z--; } if (Input.GetKey(leftKey)) { direction.x--; } if (Input.GetKey(rightKey)) { direction.x++; } if (Time.time > nextMaxSpeedRandomizationTime) { DrunkMalus currentDrunkMalus = drunkMaluses[GameManager.DrunkLevel]; maxSpeed = GaussianDistribution.Generate(currentDrunkMalus.speedMean, currentDrunkMalus.speedVariance); nextMaxSpeedRandomizationTime = Time.time + GaussianDistribution.Generate(4, 1); } if (direction == Vector3.zero) { velocity -= acceleration * velocity.normalized * Time.deltaTime / 2; if (driftAngle < driftSpeed) { driftAngle = 0; } else { driftAngle = driftAngle - Mathf.Sign(driftAngle) * driftSpeed; } } else { if (velocity.sqrMagnitude > maxSpeed * maxSpeed) { velocity -= acceleration * velocity.normalized * Time.deltaTime / 2; } else { velocity += acceleration * direction.normalized * Time.deltaTime; } transform.rotation = Quaternion.LookRotation(velocity, Vector3.up); // Calculate drifting float angle = Vector3.SignedAngle(velocity, direction, Vector3.up); if (Mathf.Abs(angle) < maxDriftAngle) { angle = 0; // Because it will never be exactly zero } angle = Mathf.Clamp(Mathf.Abs(angle), 0, driftSpeed) * Mathf.Sign(angle); if (Mathf.Abs(angle) < Mathf.Epsilon && Mathf.Abs(driftAngle) > Mathf.Epsilon) { angle = -Mathf.Clamp(Mathf.Abs(driftAngle), 0, driftSpeed) * Mathf.Sign(driftAngle); } driftAngle += angle; } velocity = Vector3.ClampMagnitude(velocity, 15); driftAngle = Mathf.Clamp(Mathf.Abs(driftAngle), 0, maxDriftAngle) * Mathf.Sign(driftAngle); // Drift transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, driftAngle); // Translate transform.Translate(velocity * Time.fixedDeltaTime, Space.World); }