unity 霰弹枪制作
Unity shotgun making
所以我对随机数学的 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);
}
}
}
}
多次拍摄:
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命中;
if (Physics.Raycast(fpsCam.transform.position, 方向, 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);
}
DrawLine 绘制(在命中的情况下)到绿色的实际命中点。未命中的镜头以 range
的长度和红色绘制。
结果:
好吧,这将取决于所讨论的霰弹枪的口径。我假设你有一把 12 号霰弹枪,通常有大约 8 颗子弹。为此,您需要 8 个独立的光线投射和 8 个不同的 "hit" 对象。在大约 25 码处,一个标准的 12 号 shell 将传播大约 40 英寸的直径,其有效射程(在它减速太多而不会伤害任何东西之前的射程)大约是 125 码。这不是绝对的,但它可以让您大致了解您的目标。
因此,在伪代码中,我将创建 8 个光线投射,每个光线投射都有其各自的 "hit" 对象。所有光线投射的长度都应为 114.3 个单位(unity 使用米作为其测量单位,因此 125 码为 114.3 米),然后您要做的是在桶的中心开始每个光线投射并创建一个 "random"模拟每 25 码(或 22.86 个单位)传播 40 英寸(1.016 个单位)的旋转。您可以通过将 Random()
与 Quaternion.AngleAxis()
结合使用来实现此目的,直到获得良好的(现实的,但仍然非常随机的)传播。
另外,我想指出一点。这些值基于从膛线枪管中射出 SLUG shell。使用带有弹头的膛线枪管可以为霰弹枪提供最大的制动力。因此,您可以考虑这些 MAX 值,以及 MIN 武器处理量(因为子弹几乎可以将您的手臂炸掉)。如果在你的游戏中,你想要大量可用的霰弹枪和 shell,并且你想要最大程度的真实感,你需要考虑当前正在射击的 shell铅弹、鸟弹或子弹,您还需要考虑枪管的类型。 Buck/birdshot 在更远的范围内可能没有那么有效,但它们的处理能力得到了很大改善。
所以我对随机数学的 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);
}
}
}
}
多次拍摄:
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命中; if (Physics.Raycast(fpsCam.transform.position, 方向, 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);
}
DrawLine 绘制(在命中的情况下)到绿色的实际命中点。未命中的镜头以 range
的长度和红色绘制。
结果:
好吧,这将取决于所讨论的霰弹枪的口径。我假设你有一把 12 号霰弹枪,通常有大约 8 颗子弹。为此,您需要 8 个独立的光线投射和 8 个不同的 "hit" 对象。在大约 25 码处,一个标准的 12 号 shell 将传播大约 40 英寸的直径,其有效射程(在它减速太多而不会伤害任何东西之前的射程)大约是 125 码。这不是绝对的,但它可以让您大致了解您的目标。
因此,在伪代码中,我将创建 8 个光线投射,每个光线投射都有其各自的 "hit" 对象。所有光线投射的长度都应为 114.3 个单位(unity 使用米作为其测量单位,因此 125 码为 114.3 米),然后您要做的是在桶的中心开始每个光线投射并创建一个 "random"模拟每 25 码(或 22.86 个单位)传播 40 英寸(1.016 个单位)的旋转。您可以通过将 Random()
与 Quaternion.AngleAxis()
结合使用来实现此目的,直到获得良好的(现实的,但仍然非常随机的)传播。
另外,我想指出一点。这些值基于从膛线枪管中射出 SLUG shell。使用带有弹头的膛线枪管可以为霰弹枪提供最大的制动力。因此,您可以考虑这些 MAX 值,以及 MIN 武器处理量(因为子弹几乎可以将您的手臂炸掉)。如果在你的游戏中,你想要大量可用的霰弹枪和 shell,并且你想要最大程度的真实感,你需要考虑当前正在射击的 shell铅弹、鸟弹或子弹,您还需要考虑枪管的类型。 Buck/birdshot 在更远的范围内可能没有那么有效,但它们的处理能力得到了很大改善。