子弹的平抛上抛路径可以使用两个方程来计算:
(1).竖直方向的运动方程:y = v0y * t - 0.5 * g * t^2
其中,y为子弹在竖直方向上的位移(即目标位置的y与子弹位置的y的绝对值),v0y为子弹的初速度(竖直方向),g为重力加速度(一般取9.8 m/s^2),t为时间(已知y,g可求t)。
(2).水平方向的运动方程:x = v0 * t
其中,x为子弹在水平方向上的位移(即目标位置的x与子弹生成位置的x的绝对值),v0为子弹的初速度(水平方向,根据(1)中的t值和x的位移距离,可求x方向的初速度),t为时间。
为了计算子弹的平抛上抛路径,需要根据给定的初速度、发射角度或竖直初速度来求解相应的初速度分量v0x和v0y。然后,可以通过以上两个方程来计算子弹在不同时间点的位置(水平位移x和竖直位移y)。
//Debug.Log("创建子弹");
GameObject bullet=GameObject.Instantiate(bulletPrefab);
bullet.transform.position = bulletSpawnPos.position;
//子弹向上的速度V0
float g = Mathf.Abs(Physics2D.gravity.y)*bullet.transform.GetComponent<Rigidbody2D>().gravityScale;
float v0 = 8;
float t0 = v0 / g;
float y0 = 0.5f * g * t0 * t0;
float v=0;
//计算子弹平抛(X轴)初速度 高度=0.5*加速度*时间的平方 --->已知y,a可求时间t --->已知x距离,t时间,根据距离=速度v*时间t可求速度v
float x = beAttackTarget.position.x - transform.position.x + Random.Range(-1f, 1f);
if (transform.position.y + y0 > beAttackTarget.position.y)
{//子弹往下抛
float y = transform.position.y - beAttackTarget.position.y + y0;
float t = Mathf.Sqrt((y * 2) / g) + t0;
v = x / t;
}else if(transform.position.y + y0 < beAttackTarget.position.y)
{//子弹往上抛
float y = beAttackTarget.position.y-transform.position.y;
float t = Mathf.Sqrt((y * 2) / g);
v0 = g*t;
Debug.Log("vo:" + v0);
v = x / t;
}
if (enemyStatus == EnemyStatus.Attack)
{
bullet.GetComponent<AcidBubbles>().SetSpeed(new Vector2(v, v0));
}