0
\$\begingroup\$

I'm trying to make a game where the player has to destroy blocks. But when the player collides with the corners of the blocks, the player's velocity slows down, or even stops.

How can I ignore the corners and keep a constant momentum?

using System.Collections;
using UnityEngine;
using TMPro;

public class Player : MonoBehaviour
{
    private Rigidbody2D rb;
    public float horizontalSpeed = 5f;
    public float verticalSpeed = 7f;
    public float sideBounceForce = 10f;


    float horizontalInput;

    public bool isLaunched = false;
    public bool isGameStart = false;
    public bool isDead = false;

    public AudioSource sfxCollide;
    public AudioSource sfxDied;

    public TextMeshProUGUI scoreText;
    public TextMeshProUGUI highScoreText;
    private int score = 0;
    private int highScore = 0;

    private SpriteRenderer playerRenderer;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        playerRenderer = GetComponent<SpriteRenderer>();

        highScore = PlayerPrefs.GetInt("HighScore", 0);
        UpdateHighScoreText();
    }

    private void FixedUpdate()
    {
        BallsMovement();
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        sfxCollide.Play();
        [![enter image description here][1]][1]
    }

    public void DestroyChildren(GameObject children)
    {
        Destroy(children);
    }

    void BallsMovement()
    {
        if (!isLaunched && Input.GetKeyDown(KeyCode.Space))
        {
            isGameStart = true;

            LaunchBall();

            return;
        }

        horizontalInput = Input.GetAxis("Horizontal");

        // Calculate the horizontal movement
        Vector2 horizontalMovement = new Vector2(horizontalInput * horizontalSpeed, rb.velocity.y);

        // Apply the horizontal movement to the ball's rigidbody
        rb.velocity = horizontalMovement;
    }

    private void LaunchBall()
    {
        rb.AddForce(Vector2.up * verticalSpeed, ForceMode2D.Impulse);
        isLaunched = true;
    }

    public bool IsGameStarted()
    {
        return isGameStart;
    }

    public Sprite GetPlayerSprite()
    {
        return playerRenderer.sprite;
    }

    public void IncreaseScore()
    {
        score++;
        UpdateScoreText();

        if (score > highScore)
        {
            highScore = score;
            UpdateHighScoreText();
            PlayerPrefs.SetInt("HighScore", highScore);
        }
    }

    private void UpdateScoreText()
    {
        scoreText.text = "Score: " + score.ToString();
    }

    private void UpdateHighScoreText()
    {
        highScoreText.text = "High Score: " + highScore.ToString();
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ If you read your question and the code isn't properly formatted, you're not done. Click the edit button and fix it, and visit the help center if you need support on correct formatting. I've made the needed edits on your behalf this time, so you can use this version as a guide for future posts. \$\endgroup\$
    – DMGregory
    Commented Jul 20, 2023 at 4:51

1 Answer 1

2
\$\begingroup\$

I think instead of using collision, you should tick the isTrigger checkbox on the box's collider, and use OnTriggerEnter2D to destroy the box.

You want to destroy the box, but in your way you have to hit the box first, which make player slower.

With trigger colliders, the collision does not deflect or slow the player, but you can still detect the collision to destroy the box.

\$\endgroup\$
1
  • \$\begingroup\$ This is a good solution. If you want the ball to reflect off the box while maintaining velocity, you can scan ahead for nearby collisions using a Physics2D.CircleCast, compute the point of intersection and reflect the velocity in FixedUpdate rather than using the physics engine's standard collision resolution that includes damping, friction, torque, etc. If you do that, the triggers never overlap so you'd destroy the box in the reflection routine rather than in OnTriggerEnter2D. \$\endgroup\$
    – DMGregory
    Commented Jul 20, 2023 at 11:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .