团结霰弹枪制作

时间:2017-12-19 15:15:24

标签: c# unity3d raycasting

所以我对随机数学的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);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

多次拍摄:

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...

对于随机性:

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

要查看结果,请使用Debug.DrawRay和DrawLine。我们想要包括错过的镜头。

range

DrawLine以绿色绘制(如果命中)实际生命值。错过的镜头以<script type="text/babel">和红色的长度绘制。

答案 1 :(得分:3)

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

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

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

相关问题