使用霰弹枪射击 2D Top-down Shooter
Shooting with a shotgun 2D Top-down Shooter
我正在尝试用霰弹枪射击,我已经用一颗子弹射击了。代码:
Vector2 shootingDirection = new Vector2(joystick.Horizontal, joystick.Vertical);
shootingDirection.Normalize();
if (shootingDirection != new Vector2(0, 0))
{
if(isShotGun) ShotGunShoot(shootingDirection);
GameObject bullet = Instantiate(bulletPrefab, crossHair.transform.position, Quaternion.identity);
bullet.transform.Rotate(0.0f, 0.0f, Mathf.Atan2(shootingDirection.y, shootingDirection.x) * Mathf.Rad2Deg);
bullet.GetComponent<Rigidbody2D>().AddForce(shootingDirection * 10f, ForceMode2D.Impulse);
}
}
但我只是想创建另外两个与主项目符号略有偏差的项目符号,因此它看起来像一个分数,但它无法正常工作。代码:
private void ShotGunShoot(Vector2 dir)
{
GameObject shotGun = Instantiate(bulletPrefab, crossHair.transform.position, Quaternion.identity);
shotGun.transform.Rotate(0.0f, 0.0f, Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 30f);
shotGun.GetComponent<Rigidbody2D>().AddForce((dir + new Vector2(-.3f, 0f)) * 10f, ForceMode2D.Impulse);
shotGun = Instantiate(bulletPrefab, crossHair.transform.position, Quaternion.identity);
shotGun.transform.Rotate(0.0f, 0.0f, Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg + 30f);
shotGun.GetComponent<Rigidbody2D>().AddForce((dir+ new Vector2(.3f, 0f)) * 10f, ForceMode2D.Impulse);
}
理应如此
现在是
感谢帮助!
为了轮换。你可以简单地设置方向(从你的图像我可以看到你的子弹必须飞向它的右向量)比如
shotgun.transform.right = dir;
shotgun.transform.Rotate(0, 0, 30);
对于加力:这使用世界 space 方向。
你总是在方向上通过并在世界中添加额外的偏移量space。这样无论你从哪个方向拍摄,额外的偏移量总是在 X 轴方向。
而是通过使用局部力来考虑方向Rigidbody.AddRelativeForce
shotGun.GetComponent<Rigidbody2D>().AddForceRelative((shotgun.transform.right + new Vector2(0f, 0.3f)).normalized * 10f, ForceMode2D.Impulse);
这现在使用项目符号的正确方向,并沿其 local Y 轴额外使用 0.3
的偏移量。
顺便说一句:如果您为预制件提供正确的类型
public Rigidbody bulletPrefab;
您可以跳过 GetComponent<Rigidbody>
个调用。
我正在尝试用霰弹枪射击,我已经用一颗子弹射击了。代码:
Vector2 shootingDirection = new Vector2(joystick.Horizontal, joystick.Vertical);
shootingDirection.Normalize();
if (shootingDirection != new Vector2(0, 0))
{
if(isShotGun) ShotGunShoot(shootingDirection);
GameObject bullet = Instantiate(bulletPrefab, crossHair.transform.position, Quaternion.identity);
bullet.transform.Rotate(0.0f, 0.0f, Mathf.Atan2(shootingDirection.y, shootingDirection.x) * Mathf.Rad2Deg);
bullet.GetComponent<Rigidbody2D>().AddForce(shootingDirection * 10f, ForceMode2D.Impulse);
}
}
但我只是想创建另外两个与主项目符号略有偏差的项目符号,因此它看起来像一个分数,但它无法正常工作。代码:
private void ShotGunShoot(Vector2 dir)
{
GameObject shotGun = Instantiate(bulletPrefab, crossHair.transform.position, Quaternion.identity);
shotGun.transform.Rotate(0.0f, 0.0f, Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 30f);
shotGun.GetComponent<Rigidbody2D>().AddForce((dir + new Vector2(-.3f, 0f)) * 10f, ForceMode2D.Impulse);
shotGun = Instantiate(bulletPrefab, crossHair.transform.position, Quaternion.identity);
shotGun.transform.Rotate(0.0f, 0.0f, Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg + 30f);
shotGun.GetComponent<Rigidbody2D>().AddForce((dir+ new Vector2(.3f, 0f)) * 10f, ForceMode2D.Impulse);
}
理应如此
现在是
感谢帮助!
为了轮换。你可以简单地设置方向(从你的图像我可以看到你的子弹必须飞向它的右向量)比如
shotgun.transform.right = dir;
shotgun.transform.Rotate(0, 0, 30);
对于加力:这使用世界 space 方向。
你总是在方向上通过并在世界中添加额外的偏移量space。这样无论你从哪个方向拍摄,额外的偏移量总是在 X 轴方向。
而是通过使用局部力来考虑方向Rigidbody.AddRelativeForce
shotGun.GetComponent<Rigidbody2D>().AddForceRelative((shotgun.transform.right + new Vector2(0f, 0.3f)).normalized * 10f, ForceMode2D.Impulse);
这现在使用项目符号的正确方向,并沿其 local Y 轴额外使用 0.3
的偏移量。
顺便说一句:如果您为预制件提供正确的类型
public Rigidbody bulletPrefab;
您可以跳过 GetComponent<Rigidbody>
个调用。