【发布时间】:2019-09-16 23:17:21
【问题描述】:
我有一把枪,它会产生一个弹丸,弹射对撞机(弹跳弹)。它应该朝枪口的方向射击,但我得到的是弹丸总是向右上方 45 度射击,我知道这是因为我的常量声明向量 2。
我尝试使用 Vector2.up,但它会阻止弹丸产生弹跳效果,因为它总是想向上移动。
我应该如何实现这些东西?我只想让弹丸射向我的枪所面对的方向并在对撞机上反弹。顺便说一句,这是一个 2D 游戏。我在下面附上了我的代码,所以你可以看到。谢谢!
射弹脚本:
private Rigidbody2D rb;
public static bool canMove = true;
void Start()
{
rb = GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(10f, 10f);
}
void Update()
{
//transform.Translate(Vector2.up * speed * Time.deltaTime);
if (canMove)
{
rb.isKinematic = false;
}
else if (!canMove)
{
rb.isKinematic = true;
}
}
枪法:
float offset = -90f;
public GameObject projectile;
public Transform shotPoint;
public GameObject child;
void Start()
{
}
void Update()
{
Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float rotZ = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);
if (Input.GetMouseButtonDown(0))
{
Instantiate(projectile, shotPoint.position, transform.rotation);
Projectile.canMove = true;
}
}
【问题讨论】:
标签: c# unity3d game-physics