Projectile coding

Discussions about Coding and Scripting
Post Reply
Torax
Adept
Posts: 406
Joined: Fri Apr 20, 2012 6:38 pm
Personal rank: Master of Coop
Location: Odessa, Ukraine

Projectile coding

Post by Torax »

Is it possible to make code for projectile to able it penetrate level surfaces? One variant i used once - set bCollideWorld to false but after that it wont spawn any effects when contact the surface and give unneeded effects: when projectile speed is low and it reaches level surface, it disappears. Only giving to projectile extremely high speeds able it to go through walls. But...if speed is too high - it wont touch and make damage to pawns. Any ideas? Or all this happens in cause of specifics of engine and basics of level structure?
Unreal. Alter your reality..forever...
JackGriffin
Godlike
Posts: 3774
Joined: Fri Jan 14, 2011 1:53 pm
Personal rank: -Retired-

Re: Projectile coding

Post by JackGriffin »

You are going to have to code a replacement class for the moment it touches the wall. Take a look at this video from the 3:14 mark:
[youtube]KXmdzgt6sR4[/youtube]
That dud bouncing from the wall effect was easy to achieve. I just destroyed the projectile and replaced it with a wood plank, meshed to look like a redeemer. It bounces off the wall, spins, and then falls to the ground perfectly.

You'll have to do something like that here. You'll need to replace your projectile on wall touch with something that behaves exactly like you want it to. The effects and stuff can still be called onto the touch position, they are not dependent on the projectile then. You won't get a smooth effect if it's slow though. UT will cease to render an object once it's middle is obscured, in this case below the wall surface. So my guess is that it will seem to penetrate then disappear towards the end. If it were me, I'd handle the penetration via a series of model animations where more and more of your projectile disappears, then place this against a wall so it seems to be passing into it.
So long, and thanks for all the fish
User avatar
Feralidragon
Godlike
Posts: 5493
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Re: Projectile coding

Post by Feralidragon »

Actors are generally not allowed to exist outside the world. The best thing you can do is:
- when the projectile hits a wall, make it hidden, remove its collision and set its physics to none;
- from there, in each tick you make the calculations yourself (based on speed and acceleration) in where the projectile should be at on that time;
- try to make a SetLocation of the projectile to that point, and if SetLocation fails (meaning it cannot place the projectile there), then you know it's still inside the wall;
- once SetLocation succeeds, then you're at the other side of the wall, so just add its collision, make it visible and set its physics to the one it had before, and the projectile will keep going normally.

NOTE: During the time it tries the SetLocation, the projectile remains where it last hit the wall, so it's not really inside the wall during the whole time.

I did something similar to my IRPR sniper so it could shoot through walls (although I didn't use projectiles, it's hitscan, so I had to detect the other side of the wall during the moment of the shot).
JackGriffin
Godlike
Posts: 3774
Joined: Fri Jan 14, 2011 1:53 pm
Personal rank: -Retired-

Re: Projectile coding

Post by JackGriffin »

Oh yeah, you could do that with the trace fire effect that the minigun and sniper rifle has with minimum effort too. The trace won't render in the hidden parts.
So long, and thanks for all the fish
Torax
Adept
Posts: 406
Joined: Fri Apr 20, 2012 6:38 pm
Personal rank: Master of Coop
Location: Odessa, Ukraine

Re: Projectile coding

Post by Torax »

Whell guys, here it is. Stuff what i done. File hosting where i uploaded pics cut them a bit...but i think you understand that i used UT minigun tracer as mesh for projectile.
And yes, I used projectile, not trace fire.

Image
As you can see, projectile normally penetrates wall, spawns effects at wall entry point (leftmost) and at wall exit (rightmost). Under effects I mean bulletholes, sparkles and smoke. Wall that it goes through is pretty thick, but script ables to configure wall thickness and number of walls to penetrate.

Image
The second wall projectile went through.
Maximum limit of penetrations I set to 2 here, that means that when it hit third surface of level, it will be destroyed.

The only known bug (at now) - when it hits too thick surface (for example i will shoot ground on this map) it spawns two hit effects at once. And also I didn't tested script online yet, so dunno how it will work. But it gives possibility to add a piece of realism using projectiles instead instant hit trace fire to pierce walls. But I should search ways to fix bugs and to test it online.

I don't know will it be useful, maybe it's easy to make for more advanced scripters, but I wanted to create this effect long time ago. From Times i started to play Unreal and making mods, because didn't saw something like this.
And now I reached some level of experience in scripting to make this.

Thanks for help and good advice, guys)

