弹丸霰弹枪统一:不一致的传播
Projectile shotgun in unity: inconsistent spread
我正在尝试使用基于射弹的霰弹枪制作射击游戏。然而,在某些角度,传播会发生变化,我猜测是由于变换的变化。向前。无论如何要修复这些看似随机的偏移量? `void ElementWeapons(int selection,int alternate)
{
float[] sgxoffsets = new float[8] {-0.025f,.025f,.025f,-.025f,.0175f,-.0175f,-.0175f,.0175f};
float[] sgyoffsets = new float[8] {-0.025f,-.025f,.025f,.025f,-.0175f,-.0175f, .0175f, .0175f };
float[] sgzoffsets = new float[8] { -0.025f, .025f, .025f, -.025f, .0175f, -.0175f, -.0175f, .0175f };
WeaponSwap WepSwap = transform.GetComponent();
switch (selection)
{
case 0: //Iron Shotugn Ferrous Fieldpiece
switch (alternate) {
case 0:
fireRate = 70;
if (firing == true)
{
ttf = Time.time + (1 / (fireRate / 60));
for (int i = 0; i < 8; i++)
{
toffset = new Vector3(sgxoffsets[i], sgyoffsets[i], sgzoffsets[i]);
var Vial = Instantiate(vial, transform.position, Cam.transform.rotation);
Vial.velocity = (90*(transform.forward +toffset));
Vial.GetComponent<VialBehavior>().vialElement = EleManager.instance.currentElement;
Vial.GetComponent<VialBehavior>().dmg = 2f;
}`
我认为您是在本地坐标中定义传播,但随后您将其与世界坐标中的 transform.forward 一起使用。
考虑在 toffset 上使用 transform.TransformPoint,然后再将其添加到 transform.forward
https://docs.unity3d.com/ScriptReference/Transform.TransformPoint.html
在使用 Chuck 的 link 进行试验后,我查看了不同的选项并发现 transform.TransformVector
,它创建了一个恒定的点差,请参见此处:
for (int i = 0; i < 8; i++)
{
toffset = new Vector3(sgxoffsets[i], sgyoffsets[i], sgzoffsets[i]);
var Vial = Instantiate(vial, transform.position, Cam.transform.rotation);
Vial.velocity = (90*(transform.forward + transform.TransformVector(toffset)));
Vial.GetComponent<VialBehavior>().vialElement = EleManager.instance.currentElement;
Vial.GetComponent<VialBehavior>().dmg = 2f;
}
我正在尝试使用基于射弹的霰弹枪制作射击游戏。然而,在某些角度,传播会发生变化,我猜测是由于变换的变化。向前。无论如何要修复这些看似随机的偏移量? `void ElementWeapons(int selection,int alternate) { float[] sgxoffsets = new float[8] {-0.025f,.025f,.025f,-.025f,.0175f,-.0175f,-.0175f,.0175f}; float[] sgyoffsets = new float[8] {-0.025f,-.025f,.025f,.025f,-.0175f,-.0175f, .0175f, .0175f }; float[] sgzoffsets = new float[8] { -0.025f, .025f, .025f, -.025f, .0175f, -.0175f, -.0175f, .0175f }; WeaponSwap WepSwap = transform.GetComponent();
switch (selection)
{
case 0: //Iron Shotugn Ferrous Fieldpiece
switch (alternate) {
case 0:
fireRate = 70;
if (firing == true)
{
ttf = Time.time + (1 / (fireRate / 60));
for (int i = 0; i < 8; i++)
{
toffset = new Vector3(sgxoffsets[i], sgyoffsets[i], sgzoffsets[i]);
var Vial = Instantiate(vial, transform.position, Cam.transform.rotation);
Vial.velocity = (90*(transform.forward +toffset));
Vial.GetComponent<VialBehavior>().vialElement = EleManager.instance.currentElement;
Vial.GetComponent<VialBehavior>().dmg = 2f;
}`
我认为您是在本地坐标中定义传播,但随后您将其与世界坐标中的 transform.forward 一起使用。
考虑在 toffset 上使用 transform.TransformPoint,然后再将其添加到 transform.forward
https://docs.unity3d.com/ScriptReference/Transform.TransformPoint.html
在使用 Chuck 的 link 进行试验后,我查看了不同的选项并发现 transform.TransformVector
,它创建了一个恒定的点差,请参见此处:
for (int i = 0; i < 8; i++)
{
toffset = new Vector3(sgxoffsets[i], sgyoffsets[i], sgzoffsets[i]);
var Vial = Instantiate(vial, transform.position, Cam.transform.rotation);
Vial.velocity = (90*(transform.forward + transform.TransformVector(toffset)));
Vial.GetComponent<VialBehavior>().vialElement = EleManager.instance.currentElement;
Vial.GetComponent<VialBehavior>().dmg = 2f;
}