gpt4 book ai didi

c# - unity 霰弹枪制作

转载 作者:行者123 更新时间:2023-11-30 14:22:54 25 4
gpt4 key购买 nike

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

DrawLine 绘制(在命中的情况下)到绿色的实际命中点。未命中的镜头以 range 的长度和红色绘制。

结果: enter image description here

关于c# - unity 霰弹枪制作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47889977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com