我正在Unity上制作自上而下的射击游戏,我需要实现shot弹枪,它会一次释放5发子弹,每下一个将比以前少旋转10度(总共20到-20)。实例化射击点上的子弹,通过子弹脚本将速度应用到子弹上,该脚本附加在子弹预制件上。我也将旋转应用于实例化方法中,但是子弹只是围绕自身旋转,而不是使其飞舞。 射击代码:
Vector2 mouseScreenPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction = (mouseScreenPosition - (Vector2) transform.position).normalized;
transform.up = direction;
GameObject newBullet1 = Instantiate(bullet, firePoint.position, Quaternion.Euler(0f, 0f, 20f)) as GameObject;
newBullet1.GetComponent<Bullet>().direction = direction;
GameObject newBullet2 = Instantiate(bullet, firePoint.position, Quaternion.Euler(0f, 0f, 10f)) as GameObject;
newBullet2.GetComponent<Bullet>().direction = direction;
子弹代码:
public Vector3 direction;
public float bulletSpeed;
void Update () {
GetComponent<Rigidbody2D>().velocity = new Vector3 (direction.x, direction.y, transform.position.z) * bulletSpeed;
}
答案 0 :(得分:0)
您需要将旋转应用于每个子弹的方向向量。向量旋转的公式是... x1 = x0 * cos(angle)-y0 * sin(angle); y1 = x0 * sin(angle)+ y0 * cos(angle);
Vector2 direction10 = new Vector2(direction.x*Mathf.cos(10*Mathf.Deg2Rad) - direction.y*Mathf.sin(10*Mathf.Deg2Rad), direction.x*Mathf.sin(10*Mathf.Deg2Rad) + direction.y*Mathf.cos(10*Mathf.Deg2Rad));
Vector2 direction20 = new Vector2(direction.x*Mathf.cos(20*Mathf.Deg2Rad) - direction.y*Mathf.sin(20*Mathf.Deg2Rad), direction.x*Mathf.sin(20*Mathf.Deg2Rad) + direction.y*Mathf.cos(20*Mathf.Deg2Rad));
newBullet2.GetComponent<Bullet>().direction = direction10;
newBullet3.GetComponent<Bullet>().direction = direction20;
... //etc
可能可以用循环清洁这种清洁剂。