【问题标题】:Bullet reflection (ricochet) not working as intended子弹反射(弹跳)未按预期工作
【发布时间】:2019-04-03 13:27:39
【问题描述】:

我正在创建 2D 自上而下的射击游戏。我得到了我希望我的子弹从墙上反弹的点。我正在使用 OnCollisionEnter2D 和rigidbody2D。子弹在撞墙后会反弹,但它们会绕着自己的轴旋转。我尝试了很多方法,但无法让这个工作。

这就是它在实践中的样子:

这是我的子弹代码和弹跳孩子的代码。

public class GunBullet : MonoBehaviour
{
    [HideInInspector] public PlayerAvatar ownerPlayerAvatar;
    SpriteRenderer spriteRenderer;
    public int damage = 15;
    [SerializeField] float bulletSpeed;
    Rigidbody2D rigidbody;

    public void Start()
    {
        rigidbody = GetComponent<Rigidbody2D>();
        spriteRenderer = GetComponent<SpriteRenderer>();
        rigidbody.velocity = transform.right * BulletSpeed;
    }
}

public class RicochetMode : MonoBehaviour
{

    Rigidbody2D rigidbody2D;
    GunBullet gunBullet;

    private void Start()
    {
        rigidbody2D = transform.root.GetComponent<Rigidbody2D>();
        gunBullet = transform.root.GetComponent<GunBullet>();
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("Obstacle"))
        {
            Debug.Log("RICOCHET");
            Vector2 reflectedPosition = Vector3.Reflect(transform.right, collision.contacts[0].normal);
            rigidbody2D.velocity = (reflectedPosition).normalized * gunBullet.BulletSpeed;

            Vector2 dir = rigidbody2D.velocity;
            float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
            rigidbody2D.MoveRotation(angle);
        }
    }
}

我希望子弹朝向正确的方向。而且我不想使用统一的 2D 物理材料。

编辑。

问题的解决方法是设置
正如@trollingchar 所说,rigidbody2D.angularVelocity 为零。

【问题讨论】:

  • 他们建议仅将 MoveRotation 用于小角度 (docs.unity3d.com/ScriptReference/Rigidbody2D.MoveRotation.html),这不是子弹反射回来的情况。可以直接设置旋转。
  • 我正在尝试rigidbody.rotate = angle,但这似乎不起作用。
  • rotation,而不是rotate。如果这不起作用,请尝试设置transform.localRotationtransform.rotation
  • 是的,当然我的意思是轮换。这些似乎也不起作用,也许我应该提到子弹的刚体是动态的。
  • 也许他们正在旋转,因为统一的内部碰撞处理认为它必须改变 angularVelocity 作为碰撞的结果。里面有什么价值? (如果不为零,则在碰撞处理中手动将其设置为零)

标签: c# unity3d collision-detection game-physics


【解决方案1】:

MoveRotation 为旋转角度添加一个增量。看起来您希望将结果方向设置为angle,在这种情况下您将使用rigidbody.rotation。

GetComponent<Rigidbody>().rotation = Quaternion.Euler(0, 0, Mathf.Atan2(dir.y, dir.x)*Mathf.Rad2Deg);

或者,您可以尝试根据碰撞后 X 或 Y 速度是否变化来反转 X 或 Y 变换。

transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);

【讨论】:

    【解决方案2】:

    请检查下面的代码,将其应用于应该具有重力为 0 的刚体和 2d 圆形对撞机的子弹

     public float speed;
    
        private void Update()
        {
            transform.Translate(Vector2.up * Time.deltaTime * speed);
        }
    
        private void OnCollisionEnter2D(Collision2D collision)
        {
            if(collision.gameObject.tag == "wall")
            {
                ContactPoint2D point = collision.contacts[0];
                Vector2 newDir = Vector2.zero;
                Vector2 curDire = this.transform.TransformDirection(Vector2.up);
    
                newDir = Vector2.Reflect(curDire, point.normal);
                transform.rotation = Quaternion.FromToRotation(Vector2.up, newDir);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-12
      • 2011-03-11
      • 2023-03-05
      • 2021-07-27
      • 1970-01-01
      • 2019-01-26
      • 2015-03-23
      • 1970-01-01
      相关资源
      最近更新 更多