【问题标题】:I want to Know what Physics Pang Game use in Ball Bounce我想知道Physics Pang Game在Ball Bounce中使用了什么
【发布时间】:2019-04-26 01:50:27
【问题描述】:

我正在尝试像 pang 游戏一样进行控球。我写了这段代码,但遗漏了一些东西,或者我做错了什么。

我尝试使用物理材质,但我无法控制它。 当我将反弹设置为 1 Y 位置时,每帧都会增加。所以我对此无能为力。

private void OnTriggerEnter(Collider other)
{
    Debug.Log("Collied");

    if(other.tag == "rightWall")
    {
        direction = false;
    } 
    else if(other.tag == "leftWall")
    {
        direction = true;
    }

    if(other.tag == "groundWall") rigid.velocity = Vector3.up * 10;
    if(other.tag != "topWall")  BallMove(); 
}

void BallMove()
{
    if (direction == false)
    {
        rigid.AddForce(Vector3.Lerp(transform.position, new Vector3(-300f, 0, 0), Time.deltaTime * ballForce));
    }
    else
    {
        rigid.AddForce(Vector3.Lerp(transform.position, new Vector3(300f, 0,0), Time.deltaTime * ballForce));
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    如果您希望刚体以恒定速度移动,则不应使用力。 而是将球的刚体 drag 设置为 0 并为其指定起始速度。

    void Start(){
        rigi.velocity=new Vector3(1f,1f,0f); //set any starting velocity you want
    }
    

    然后你可以在球撞到墙上时翻转运动方向:

    OnTriggerEnter(Collider other) { 
    
       if(other.gameObject.tag == "rightWall" || other.gameObject.tag == "leftWall")
       {
           rigi.velocity=new Vector3(-rigi.velocity.x, rigi.velocity.y, rigi.velocity.z);
    
       } else if(other.gameObject.tag == "groundWall")
       {
           rigi.velocity = new Vector3(rigi.velocity.x, 10, rigi.velocity.z);
       }
    }
    

    在你的情况下,我还建议制作rigidBody kinematic 以避免运动错误

    【讨论】:

    • 嗯,它不是乒乓球游戏,它的 pang 是不同的。但是有一个问题是球松了它的高点,它的表现就像它有质量一样。每次反弹球都会失去高度。再告诉我一个 -rigi.velocity.x 是反转速度吗?我想设置固定的球高。
    • -rigi.velocity.x 正在恢复 x 速度。我编辑了我的答案,所以它的行为我认为你想要它。现在如果球击中“groundWall”,它的 y-Speed 设置为 10,或者你想要的任何值
    • 感谢它现在的工作。这是最简单的方法。
    【解决方案2】:

    您当然想使用冲动而不是力(力会在很长一段时间内施加,而冲动更像是停留 1 帧的电击) https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

    类似:

     rigid.AddForce(Vector3.Lerp(transform.position, new Vector3(300f, 0,0), Time.deltaTime * ballForce), ForceMode.Impulse);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      • 2019-11-27
      • 1970-01-01
      • 2021-09-06
      相关资源
      最近更新 更多