![](/img/trans.png)
[英]How to apply velocity to a parent object that ignores child object's mass in Unity?
[英]Unity: How to apply rotation on object's velocity?
我在Unity上制作了自上而下的射击游戏,我需要实现gun弹枪,它会一次释放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;
}
您需要将旋转应用于每个项目符号的方向向量。 向量旋转的公式为... 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
可能可以循环使用此清洁器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.