• Please make sure you are familiar with the forum rules. You can find them here: https://forums.tripwireinteractive.com/index.php?threads/forum-rules.2334636/

Code having an issue adding spread to projectile spawner

Falidell

Grizzled Veteran
May 22, 2009
645
78
34
Mesa,Az,USA
this is my current piece of code related for adding spread.. i know it might be rather dirty.
Code:
var() float Accuracyspread;

function rotator randRot()
{
	local rotator NewRot;
	NewRot.Yaw=self.rotation.yaw+(((Frand()*1000)-(Frand()*1000))*AccuracySpread);
	NewRot.pitch=self.rotation.pitch+(((Frand()*1000)-(Frand()*1000))*AccuracySpread);
	NewRot.roll=self.rotation.roll;
	return NewRot;
}

//later on i use this function the the spawning part.

p=spawn(ProjectileType,,,Jitter(),RandRot());

now i also have what i call a jitter area which is the Jitter function shown here

Code:
function vector Jitter()
{
	local vector NewVec;
	NewVec.X=self.Location.X+RandRange(SpawnJitterArea.X*-1,SpawnJitterArea.X);
	NewVec.Y=self.Location.Y+RandRange(SpawnJitterArea.Y*-1,SpawnJitterArea.Y);
	NewVec.Z=self.Location.Z+RandRange(SpawnJitterArea.Z*-1,SpawnJitterArea.Z); 
	return NewVec;
}

so i know i can use a function in place of a rotator/vector variable to provide a new location/rotation for spawning things my problem is that when i test it out there is no rotation at all even if i set AcurracySpread=2 instead of a more reasonable 0.25. no matter what i put it has no effect what so ever.

the jitter works fine it's just the spread/rotation function isn't working.... anyways i was wondering if im just doing something stupid? i never was very good at math or equations so i may very well have overcomplicated/f'ed up the math.

extra usefull information:
-this is for a trigger actor that spawns projectiles. not a gun.
-other then this part the rest works A-ok.

-here is the rest of the code if you wish to see
Spoiler!
 
Last edited:
I suggest working with this code:

Code:
var() int ProjPerFire;
var() Vector ProjSpawnOffset; // +x forward, +y right, +z up
function DoFireEffect()
{
    local Vector StartProj, StartTrace, X,Y,Z;
    local Rotator R, Aim;
    local Vector HitLocation, HitNormal;
    local Actor Other;
    local int p;
    local int SpawnCount;
    local float theta;
    Instigator.MakeNoise(1.0);
    Weapon.GetViewAxes(X,Y,Z);
    StartTrace = Instigator.Location + Instigator.EyePosition();// + X*Instigator.CollisionRadius;
    StartProj = StartTrace + X*ProjSpawnOffset.X;
    if ( !Weapon.WeaponCentered() )
     StartProj = StartProj + Weapon.Hand * Y*ProjSpawnOffset.Y + Z*ProjSpawnOffset.Z;
    // check if projectile would spawn through a wall and adjust start location accordingly
    Other = Weapon.Trace(HitLocation, HitNormal, StartProj, StartTrace, false);
// Collision attachment debugging
 /*   if( Other.IsA('ROCollisionAttachment'))
    {
     log(self$"'s trace hit "$Other.Base$" Collision attachment");
    }*/
    if (Other != None)
    {
        StartProj = HitLocation;
    }
    Aim = AdjustAim(StartProj, AimError);
    SpawnCount = Max(1, ProjPerFire * int(Load));
    switch (SpreadStyle)
    {
    case SS_Random:
        X = Vector(Aim);
        for (p = 0; p < SpawnCount; p++)
        {
            R.Yaw = Spread * (FRand()-0.5);
            R.Pitch = Spread * (FRand()-0.5);
            R.Roll = Spread * (FRand()-0.5);
            SpawnProjectile(StartProj, Rotator(X >> R));
        }
        break;
    case SS_Line:
        for (p = 0; p < SpawnCount; p++)
        {
            theta = Spread*PI/32768*(p - float(SpawnCount-1)/2.0);
            X.X = Cos(theta);
            X.Y = Sin(theta);
            X.Z = 0.0;
            SpawnProjectile(StartProj, Rotator(X >> Aim));
        }
        break;
    default:
        SpawnProjectile(StartProj, Aim);
    }
}
function projectile SpawnProjectile(Vector Start, Rotator Dir)
{
    local Projectile p;
    if( ProjectileClass != None )
        p = Weapon.Spawn(ProjectileClass,,, Start, Dir);
    if( p == None )
        return None;
    p.Damage *= DamageAtten;
    return p;
}
simulated function vector GetFireStart(vector X, vector Y, vector Z)
{
    return Instigator.Location + Instigator.EyePosition() + X*ProjSpawnOffset.X + Y*ProjSpawnOffset.Y + Z*ProjSpawnOffset.Z;
}
defaultproperties
{
    ProjPerFire=1
    NoAmmoSound=Sound'Inf_Weapons_Foley.Misc.dryfire_rifle'
    ProjSpawnOffset=(X=0,Y=0,Z=-10)
    bLeadTarget=true
    bInstantHit=false
 WarnTargetPct=+0.5
}
 
  • Like
