Unity霰弹枪制作

dru*_*ch 5 c# unity-game-engine raycasting

所以我对 C# 的随机数学有点陌生,我学会了如何制作步枪、手枪等。但我想学习如何使用光线投射而不是射弹制作霰弹枪。对于光线投射,我厌倦了做超过 1 次光线投射,但不知道如何使它们随机,所以现在我只能使用 1 次光线投射。我想在拍摄时进行随机传播。这是脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShotGun : MonoBehaviour {
    private int pellets = 9;

    private float spreadAngle = 10f;

    private float damage = 10f;

    private float range = 1000f;

    private Camera fpsCam;

    // Use this for initialization
    void Start () 
    {
    }

    // Update is called once per frame
    void Update () 
    {
        if (Input.GetButtonDown("Fire1"))
        {
            ShotgunRay();
        }

    }

    private void ShotgunRay()
    {
        RaycastHit hit;
        if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
        {
            Health_Armor target = hit.transform.GetComponent<Health_Armor>();

            if (target != null)
            {
                target.TakeDamage(damage);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

KYL*_*L3R 5

多次拍摄:

int amountOfProjectiles = 8;
if (Input.GetButtonDown("Fire1"))
    {
        for(int i = 0; i < amountOfProjectiles; i++)
        {
            ShotgunRay();
        }
    }
Run Code Online (Sandbox Code Playgroud)

对于随机性:

using Random = UnityEngine.Random;

Vector3 direction = fpsCam.transform.forward; // your initial aim.
Vector3 spread = Vector3.zero;
spread+= fpsCam.transform.up * Random.Range(-1f, 1f); // add random up or down (because random can get negative too)
spread+= fpsCam.transform.right * Random.Range(-1f, 1f); // add random left or right

// Using random up and right values will lead to a square spray pattern. If we normalize this vector, we'll get the spread direction, but as a circle.
// Since the radius is always 1 then (after normalization), we need another random call. 
direction += spread.normalized() * Random.Range(0f, 0.2f);
Run Code Online (Sandbox Code Playgroud)

RaycastHit 命中;if (Physics.Raycast(fpsCam.transform.position, Direction, out hit, range)) { // 等等...

要查看结果,请使用 Debug.DrawRay 和 DrawLine。我们想要包括错过的镜头。(我们画了1s,这样更容易看)

if (Physics.Raycast(fpsCam.transform.position, direction, out hit, range))
{
    Debug.DrawLine(fpsCam.transform.position, hit.point, Color.green, 1f);
}
else
{
    Debug.DrawLine(fpsCam.transform.position, fpsCam.transform.position + direction * range, Color.red, 1f);
}
Run Code Online (Sandbox Code Playgroud)

DrawLine 将(在命中的情况下)绘制到绿色的实际命中点。错过的投篮会以 和 的长度绘制range为红色。

结果: 在此输入图像描述

  • 尽管没有“Mathf.Random”方法,但答案很好,评论也很好。请改用“Random.Range(-1f, 1f)”。 (3认同)

Bra*_*ler 3

嗯,这取决于所讨论的霰弹枪的口径。我假设您有一把 12 号霰弹枪,通常装有大约 8 颗子弹。为此,您需要 8 个独立的光线投射和 8 个不同的“命中”对象。在大约 25 码处,标准 12 号炮弹的直径大约为 40 英寸,其有效射程(在减速过多而伤害任何物体之前的射程)约为 125 码。这不是绝对的,但它可以让您大致了解自己的目标。

因此,在伪代码中,我将创建 8 个光线投射,每个光线投射都有其各自的“命中”对象。所有光线投射的长度应为 114.3 单位(Unity 使用米作为测量单位,因此 125 码为 114.3 米),那么您要做的就是在桶的中心开始每个光线投射并创建一个“随机”旋转,将模拟每 25 码(或 22.86 单位)传播 40 英寸(1.016 单位)。Random()您可以通过组合来实现此目的,Quaternion.AngleAxis()直到获得良好的(现实的,但仍然非常随机)的传播。

另外,我还想指出一点。这些值基于从膛线枪管中发射子弹弹的情况。使用带有弹头的线膛枪管可以为霰弹枪提供最大的制动力。因此,您可以考虑这些最大值和最小武器处理量(因为子弹几乎可以炸掉您的手臂)。如果在你的游戏中,你想要有大量的霰弹枪和炮弹可供使用,并且你想要最大程度的真实感,那么你需要考虑当前发射的炮弹是铅弹、鸟弹还是子弹,并且您还需要考虑桶的类型。雄鹿/鸟弹在较远的距离上可能不那么有效,但它们的操控性得到了很大改善。