初学者项目-Pong-“ Ball Script”有点越野车:(

问题描述

我今天开始编写第一款计算机游戏,因此我选择了一个简单的游戏:Pong。

核心游戏已经可以使用,但我目前正尝试每次 player1 得分时添加一些功能,例如提高速度。 (敌人目前也是一个可控字符,我打算为此创建一个简单的KI)。它在一开始就运作良好,但是在游戏中的某个时刻,开始出现错误,这意味着:没有从 players1反弹。 / strong>的字符框,并试图保持与敌人发生冲突后弹跳的方向。速度太高了吗?我是否需要更改 players1 框的内容

视频显示如下:https://youtu.be/qzHGoQtRmEM

该脚本当前看起来像这样:

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using UnityEngine;
using UnityEngine.UI;

public class Ball : MonoBehavIoUr
{
    //variables
    public float speed;
    public float restartspeed;
    int scorePlayer1;
    int scoreEnemy;
    public Text txtPlayer1;
    public Text txtEnemy;
    public float pausetime;
    bool quit = false;

    
    void Start()
    {
        GetComponent<Rigidbody2D>().veLocity = new Vector2(1,0) * speed * Time.deltaTime;
    }


    void Update()
    {
        
    }
   //Collision Physics
    private void OnCollisionEnter2D(Collision2D col)
    {
        //print(col.gameObject.name);

        if (col.gameObject.name == "Player1")
        {
            //Collision Player1
            float y = hitObject(transform.position,col.transform.position,col.collider.bounds.size.y);
            //Calculate Direction
            Vector2 dir = new Vector2(1,y);
            //Directionvector Physics
            GetComponent<Rigidbody2D>().veLocity = dir * speed * Time.deltaTime;
        }
        if (col.gameObject.name == "Enemy")
        {
            //collision Enemy
            float y = hitObject(transform.position,col.collider.bounds.size.y);
            //Calculate Direction
            Vector2 dir = new Vector2(-1,y);
            //Directionvector Physics
            GetComponent<Rigidbody2D>().veLocity = dir * speed * Time.deltaTime;
        }
        if (col.gameObject.name == "WallLeft")
        {
            scorePlayer1 = scorePlayer1 + 1;
            txtPlayer1.text = scorePlayer1.ToString();
            //Restart with a pause x sec
            StartCoroutine(waiter());
        }
        if (col.gameObject.name == "WallRight")
        {
            scoreEnemy = scoreEnemy + 1;
            txtEnemy.text = scoreEnemy.ToString();
            //Restart with a pause of x sec
            StartCoroutine(waiter());
        }
    }

    float hitObject(Vector2 ballPos,Vector2 playerPos,float playerhight)
    {
        return (ballPos.y - playerPos.y) / playerhight;
    }

    //pausefunction + reset to origin
    IEnumerator waiter()
    {
        float counter = 0;
        Vector2 temp = new Vector2(0,0);
        gameObject.transform.position = temp;
        Vector2 freeze = new Vector2(0,0);
        GetComponent<Rigidbody2D>().veLocity = freeze;

        float waitTime = pausetime;

        while (counter < waitTime)
        {
            counter += Time.deltaTime;
            Debug.Log("Waited for: " + counter + "seconds");
            if (quit)
            {
                yield break;
            }
            yield return null;
        }
        //push ball again to players direction with higher veLocity
        GetComponent<Rigidbody2D>().veLocity = new Vector2(1,0) * (restartspeed + scorePlayer1);
        
    }

}

对所有人的爱很多^^(对不起,如果剧本太长,但我不知道哪一行与问题相关)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)