首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >统一游戏BreakOut球失去速度和方向

统一游戏BreakOut球失去速度和方向
EN

Stack Overflow用户
提问于 2022-08-04 04:27:58
回答 1查看 41关注 0票数 -1

我刚刚完成了一个BreakOut风格的游戏,但有一个错误,有时球被粘在地图的边缘,没有方向或速度,如屏幕截图所示。

我所看到的是,当球完全失去轨迹或速度,但不能解决错误时,就会发生这种情况。

在这里输入图像描述

我的代码

代码语言:javascript
复制
public class Ball : MonoBehaviour
{
    [SerializeField] Rigidbody2D rigidbody2D;
    Vector2 moveDirection;
    Vector2 currentVelocity;
     float velocity=10;
    //GameManager gameManager;
    Transform paddle;
    [SerializeField] AudioController audioController;
    [SerializeField] AudioClip bounceSfx;
    [SerializeField] AudioClip dieSfx;
    public bool superBall;
    [SerializeField] float superBallTime=10;
    [SerializeField]float yMinSpeed = 10;
    [SerializeField]TrailRenderer trailRenderer;

    public bool SuperBall
    {
        get=> superBall;
        set{
            superBall=value;
            if(superBall)
                StartCoroutine(ResetSuperBall());
        }
    }
    // Start is called before the first frame update
    void Start()
    {

        //rigidbody2D = GetComponent<Rigidbody2D>();
        //rigidbody2D.velocity = Vector2.up*velocity*Time.deltaTime;
        GameManager.Instance = FindObjectOfType<GameManager>();
        paddle = transform.parent;
    }
    // Update is called once per frame
    void Update()
    {
        currentVelocity = rigidbody2D.velocity;
        if (Mathf.Abs(currentVelocity.y) < 3 && Mathf.Abs(currentVelocity.y) < 3 && GameManager.Instance.ballOnGame)
        {
            velocity = 10;
            rigidbody2D.velocity = Vector2.up * velocity ;
        }
        if (Mathf.Abs(currentVelocity.y) + Mathf.Abs(currentVelocity.y) < 10 && GameManager.Instance.ballOnGame)
        {
            velocity = 10;
            rigidbody2D.velocity = Vector2.up * velocity ;
        }
        if  (velocity <10 && GameManager.Instance.ballOnGame)
        {
            velocity = 10;
            rigidbody2D.velocity = Vector2.up * velocity ;
        }

        if ((Input.GetKey(KeyCode.W) && GameManager.Instance.ballOnGame == false)||(Input.GetKey(KeyCode.Space) && GameManager.Instance.ballOnGame == false))
        {
            rigidbody2D.velocity = Vector2.up * velocity ;
            transform.parent = null;
            GameManager.Instance.ballOnGame = true;
            rigidbody2D.isKinematic = false;
            rigidbody2D.AddForce(new Vector3(velocity, velocity, 0));
            if (!GameManager.Instance.GameStarted)
            {
                GameManager.Instance.GameStarted = true;

            }
        }
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.transform.CompareTag("Brick") && superBall)
        {
            rigidbody2D.velocity = currentVelocity;
            return;
        }
        moveDirection=Vector2.Reflect(currentVelocity,collision.GetContact(0).normal);
        if (Mathf.Abs(moveDirection.y) < yMinSpeed)
        {
            //permitir velocidad minima
            moveDirection.y = yMinSpeed*Mathf.Sign(moveDirection.y);
        }
        rigidbody2D.velocity=moveDirection;

        audioController.PlaySfx(bounceSfx);

        if (collision.transform.CompareTag("BottomLimit"))
        {
            if(GameManager.Instance != null)
            {
                GameManager.Instance.PlayerLives--;
                audioController.PlayDie(dieSfx);
                if (GameManager.Instance.PlayerLives > 0)
                {
                    rigidbody2D.velocity = Vector2.zero;
                    transform.SetParent(paddle);
                    transform.localPosition = new Vector2(0, 0.65f);
                    GameManager.Instance.ballOnGame = false;
                }
                
            }
        }
    }
    IEnumerator ResetSuperBall()
    {
        trailRenderer.enabled = true;
        yield return new WaitForSeconds(superBallTime);
        trailRenderer.enabled = false;
        GameManager.Instance.powerIsActive = false;
        superBall = false;

    }

}
EN

回答 1

Stack Overflow用户

发布于 2022-08-04 14:19:50

这是低速刚体常见的问题。物理引擎实现了一个Physics2D.velocityThreshold,它旨在抑制缓慢的反弹,并使一堆刚体平静下来。

velocityThreshold的默认值是1,这意味着速度较慢的x或y分量将被累加到0。

要解决此问题,只需将值降低到0.001。您可以在位于Edit->ProjectSettings->Physics2d的Physcs2d选项卡中执行此操作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73230389

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档