团结霰弹枪制作

问题描述 投票:3回答:2

所以我对随机数学的C#有点新,我学会了如何制作步枪,手枪等。但我想学习如何使用光线投射制作霰弹枪,而不是使用射弹。有了raycast,我厌倦了做更多的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);
            }
        }
    }
}
c# unity3d raycasting
2个回答
3
投票

那么这将取决于所讨论的霰弹枪的规格。我假设你有一把12号霰弹枪,通常有8个弹丸。为此,您需要8个单独的光线投射和8个不同的“命中”对象。在大约25码处,一个标准的12号炮弹的直径大约为40英寸,它的有效射程(在它减速太大而不会伤到任何东西之前的范围)大约是125码。这不是绝对的,但它可以让您大致了解您的目标。

所以,在伪代码中,我会创建8个光线投射,每个光线投射都有自己各自的“命中”对象。所有的光线投影应该有114.3个单位(统一使用米作为其测量单位,所以125码是114.3米),那么你想要做的是开始每个光线投射在枪管的中心并创造一个“随机”旋转,将模拟每25码(或22.86单位)的40英寸(1.016单位)传播。你可以通过将Random()Quaternion.AngleAxis()结合起来直到你得到一个好的(现实的,但仍然非常随机的)传播来实现这一点。

另外,我想指出一点。这些值是基于从一个膛线枪管中射出一个SLUG外壳。使用带有弹簧的膛线枪管可以用霰弹枪提供最大的制动力。所以,你可以考虑这些MAX值,使用MIN武器处理量(因为一个slu can几乎可以让你的手臂脱落)。如果在你的游戏中,你想要大量的霰弹枪和炮弹可供使用,并且你想要最大程度的真实感,那么你需要考虑当前射击的炮弹是弹射,鸟类还是slu and,以及你还需要考虑桶的类型。降落/鸟类在更远的范围内可能不那么有效,但它们的处理得到了很大改善。


3
投票

多次拍摄:

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

对于随机性:

Vector3 direction = fpsCam.transform.forward; // your initial aim.
Vector3 spread;
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);


if (Physics.Raycast(fpsCam.transform.position, direction, out hit, range))
    {
     // etc...

要查看结果,请使用Debug.DrawRay和DrawLine。我们希望包括错过的投篮。

if (Physics.Raycast(fpsCam.transform.position, direction, out hit, range))
    {
        Debug.DrawLine(fpsCam.transform.position, hit.point, green);
    }
     else
    {
        Debug.DrawRay(fpsCam.transform.position, direction, range, red);
     }

DrawLine以绿色绘制(在命中的情况下)实际生命值。错过的镜头被绘制在range的长度和红色。

© www.soinside.com 2019 - 2024. All rights reserved.