日志2025.1.21
1.添加了子弹功能
private void OnCollisionEnter(Collision collision)
{
//不让子弹到处乱窜
rb.constraints = RigidbodyConstraints.FreezeAll;
}
2.添加了精准射击和非精准射击
private void UpdateAimPosition()
{
//精确瞄准
aim.position = GetMouseHitInfo().point;
//非精确瞄准
if (!isAimingPrecisly)
aim.position = new Vector3(aim.position.x, transform.position.y + 1, aim.position.z);
}
private Vector3 BulletDirection()
{
//消除因为移动而对子弹设计产生的误差
weaponHolder.LookAt(aim);
gunPoint.LookAt(aim);
Vector3 direction = (aim.position - gunPoint.position).normalized;
if(player.aim.isAimingPrecisly)
direction.y = 0;
return direction;
}
private void Shoot()
{
GameObject newBullet = Instantiate(bulletPrefab, gunPoint.position, Quaternion.LookRotation(gunPoint.forward));
//这里方向用 BulletDirection() 而不用 gunPoint.forward 是为了消除 Aim 位置对y轴的影响(非精准射击)
newBullet.GetComponent<Rigidbody>().velocity = bulletSpeed * BulletDirection();
Destroy(newBullet, 10);
animator.SetTrigger("fire");
}