具体的步骤:
了解使用
水平方向的速度是:v1=v0×cosθ
竖直方向的速度是:v2=v0×sinθ-gt
y=v0×t-(gt^2)/2
克隆线段,坐标信息,线段的长度,线段的间距。
检测碰撞的点、控制线段的显示和隐藏、绘制弧
克隆子弹,计算子弹的发射速度,子弹的朝向
点击空格,初始化子弹数据,子弹发射
在plyer下创建一个空物体为Arc;在代码中用创建一个空物体再添加组件LineRenderer,同时把这些空物体存到list中。
segmentCount 线段的个数
LineRendererList 为LineRenderer类型
segmentwidth 设置的是0.01f
public void CreateArc()
{
for (int i = 0; i < segmentCount; i++)
{
GameObject cl = new GameObject("Arc_" + i);
cl.transform.SetParent(player.transform.Find("Arc"));
LineRendererList.Add(cl.AddComponent());
LineRendererList[i].startWidth = segmentwidth;
LineRendererList[i].endWidth = segmentwidth;
LineRendererList[i].material = LineMaterial;
LineRendererList[i].shadowCastingMode = ShadowCastingMode.Off;
LineRendererList[i].receiveShadows = false;
LineRendererList[i].lightProbeUsage = LightProbeUsage.Off;
LineRendererList[i].reflectionProbeUsage = ReflectionProbeUsage.Off;
LineRendererList[i].enabled = false;
}
}
获取在每个时间时,弧线的位置
IsUseGravity 是bool类型
使用公式:y=v0×t-(gt^2)/2
v0是我们手动定义的速度,
public Vector3 GetTimePosition(float time)
{
Vector3 usegravity = IsUseGravity ? Physics.gravity : Vector3.zero;
Vector3 result = player.transform.position+time arcSpeed + (0.5f time time) usegravity;
return result;
}
设置线段的坐标
public void SetPostion(int index, Vector3 StartPostion, Vector3 endPostion)
{
LineRendererList[index].enabled = true;
LineRendererList[index].SetPosition(0, StartPostion);
LineRendererList[index].SetPosition(1, endPostion);
}
每个线段的长度,和线段之间的间距
float timeStep = arcDuration / segmentCount;
[Range(0, 1)] public float segmentBreak;
当物体碰撞时返回碰撞的时间
HitLayer是地板的当前层级
Physics.Linecast在API有解释,在两点之间绘制射线,在这线段之间如有
碰撞返回为真
碰撞时间:如a到b绘制射线,a为第一个点,b为第二个点。
设置a点的时间是A,设置b点的时间是B。
timeStep 为A与B的时间差
AB 之间的距离为C,
碰撞点与A之间的距离 为D
碰撞的点一定是在线段之间,所以时间=(D/C)*timeStep +A
public float GetHitTime(out RaycastHit hitInfo)
{
float timeStep = arcDuration / segmentCount;
float startTime_1 = 0.0f;
Vector3 startPos_1 = GetTimePosition(startTime_1);
hitInfo = new RaycastHit();
for (int i = 0; i < segmentCount; i++)
{
float endtime_1 = timeStep + startTime_1;
Vector3 endpos_1 = GetTimePosition(endtime_1);
if (Physics.Linecast(startPos_1, endpos_1, out hitInfo, HitLayer))
{
Bullseye.transform.position = hitInfo.point+Vector3.up*0.1F;
Bullseye.SetActive(true);
float distance = Vector3.Distance(startPos_1, endpos_1);
float hitdistance = hitInfo.distance;
return startTime_1 + (hitdistance / distance) * timeStep;
}
startPos_1 = endpos_1;
startTime_1 = endtime_1;
}
return float.MaxValue;
}
绘制弧线
在unity中抛物线的速度是距离,我们需要初速度=(目标的方向*速度)
timeStep 设置一个线段的时间,segmentBreak线段之间的间距时间
private void DrawArc()
{
float timeStep = arcDuration / segmentCount;
arcSpeed = transform.forward * startVelocity;
currentTimeOffset = (Time.time - arcTimeOffset) *0.2f;
if (currentTimeOffset > (timeStep + segmentBreak))
{
currentTimeOffset = 0;
arcTimeOffset = Time.time;
}
float starttime = currentTimeOffset;
hitTime = GetHitTime(out hitInfo);
int i = 0;
for (i = 0; i < segmentCount; i++)
{
float endTime = starttime + timeStep;
Vector3 StartPos = GetTimePosition(starttime);
Vector3 Endpos = GetTimePosition(endTime);
SetPostion(i, StartPos, Endpos);
starttime += timeStep + segmentBreak;
if (starttime > hitTime)
{
break;
}
}
HideLine(i);
}
克隆子弹
bullet = GameObject.CreatePrimitive(PrimitiveType.Capsule);
子弹的发射速度
velocityX 水平的速度
velocityY 垂直速度
tanθ=对边/临边
public void BulletMove()
{
float time = Time.time - currentTime;
Vector3 playerXZ = transform.position;
playerXZ.y = 0;
velocityX = arcSpeed.magnitude;
velocityY =startVelocity Mathf.Sin(launchAngle Mathf.Deg2Rad) + Physics.gravity.y * time ;
Vector3 speed =transform.forward;
speed *= velocityX;
speed.y = velocityY;
bullet.transform.position += (speed) * Time.fixedDeltaTime;
Vector3 speedXZ = speed;
speedXZ.y = 0;
float tempTan = speedXZ.magnitude / speed.y;
float hu = Mathf.Atan(tempTan);
float angle = hu * Mathf.Rad2Deg;
bullet.transform.eulerAngles = new Vector3(angle, bullet.transform.eulerAngles.y, bullet.transform.eulerAngles.z);
if (Vector3.Distance(bullet.transform.position, Bullseye.transform.position) < 0.1f)
{
IsMove = !IsMove;
}
}
目的:
当玩家移动,绘制的弧线也可以移动,子弹防止朝向不对
public void InitBullet()
{
bullet.transform.localScale = Vector3.one * 0.3f;
bullet.transform.position = player.transform.position;
bullet.transform.rotation = player.transform.rotation;
}
点击空格时:
要算出当前时间
currentTime = Time.time;
初始化子弹数据
考虑当旋转角度>180时
if (launchAngle > 180)
{
launchAngle = 360 - launchAngle;
}
IsMove = !IsMove
ControlMove();
DrawArc();
放在FixedUpdate()
更多unity2018的功能介绍请到paws3d爪爪学院查找。