:rock:
Unreal. Alter your reality..forever...
Torax
Adept
Posts: 406
Joined: Fri Apr 20, 2012 6:38 pm
Personal rank: Master of Coop
Location: Odessa, Ukraine

Re: Projectile coding

Post by Torax »

Whell, finally i fixed known bugs. Offline it works absolutely normally, without crashes or bugs. Big thanks to Feralidragon for one advise which helped me to solve problems)

Posting here code of this function with comments. Maybe somebody will need this stuff in some ideas)
And maybe first look on code will explain to advanced coder will it work normally online, because I'm thinking about did i correctly used "simulated" prefix or it must be removed...any ideas about this? Or needed online tests to show how it will act between server and client?

Code: Select all

var() float PenetrationPower; // Maximum thickness of surface that projectile can penetrate
var() int PenetrationFactor;  // Maximum amount of surfaces to let projectile go through

var int i; //Int number to count PenetrationPower

simulated function HitWall (vector HitNormal, actor Wall)
{

     Spawn(class'UT_HeavyWallHitEffect',,, Location, Rotator(HitNormal)); // Spawn effect at the entry point of projectile

     if ( i < PenetrationFactor ) // Check out does i reached PenetrationFactor each time it hits wall, if yes - destroy projectile
       {
        ProcessPenetration(Location, HitNormal);
        i++;       
       }
     else
       {
        Destroy(); 
       }
 
      if ( Role == ROLE_Authority )
	{
		if ( (Mover(Wall) != None) && Mover(Wall).bDamageTriggered )
			Wall.TakeDamage( Damage, instigator, Location, MomentumTransfer * Normal(Velocity), '');
		MakeNoise(1.0);      
	}

   
}

simulated function ProcessPenetration(vector HitLocation, vector HitNormal)
{
  local vector TraceHitLocation, StartTrace, EndTrace;   
     
     StartTrace = Location;
     EndTrace = StartTrace  + PenetrationPower * vector(Rotation); // Calculating PenetrationPower
     
     FastTrace (EndTrace,EndTrace); // Trace to check does surface thicker, than allowable amount of PenetrationPower
     
     if ( FastTrace (EndTrace,EndTrace) == True ) // If True, then apply penetration. Otherwise destroy projectile..
     {

      Trace (TraceHitLocation, HitNormal, StartTrace, EndTrace, False); // Process normal Trace from one side of surface to other and set output point of projectile

      SetLocation(TraceHitLocation);                            // Set location of projectile to output point

      Spawn(class'UT_HeavyWallHitEffect',,, TraceHitLocation, Rotator(HitNormal)); // Spawn effects at output point
     }
     else
     {
      Destroy();
     }
}
Unreal. Alter your reality..forever...
User avatar
Feralidragon
Godlike
Posts: 5493
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Re: Projectile coding

Post by Feralidragon »

"simulated" only means "run on client too", that's is all (and depending on the circumstances in which the function is called or declared in the replication block, it may run solely in the client or not run at all, but that's a bit more hard to explain, as it depends on many other variables and the code flow itself and what each function is supposed to do (in short: replication)).
I will explain all that soon in my set of tutorials though.
Torax
Adept
Posts: 406
Joined: Fri Apr 20, 2012 6:38 pm
Personal rank: Master of Coop
Location: Odessa, Ukraine

Re: Projectile coding

Post by Torax »

Okay, thanks
waiting for new tuts)
Unreal. Alter your reality..forever...
MrLoathsome
Inhuman
Posts: 958
Joined: Wed Mar 31, 2010 9:02 pm
Personal rank: I am quite rank.
Location: MrLoathsome fell out of the world!

Re: Projectile coding

Post by MrLoathsome »

Correct me if I am wrong, but I think you can eliminate 1 of the calls to FastTrace in your ProcessPenetration function.

Code: Select all

     FastTrace (EndTrace,EndTrace); // Trace to check does surface thicker, than allowable amount of PenetrationPower
     
     if ( FastTrace (EndTrace,EndTrace) == True ) // If True, then apply penetration. Otherwise destroy projectile..
     {
Could just be:

Code: Select all

     if ( FastTrace (EndTrace,EndTrace) )
     {
blarg
Torax
Adept
Posts: 406
Joined: Fri Apr 20, 2012 6:38 pm
Personal rank: Master of Coop
Location: Odessa, Ukraine

Re: Projectile coding

Post by Torax »

Yep)

I just wrote a bit more more code for myself, to better and more accurately understand what I'm doing and how)
It is also possible (as I think) to combine HitWall and ProcessPenetration functions.
They were separated also to make script readable and more understandable. Also to able make specific changes in code, such like additional effects and so on
Unreal. Alter your reality..forever...
Post Reply