feedback addressed
This commit is contained in:
+61
-1
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user