Unity霰弹枪制作

4

我对C#的随机数学有点陌生,但我学会了如何制作步枪、手枪等。但我想学习如何使用射线来制作霰弹枪,而不是使用弹丸。我尝试使用射线进行多次射击,但不知道如何使它们随机,所以现在我只能使用一个射线。我想在射击时制造随机散布。以下是脚本:

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);
            }
        }
    }
}
2个回答

4

多重拍摄:

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

对于随机性:

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);

RaycastHit命中; 如果(Physics.Raycast(fpsCam.transform.position,direction,out hit,range)) { //等等...

使用Debug.DrawRay和DrawLine查看结果。我们想要包括未命中的射击。(我们绘制1秒,这样更容易看到)

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);
}

DrawLine函数会在命中时以绿色绘制实际命中点。未命中的射击会以range长度和红色绘制。

结果: enter image description here


3
回答很好,并且评论得当,尽管没有Mathf.Random方法。请使用Random.Range(-1f, 1f)代替。 - Ian H.
谢谢@ian,我是凭记忆打的,应该查一下文档 :) - KYL3R
我在 direction = fpsCam.transform.forward; 这行代码处遇到了一个错误。 - drugsandwich

3
这将取决于所使用的霰弹枪规格。我假设您使用的是12号规格的霰弹枪,通常有大约8个小球。因此,您需要进行8次单独的光线投射,并获得8个不同的“命中”物体。在大约25码的距离上,标准的12号规格的子弹将会散开大约40英寸的直径,其有效射程(在减速之前可以对任何东西造成伤害的范围)为125码左右。这并非绝对值,但可以给您一个大致的想法。
因此,在伪代码中,我会创建8个光线投射,每个都有自己相应的“命中”物体。所有光线投射的长度应为114.3个单位(Unity使用米作为其测量单位,因此125码为114.3米),然后您需要从枪管中心开始每个光线投射,并创建一个“随机”旋转,以模拟每25码(或22.86个Unity单位)散开1.016个Unity单位的40英寸散布。您可以通过将Random()Quaternion.AngleAxis()结合使用,直到获得良好(逼真但仍然非常随机)的散布效果。
此外,我想指出。这些值是基于使用膛线枪管射击SLUG弹药的情况。使用带有膛线的枪管和SLUG弹药可以为霰弹枪提供最大的制止力。因此,您可以将这些视为最大值,并考虑到最小的武器操作量(因为SLUG弹药几乎可以炸掉您的手臂)。如果在您的游戏中,您想要提供各种可用的霰弹枪和弹药,并且您想要达到最大水平的逼真度,那么您需要考虑当前发射的弹药是否是火鸟弹、铅弹或SLUG弹,您还需要考虑枪管的类型。火鸟弹/铅弹在较远距离上可能不太有效,但其操作性能要好得多。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接