From 644e9f3418c1f1411eb2d1af16bc8e3c7489f136 Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Sun, 9 Apr 2023 09:19:32 +0200 Subject: [PATCH] Smooth AI --- Assets/Scripts/Game/AIPlayer.cs | 13 +++++++------ Assets/Scripts/Game/GameManager.cs | 6 +++--- Assets/Scripts/Game/Player.cs | 28 ++++++++++++++-------------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/Assets/Scripts/Game/AIPlayer.cs b/Assets/Scripts/Game/AIPlayer.cs index 8c68ff9..030ad9d 100644 --- a/Assets/Scripts/Game/AIPlayer.cs +++ b/Assets/Scripts/Game/AIPlayer.cs @@ -14,8 +14,8 @@ namespace Game { private bool BallApproaches(Ball ball) { var ballVy = ball.Rb.velocity.y; return Side switch { - ESide.Bottom => ballVy < 0, - ESide.Top => ballVy > 0, + Side.Bottom => ballVy < 0, + Side.Top => ballVy > 0, _ => throw new Exception("Side not set on player!") }; } @@ -91,7 +91,7 @@ namespace Game { if (area.Contains(end)) return end.x; - float playerY = Side == ESide.Bottom ? area.yMin : area.yMax; + float playerY = Side == Side.Bottom ? area.yMin : area.yMax; Vector2 playerLeft = new Vector2(area.xMin, playerY); Vector2 playerRight = new Vector2(area.xMax, playerY); @@ -161,7 +161,7 @@ namespace Game { private float currentSmoothV; private void ApproachPosition(float pos) { - float result = Mathf.SmoothDamp(X(), pos, ref currentSmoothV, .5f); + float result = Mathf.SmoothDamp(X(), pos, ref currentSmoothV, .5f, Speed); transform.position = new Vector2(result, Y()); ClampInsideBorders(); } @@ -169,14 +169,15 @@ namespace Game { private bool isApproaching; private float lastDirection; private void FixedUpdate() { + float dt = Time.fixedDeltaTime; float target = GetTargetPosition(); - float h = Width / 2; + float h = Mathf.Max(Speed * dt, Width / 2); goingLeft = target < X() - h; goingRight = target > X() + h; if (goingLeft || goingRight) { isApproaching = false; lastDirection = goingLeft ? -1 : 1; - TryLinearMove(Time.fixedDeltaTime); + TryLinearMove(dt); } else { if (!isApproaching) { diff --git a/Assets/Scripts/Game/GameManager.cs b/Assets/Scripts/Game/GameManager.cs index 2661816..b4bd921 100644 --- a/Assets/Scripts/Game/GameManager.cs +++ b/Assets/Scripts/Game/GameManager.cs @@ -53,7 +53,7 @@ namespace Game { } private void Start() { - Settings.Type = Type.Hybrid; + Settings.Type = Type.AI; Settings.AIDifficulty = Difficulty.VeryHard; var ball = Instantiate(ballPrefab).GetComponent(); @@ -85,8 +85,8 @@ namespace Game { default: throw new ArgumentOutOfRangeException(); } - p1.Side = Player.ESide.Bottom; - p2.Side = Player.ESide.Top; + p1.Side = Side.Bottom; + p2.Side = Side.Top; Tests(); } diff --git a/Assets/Scripts/Game/Player.cs b/Assets/Scripts/Game/Player.cs index cacc3c3..16f5576 100644 --- a/Assets/Scripts/Game/Player.cs +++ b/Assets/Scripts/Game/Player.cs @@ -4,11 +4,12 @@ using UnityEngine; using UnityEngine.InputSystem; namespace Game { + + public enum Side {Top, Bottom} + public class Player : NetworkBehaviour { - - public enum ESide {Top, Bottom} - public ESide Side { get; set; } + public Side Side { get; set; } private int score; @@ -18,15 +19,15 @@ namespace Game { protected float Speed => 10; // Unit distance from zero - private float Border => 7; + private float Border => 10; private SpeedModification speedModification; private BorderModification borderModification; protected float Width => transform.localScale.x; - private float LeftSide => X() - Width / 2; - private float RightSide => X() + Width / 2; + private float LeftEdge => X() - Width / 2; + private float RightEdge => X() + Width / 2; protected float X() { return transform.position.x; @@ -37,10 +38,10 @@ namespace Game { } protected void ClampInsideBorders() { - if (LeftSide < -Border) - transform.Translate(Vector2.right * (-Border - LeftSide), Space.World); - if (RightSide > Border) - transform.Translate(Vector2.left * (RightSide - Border), Space.World); + if (LeftEdge < -Border) + transform.Translate(Vector2.right * (-Border - LeftEdge), Space.World); + if (RightEdge > Border) + transform.Translate(Vector2.left * (RightEdge - Border), Space.World); } protected void TryLinearMove(float h) { @@ -52,14 +53,13 @@ namespace Game { private void Start() { float y = Side switch { - ESide.Bottom => BorderSize.Singleton.y1, - ESide.Top => BorderSize.Singleton.y2, + Side.Bottom => BorderSize.Singleton.y1, + Side.Top => BorderSize.Singleton.y2, _ => throw new ArgumentOutOfRangeException() }; transform.position = new Vector2(0, y); - if (Side == ESide.Top) { + if (Side == Side.Top) transform.Rotate(transform.forward, 180); - } } }