stef538 Posted May 1, 2020 Share Posted May 1, 2020 in GTA IV it was possible to just add a light and attach it to a vehicle. I have found "DRAW_SPOT_LIGHT" with the example code from NativeDB but it doesn't seem to work. Does anybody else have this issue? Link to comment Share on other sites More sharing options...
LeeC22 Posted May 1, 2020 Share Posted May 1, 2020 This is the line I used in the test project I did for someone else on here World.DrawSpotLightWithShadow(lightPosition + spotLightOffset, new Vector3(rotX, rotY, rotZ), Color.White, Distance, Brightness, Roundness, Radius, Fadeout); Distance, Brightness etc... are set to this: private float Distance = 200f; private float Brightness = 5f; private float Roundness = 1f; private float Radius = 10f; private float Fadeout = 5f; The rotX, rotY & rotZ were calculated with this: // Calculate the rotations float rotX = (float)Math.Cos(Deg2Rad(lightHeading)); float rotY = (float)Math.Sin(Deg2Rad(lightHeading)); float rotZ = (float)Math.Sin(Deg2Rad(LightAngle)); I attached a spotlight prop to the vehicle and then used that prop position as the source of the light... if you want the full test class, you are welcome to it. It's messy, full of commented out code but it works. You can control the spotlight as well. Jitnaught and stef538 2 Link to comment Share on other sites More sharing options...
stef538 Posted May 1, 2020 Author Share Posted May 1, 2020 Hmm, looks like I did something wrong because the light won't show up. Could you share that piece of code you were talking about? Link to comment Share on other sites More sharing options...
LeeC22 Posted May 1, 2020 Share Posted May 1, 2020 (edited) I've cleared out the commented stuff. I seem to be doing some strange stuff in there in some places and I did it that long ago, I'm not quite sure why... but it works. using System; using System.Drawing; using GTA; using GTA.Math; using Control = GTA.Control; namespace SpotlightTest { public class cSpotlightTest : Script { private int CurrentGameTime; private int LastGameTime; private Vector3 LightOffset = new Vector3(0, 0, 1f); private float LightHeading = 0f; private float LightAngle = -20f; private float Distance = 200f; private float Brightness = 5f; private float Roundness = 1f; private float Radius = 10f; private float Fadeout = 5f; private float LightPitchMin = -40f; private float LightPitchMax = 5f; private float HorizontalMoveSpeed = 60f; private float VerticalMoveSpeed = 50f; float OffsetRotate = 0f; private Prop SpotLight; public cSpotlightTest() { Tick += onTick; Aborted += CSpotlightTest_Aborted; Interval = 0; LastGameTime = CurrentGameTime; } private void CSpotlightTest_Aborted(object sender, EventArgs e) { if (SpotLight != null) { SpotLight.MarkAsNoLongerNeeded(); SpotLight.Delete(); } } private void onTick(object sender, EventArgs e) { // Exits from the loop if the game is loading if (Game.IsLoading) return; CurrentGameTime = Game.GameTime; int elapsedGameTime = CurrentGameTime - LastGameTime; LastGameTime = CurrentGameTime; float timeModifier = elapsedGameTime / 1000f; // If we have a vehicle if (Game.Player.Character.IsSittingInVehicle()) { // Get the player vehicle Vehicle playerVehicle = Game.Player.Character.CurrentVehicle; if (SpotLight == null) { float spotRotX = Deg2Rad(playerVehicle.Rotation.X); float spotRotY = Deg2Rad(playerVehicle.Rotation.Y); float spotRotZ = Deg2Rad(playerVehicle.Rotation.Z); SpotLight = World.CreateProp("prop_spot_01", Vector3.Zero, false, false); SpotLight.AttachTo(playerVehicle, playerVehicle.GetBoneIndex("chassis_dummy"), LightOffset + new Vector3(1f,0,0), new Vector3(0, 0, 180f)); } // If we are pressing Gamepad RB, disable the look controls so that we can move the spotlight if (Game.IsControlPressed(2, Control.VehicleAttack)) { Game.DisableControlThisFrame(2, Control.LookLeftRight); Game.DisableControlThisFrame(2, Control.LookUpDown); // Get some inputs to change the direction and pitch of the light LightHeading -= (Game.GetDisabledControlNormal(2, Control.LookLeftRight) * HorizontalMoveSpeed) * timeModifier; LightAngle += (Game.GetDisabledControlNormal(2, Control.LookUpDown) * VerticalMoveSpeed) * timeModifier; } LightHeading = (LightHeading + 360) % 360; // Clamp the light angle between 0 and -20 degrees LightAngle = Math.Max(Math.Min(LightAngle, LightPitchMax), LightPitchMin); // Get the bone position of the chassis Vector3 lightPosition = playerVehicle.GetBoneCoord(playerVehicle.GetBoneIndex("chassis_dummy")); // Compensate for the vehicle heading float lightHeading = LightHeading + (playerVehicle.Heading + 90f); lightHeading = (lightHeading + 360) % 360; // Calculate the rotations float rotX = (float)Math.Cos(Deg2Rad(lightHeading)); float rotY = (float)Math.Sin(Deg2Rad(lightHeading)); float rotZ = (float)Math.Sin(Deg2Rad(LightAngle)); float OffsetX = 1; float OffsetY = 0; Vector3 spotLightOffset = (playerVehicle.RightVector.Normalized * OffsetX) + (playerVehicle.ForwardVector.Normalized * OffsetY) + LightOffset; SpotLight.AttachTo(playerVehicle, playerVehicle.GetBoneIndex("chassis_dummy"), LightOffset + new Vector3(1f, 0, 0), new Vector3(-LightAngle, 0, LightHeading + 180f)); World.DrawSpotLightWithShadow(lightPosition + spotLightOffset, new Vector3(rotX, rotY, rotZ), Color.White, Distance, Brightness, Roundness, Radius, Fadeout); } } // Converts Degrees to Radians private float Deg2Rad(float deg) { return deg * 0.01745329f; } } } Some of the indenting is messed up because this forum software doesn't always like tabs. Edit: Meant to add, I was testing this with Trevor's Bodhi, so the light position is based on that vehicle. Edited May 1, 2020 by LeeC22 stef538 1 Link to comment Share on other sites More sharing options...
stef538 Posted May 2, 2020 Author Share Posted May 2, 2020 This is wonderful! Thank you very much once again! LeeC22 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now