feedback addressed

This commit is contained in:
2026-01-30 21:22:59 +05:30
parent 85ef6fb856
commit aedb4f0e96
10 changed files with 166 additions and 48 deletions
+61 -1
View File
@@ -5,7 +5,10 @@ public class Ball : MonoBehaviour
{
[HideInInspector]public Rigidbody2D rb;
Vector3 startPosition;
public float wallDetectionLength = 0.5f;
public float wallStuckFixForce = 1f;
public float maxWallStuckVelocity = 5f;
public LayerMask wallLayerMask;
void Awake()
{
rb = GetComponent<Rigidbody2D>();
@@ -26,4 +29,61 @@ public class Ball : MonoBehaviour
yield return null;
}
}
public bool touchingB,touchingL,touchingR,touchingT = false;
void Update()
{
WallDetection();
}
void OnCollisionEnter2D(Collision2D collision)
{
float otherSpeed = collision.relativeVelocity.magnitude;
float velocityMult = 1- Mathf.Clamp01(rb.linearVelocity.magnitude / maxWallStuckVelocity);
Debug.Log($"{collision.collider.name} hit ball @ {otherSpeed}, ball speed: {rb.linearVelocity.magnitude}");
if (touchingR) {
rb.AddForce(Vector2.left * otherSpeed *wallStuckFixForce * velocityMult * Time.deltaTime);
}
if (touchingL) {
rb.AddForce(Vector2.right * otherSpeed * wallStuckFixForce * velocityMult * Time.deltaTime);
}
if (touchingT) {
rb.AddForce(Vector2.down * otherSpeed * wallStuckFixForce * velocityMult * Time.deltaTime);
}
if (touchingB) {
rb.AddForce(Vector2.up *otherSpeed * wallStuckFixForce * velocityMult * Time.deltaTime);
}
}
void WallDetection(){
// Linecast for each direction
Vector3 pos = transform.position;
RaycastHit2D hitL = Physics2D.Linecast(pos, pos + Vector3.left * wallDetectionLength, wallLayerMask);
touchingL = hitL.collider != null;
RaycastHit2D hitR = Physics2D.Linecast(pos, pos + Vector3.right * wallDetectionLength, wallLayerMask);
touchingR = hitR.collider != null;
RaycastHit2D hitT = Physics2D.Linecast(pos, pos + Vector3.up * wallDetectionLength, wallLayerMask);
touchingT = hitT.collider != null;
RaycastHit2D hitB = Physics2D.Linecast(pos, pos + Vector3.down * wallDetectionLength, wallLayerMask);
touchingB = hitB.collider != null;
}
void OnDrawGizmos()
{
Gizmos.color = touchingL ? Color.red : Color.green;
Gizmos.DrawLine(transform.position, transform.position + (Vector3.left * wallDetectionLength));
Gizmos.color = touchingR ? Color.red : Color.green;
Gizmos.DrawLine(transform.position, transform.position + (Vector3.right * wallDetectionLength));
Gizmos.color = touchingT ? Color.red : Color.green;
Gizmos.DrawLine(transform.position, transform.position + (Vector3.up * wallDetectionLength));
Gizmos.color = touchingB ? Color.red : Color.green;
Gizmos.DrawLine(transform.position, transform.position + (Vector3.down * wallDetectionLength));
}
}
+9 -4
View File
@@ -23,7 +23,9 @@ public class GameManager : MonoBehaviour
}
public EventTrigger gameInputPanel;
public Rigidbody2D ball;
public float puckForce = 1f;
public float puckForce = 20f;
[Range(0f,1f)]
public float puckForceMaxPoint = 0.75f;
public float ballMoveTime = 3f;
public float puckDragClamp = 5f;
@@ -114,13 +116,16 @@ public class GameManager : MonoBehaviour
Debug.Log("Pointer up");
PointerEventData pointerEventData = eventData as PointerEventData;
Vector2 endPosition = pointerEventData.position;
Vector2 direction = endPosition - startPosition;
// Vector2 direction = endPosition - startPosition;
Vector2 direction = selectedPuck.lineEnd;
if(coroutinePostLaunch != null){
StopCoroutine(coroutinePostLaunch);
}
coroutinePostLaunch = StartCoroutine(CoroutinePostLaunch());
selectedPuck.GetComponent<Rigidbody2D>().AddForce(-direction * puckForce * Time.deltaTime, ForceMode2D.Impulse);
float force = puckForce * Time.deltaTime;
Debug.Log($"launching puck at {direction}");
selectedPuck.GetComponent<Rigidbody2D>().AddForce(-direction * force, ForceMode2D.Impulse);
startPosition = Vector2.zero;
selectedPuck = null;
@@ -169,7 +174,7 @@ public class GameManager : MonoBehaviour
public void OnGoal(Team team){
freezeInput=true;
if(team == SelectedTeam){
if(team == Team.Blue){
blueScore++;
blueScoreText.text = blueScore.ToString();
}else{
+1 -1
View File
@@ -6,7 +6,7 @@ public class Goal : MonoBehaviour
void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.CompareTag("Player")){
Debug.Log("GOAL!");
Debug.Log($"GOAL! {team.ToString()}", gameObject);
Ball ball = collision.gameObject.GetComponent<Ball>();
Debug.Log(ball.name);
GameManager.instance.OnGoal(team);
+4 -4
View File
@@ -51,21 +51,21 @@ public class Puck : MonoBehaviour
GameManager.instance.DisposePuck(this);
}
public Vector3 lineEnd;
void Update()
{
if (isSelected && lineRenderer != null)
{
Vector3 worldEnd = Camera.main.ScreenToWorldPoint(new Vector3(GameManager.instance.curPosition.x, GameManager.instance.curPosition.y, Camera.main.nearClipPlane));
Vector3 localEnd = transform.InverseTransformPoint(worldEnd);
lineEnd = transform.InverseTransformPoint(worldEnd);
float clamp = GameManager.instance.puckDragClamp;
localEnd = Vector2.ClampMagnitude(localEnd, clamp);
lineEnd = Vector2.ClampMagnitude(lineEnd, clamp);
// Only two points for the line
lineRenderer.positionCount = 2;
lineRenderer.SetPosition(0, Vector3.zero);
lineRenderer.SetPosition(1, new Vector3(localEnd.x, localEnd.y, 0));
lineRenderer.SetPosition(1, new Vector3(lineEnd.x, lineEnd.y, 0));
}
selectedMarker.gameObject.SetActive(GetMarkerVisibility());