Is there any hitscan laserbeam-like function that has autoaim?

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
Slayer777
Posts: 8
Joined: Sun Nov 12, 2023 5:42 am
Preferred Pronouns: He/Him

Re: Is there any hitscan laserbeam-like function that has autoaim?

Post by Slayer777 »

Code: Select all

	bool A_FireFragment(Class<Actor> projectile, double angleRange = 360, double vrange = 90, int numRays = 60, double distance = 512)
	{
		for (int i = 0; i < numRays; i++)
		{
			FTranslatedLineTarget t;
			double an = angleRange/numRays * i;
			double pitch = self.AimLineAttack(an, distance, t, vrange);
			if (t.linetarget != null && t.lineTarget != target)
			{
				vector3 offset = Vec3Offset(0, 0, 0);
				SpawnMissileXYZ(offset, t.lineTarget, projectile, true, target);
				return true;
			}
		}
		return false;
	}
Using SpawnMissileXYZ because it looks like SpawnMissile only launches from 32 units up.
Jarewill
 
 
Posts: 1807
Joined: Sun Jul 21, 2019 8:54 am

Re: Is there any hitscan laserbeam-like function that has autoaim?

Post by Jarewill »

The reason it does that is because you didn't add an offset on the Z axis, so the projectile flies from the actor's feet to the target's feet.
I assume you want the projectile to fly from the actor's feet to the enemy's center?
I am certain there is a better way to do this, but try this code:

Code: Select all

vector3 offset = Vec3Offset(0, 0, 0);
let proj = SpawnMissileXYZ(offset, t.lineTarget, projectile, true, target); //Cast the projectile to a pointer
proj.A_Stop(); //Stop the projectile
proj.Vel3DFromAngle(proj.speed,proj.AngleTo(t.lineTarget),proj.PitchTo(t.lineTarget,0,t.lineTarget.height/2)); //Aim it at the monster's center and fire

Return to “Scripting”