I am trying to make a shooter with a shotgun that is projectile based. However, at certain angles the spread changes from what it should be, im guessing due to the variation in transform. Forward. Is there anyway to fix these seemingly random offsets? `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;
}`
CodePudding user response:
I think you're defining the spread in local coordinates but then you're using that with transform.forward which is in world coordinates.
Consider using transform.TransformPoint on the toffset before adding it to transform.forward
https://docs.unity3d.com/ScriptReference/Transform.TransformPoint.html
CodePudding user response:
After Experimenting with Chuck's link, I looked at different options and found transform.TransformVector
, which created a constant spread see here:
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;
}