【问题标题】:Ball does not bounce off the screen球不会从屏幕上反弹
【发布时间】:2019-10-24 23:07:14
【问题描述】:

首先,对不起我的英语。 我希望我能正确解释 在我的比赛中,球应该在边缘的任何地方反弹。从视频中,我有一个代码,球在场上应该保留,但是,球现在跳起来并沿着屏幕滑动

我试过if语句,但我是新手,不知道我是否正确

public class Ball_Controller : MonoBehaviour

{ [SerializeField] 私有浮点 moveSpeed = 10f;

[SerializeField] private Vector2 startDirection;

[SerializeField] private Vector2 startOtherDirection;

private Rigidbody2D rb;

public static bool GameIsover;
public GameObject gameOverUI;


public Transform topLeft;
public Transform bottomRight;





private void Start()
{



    GameIsover = false;

}
void Update()
{
    BallMove();


    transform.position = new Vector3(Mathf.Clamp(transform.position.x, topLeft.position.x, bottomRight.position.x), Mathf.Clamp(transform.position.y, bottomRight.position.y, topLeft.position.y), transform.position.z);
    if (transform.position.y == Screen.height || transform.position.x == Screen.width)
    {

        BallMoveOther();
    }





    if (GameIsover)
        return;

    if (Input.GetKeyDown("e"))
    {
        EndGame();


    }



}



void EndGame()
{

    Debug.Log("Game Over");
    GameIsover = true;
    gameOverUI.SetActive(true);
    Time.timeScale = 0;


}



private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.tag.Equals ("Player"))
    {

        Destroy(collision.gameObject);
        Destroy(gameObject);
        EndGame();
    }




}

private void BallMove()
{
    rb = GetComponent<Rigidbody2D>();

    rb.velocity = moveSpeed * startDirection;
}
private void BallMoveOther()
{
    rb = GetComponent<Rigidbody2D>();
    rb.velocity = moveSpeed * startOtherDirection;
}

}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    问题在于下面的代码,

    transform.position = new Vector3(Mathf.Clamp(transform.position.x, topLeft.position.x, bottomRight.position.x), Mathf.Clamp(transform.position.y, bottomRight.position.y, 
    topLeft.position.y), transform.position.z);
    
    if (transform.position.y == Screen.height || transform.position.x == Screen.width){
        BallMoveOther();
    }
    
    1. 避免使用单独的对象来查找屏幕边界
    2. 在 if 语句中,transform.position.y 是全局位置,而 screen.height 不是。

    试试这样的:

    // Converting Screen positions to world space
    Vector3 screenStart = Camera.main.ScreentoWorldPoint(new Vector3(0,0,0));
    Vector3 screenEnd = Camera.main.ScreentoWorldPoint(new Vector3(screen.width,screen.height,0));
    
    // Clampping the sprite within the screen
    transform.position = new Vector3(
        Mathf.Clamp(transform.position.x,screenStart.position.x,screenEnd.position.x),
        Mathf.Clamp(transform.position.y,screenStart.position.y,screenEnd.position.y),
        transform.position.z);
    // Getting the length and width of the sprite
    Vector2 SpriteBnds = transform.GetComponent<SpriteRenderer>().bounds.size;
    
    // checking if the sprite went beyond the screen bounds in any four directions
    if(
        transform.position.y+(SpriteBnds.y/2) >=screenEnd.position.y ||
        transform.position.y - (SpriteBnds.y/2) <= screenstart.position.y ||
        transform.position.x+(SpriteBnds.x/2) >=screenEnd.position.x ||
        transform.position.x - (SpriteBnds.x/2) <= screenstart.position.x)
        {
            BallMoveOther();
        }
    

    【讨论】:

    • 您好,首先感谢您的快速回复。当我接手剧本时,我的球挂在左下角并停留在那里
    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多