Reactions: Falidell
Upvote 0
ooo thankyou so much yoyo!! =D i might just use the ss_random and/or ss_line pieces of code. to go along with my jitter function. mainly because that code looks to be for a gun or pawn type thingy.. this is a trigger actor( Extends Triggers ) im making.

im a little lost only because of the names you have choosen for them what

exactly is SS_line. by the looks of it, it should simulate actual gunfire spread?
EDIT: ok i staired at it for a bit and came up with a another theory for SS_line. it creates an imaginary circle and spawns the projectile in a random location within that circle.so it's kind of like my jitter

and ss_random also looks like it'll simulate actual gunfire spread but doesn't make a circle which is perfectly ok by me.

just a little lost on that as i said im no good with math =/ ... lol silly i know


and again thankyou so much for your help with this so far =)
 
Last edited:
Upvote 0
ooo thankyou so much yoyo!! =D i might just use the ss_random and/or ss_line pieces of code. to go along with my jitter function. mainly because that code looks to be for a gun or pawn type thingy.. this is a trigger actor( Extends Triggers ) im making.

im a little lost only because of the names you have choosen for them what

exactly is SS_line. by the looks of it, it should simulate actual gunfire spread?
EDIT: ok i staired at it for a bit and came up with a another theory for SS_line. it creates an imaginary circle and spawns the projectile in a random location within that circle.so it's kind of like my jitter

and ss_random also looks like it'll simulate actual gunfire spread but doesn't make a circle which is perfectly ok by me.

just a little lost on that as i said im no good with math =/ ... lol silly i know


and again thankyou so much for your help with this so far =)


SS_Line will determine the distance between each shot, higher the spread the further apart it will be from the last shot. SS_Random works exactly how the KF gun fire does; randomly.
 
Upvote 0
ah sweet and it works perfectly thanks again Yoyo!! =D

at first it didn't then i realized it needs to be "pinged" if you will to update the random rotator

i managed to do with with
Code:
	function updatelocationrotation()
	{
		nA=AccuracySpread*10000;
		NewVec.X=self.Location.X+RandRange(SpawnJitterArea.X*-1,SpawnJitterArea.X);
		NewVec.Y=self.Location.Y+RandRange(SpawnJitterArea.Y*-1,SpawnJitterArea.Y);
		NewVec.Z=self.Location.Z+RandRange(SpawnJitterArea.Z*-1,SpawnJitterArea.Z);
		NewRot.Yaw=(nA*(FRand()-0.5));
		NewRot.pitch=(nA*(FRand()-0.5));
		NewRot.roll=(nA*(FRand()-0.5));
		oldrot.yaw=rotation.yaw+NewRot.yaw;
		oldrot.pitch=rotation.pitch+NewRot.pitch;
		oldrot.roll=rotation.roll+NewRot.roll;
	}

and that funtion is called just before the projectile is spawned =D

works like a charm.

nA=AccuracySpread*10000; this part is just so 0.25=degree's or close to it.. i'll fine tune it and such so it does actually turn 0.25 into 25degree's
Edit: my thoughts are sense 360degrees in unrealunits is 65536 so / that by 360 = 182.04444... so
i think that if i did
nA=182.05*(AccuracySpread*100) that should make it so 0.25=25degree spread right?


and yes i know i have oldrot and newrot backwards lol im just too lazy to rename them atm. more focused on getting it down 100% the way i want it to be

your a live saver =D
 
Last edited:
Upvote 0