Search In
• More options...
Find results that contain...
Find results in...

# [C#.NET| Drawlight on attached vehicle bone change always the light direction

## Recommended Posts

Hello guys.

I tried to draw a light on the position of a vehicle bone.

I used DRAW_LIGHT_WITH_RANGE,DRAW_SPOT_LIGHT and GET_WORLD_POSITION_OF_ENTITY_BONE native functions to make it to work.

But really all of these natives gives me a problem. The direction where the light will draw, will always be a specific direction of the world.

What that does that mean? Well, if i move the vehicle (rotate my vehicle), then the light direction goes a little bit different. But if i move the car at the same position like i was before, then i will get the same light direction like before.

I know that vehicle bones are not changing they rotation or something while driving. They always stay at the pivot of bone direction.

And i tried to use custom values on the Z axis instead of the bone Z value, but that just simply makes the whole light like not working.

So what is my main question? How can i freeze the light direction? I would like to use a spotlight, but there i have this problem on all axis, X,Y,Z.

Its like leaving the Z value somewhere on the map if you know what i mean.

I hope someone can help me with this.

I just draw the coordinates of the bone on screen, i can see that the values are always changing like normal coordinates. I know that the game caculates the current bone position, but why does the rotation get so hard affected?

Thanks

MrGTAmodgerman

##### Share on other sites
Posted (edited)

As far as I know, spotlights point in a direction relative to the light's position. So if you attach it to the front of a vehicle and point it ahead to light the ground in front of the vehicle, if you turn 90 degrees to the right, the light would be pointing to the left side of the vehicle because the light doesn't rotate.

So if you had a direction of Vector(0f, .75f, -1f), that would be pointing slightly ahead in a downward direction, if you were heading at 0 degrees. But it would be pointing to the left if the vehicle turned 90 degrees to the right.

So this code would put a light 3f units above the chassis and point it in a direction .75f ahead, which lights the road just in front of the vehicle. As you rotate, the light points in the same place relative to the vehicle... so as you rotate, the light rotates.

Short video showing what this code does (when it finishes processing this 13 second video).

Edit: I have just edited that code to put a proper check on playerVehicle or it will get a null object reference.

I suspect the best way to do this, would be to specify a position offset from the vehicle and then turn that into a rotation for the light. My maths isn't that good so I can't create the code that does that so quickly.

`    Vehicle playerVehicle = Game.Player.Character.CurrentVehicle;    if (playerVehicle != null)    {        Vector3 lightPosition = playerVehicle.GetBoneCoord(playerVehicle.GetBoneIndex("chassis_dummy"));        Vector3 lightOffset = new Vector3(0, 0, 3f);        float distance = 10f;        float brightness = 10f;        float roundness = 1f;        float radius = 20f;        float fadeout = .5f;        float dirX = (float)Math.Cos((playerVehicle.Heading + 90f) * 0.01745329f);        float dirY = (float)Math.Sin((playerVehicle.Heading + 90f) * 0.01745329f);                    World.DrawSpotLight(lightPosition + lightOffset, new Vector3(dirX * .75f, dirY * .75f, -1f), Color.Red, distance, brightness, roundness, radius, fadeout);    }`
Edited by LeeC2202

##### Share on other sites
Posted (edited)

As far as I know, spotlights point in a direction relative to the light's position. So if you attach it to the front of a vehicle and point it ahead to light the ground in front of the vehicle, if you turn 90 degrees to the right, the light would be pointing to the left side of the vehicle because the light doesn't rotate.

So if you had a direction of Vector(0f, .75f, -1f), that would be pointing slightly ahead in a downward direction, if you were heading at 0 degrees. But it would be pointing to the left if the vehicle turned 90 degrees to the right.

So this code would put a light 3f units above the chassis and point it in a direction .75f ahead, which lights the road just in front of the vehicle. As you rotate, the light points in the same place relative to the vehicle... so as you rotate, the light rotates.

Short video showing what this code does (when it finishes processing this 13 second video).

`    Vehicle playerVehicle = Game.Player.Character.CurrentVehicle;    Vector3 lightPosition = playerVehicle.GetBoneCoord(playerVehicle.GetBoneIndex("chassis_dummy"));    Vector3 lightOffset = new Vector3(0, 0, 3f);    float distance = 10f;    float brightness = 10f;    float roundness = 1f;    float radius = 20f;    float fadeout = .5f;    float dirX = (float)Math.Cos((playerVehicle.Heading + 90f) * 0.01745329f);    float dirY = (float)Math.Sin((playerVehicle.Heading + 90f) * 0.01745329f);                World.DrawSpotLight(lightPosition + lightOffset, new Vector3(dirX * .75f, dirY * .75f, -1f), Color.Red, distance, brightness, roundness, radius, fadeout);`

Oh thank you man. You was so fast and soo much helpful. I really miss you in the GTA 5 mods community. You have so much skills. Thank you man!

I thank you before i test it, let me see.

Edited by MrGTAmodsgerman

##### Share on other sites

Ok, i got a problem which i always get on some codes on research of this problem. Because alot of people wrote Color.Red or what ever.

For some reason, Color does not work for me in any way. I tried a lot of things, but i have the updated SHV.NET DLL in my project.

##### Share on other sites

I have just edited that code to put a proper check on playerVehicle or it will get a null object reference.

I suspect the best way to do this, would be to specify a position offset from the vehicle and then turn that into a rotation for the light. My maths isn't that good so I can't create the code that does that so quickly.

##### Share on other sites

Ok, i got a problem which i always get on some codes on research of this problem. Because alot of people wrote Color.Red or what ever.

For some reason, Color does not work for me in any way. I tried a lot of things, but i have the updated SHV.NET DLL in my project.

Make sure you have using System.Drawing; at the top of your code.

##### Share on other sites
Posted (edited)

I suspect the best way to do this, would be to specify a position offset from the vehicle and then turn that into a rotation for the light. My maths isn't that good so I can't create the code that does that so quickly.

I would like to try your code and my math is not that good too, but i can't use spotlights for now because he wants Color. which always does not exist in my current context as VS says. Wrierd.

EDIT:

Yes i already tried this. That does not change anything.

EDIT2: Ohh wrierd, for some reason the resource itself was missing. works now

Edited by MrGTAmodsgerman

##### Share on other sites

Oops, sorry... I forgot about the Reference as well. It's always in my default mod template, so I forget that you have to add it.

##### Share on other sites
Posted (edited)

Oops, sorry... I forgot about the Reference as well. It's always in my default mod template, so I forget that you have to add it.

No problem

Can you tell me now what you did there?

`            float dirX = (float)Math.Cos((playerVehicle.Heading + 90f) * 0.01745329f);            float dirY = (float)Math.Sin((playerVehicle.Heading + 90f) * 0.01745329f);`

Maybe i am too noobish, but why or what is this float that you multiplie? 0.01745329?

`new Vector3(dirX * .75f, dirY * .75f, -1f)`

And then where you create the spot light, you just created a new vector with a multiplie of 75. But why 75? If you do this here, why did you create a own offset value then? Yea. I am too noobish and not that math guy. I know that a Vector3 means everything around the main point. So a multiplie would increase the distance between the point that i want and what happend.

So basicly i want only to change the light direction and such.

What i need to edit?

I try to understand your text above

Edited by MrGTAmodsgerman

##### Share on other sites
Posted (edited)

Multiplying the heading by 0.01745329f converts from Degrees to Radians, which is what Sin and Cos need to work with. I have a function called Deg2Rad() which does the same thing.

I actually used .75f out of error. I presumed that 1f would be directly ahead, so I thought .75f would be at a diagonal angle. But 1f doesn't seem to be directly ahead at all, so my thinking was flawed. The higher that number is, the further away from the vehicle the light will be. But that also works with this line:

`Vector3 lightOffset = new Vector3(0, 0, 3f);`

If you increase the 3f, then the light also gets further away.

See this is my problem, I don't always understand things with Vectors, so I end up throwing the first number that comes into my head at it. Sometimes I get a result of some kind that I can work with, like in this case. But it always feels like trial and error... it's why I only ever show simple code snippets, as I know the experienced coders would just love to rip my code to pieces for its poor quality.

Edit: Ah, I see what it is now. I just set that number (the .75f) to 3.14152 and the light was straight ahead, so they're angles in radians. Doh! So...

`World.DrawSpotLight(lightPosition + lightOffset, new Vector3(dirX * (45f * 0.01745329f), dirY * (45f * 0.01745329f), -(45f * 0.01745329f)), Color.White, distance, brightness, roundness, radius, fadeout);`

Points the light in a 45 degree angle from its position.

Edited by LeeC2202

##### Share on other sites
Posted (edited)

Edit: Ah, I see what it is now. I just set that number (the .75f) to 3.14152 and the light was straight ahead, so they're angles in radians. Doh! So...

Points the light in a 45 degree angle from its position.

Well thats sounds all so complicated. It feels like that X Y Z values are all the same. What do i need to change when the light don't move with the vehicle in sideway. I mean, the vehicle stands on a hill and the light is still in position like on a flat ground you know? Also what about moving the light to the left or right? If i change one of the values, then something goes wrong in caculation, just like that the spot light moves around while rotating the vehicle. It seems like that i never will able to make these values usefriendly for writing them into a INI file

I didn't expected that. But you helped me to get very near to want i was intended to do. I should read all this here again when i have more time for testing.

Edited by MrGTAmodsgerman

##### Share on other sites
Posted (edited)

This might help:

When you are in a vehicle, use RB + Look Left & RIght to rotate the light around the vehicle and use RB + Look Up & Down to alter the light angle. I have no idea whether this code is "correct" but it seems to work.

It might at least give you a starting point and then maybe someone more experienced can tell you the proper way to do it.

`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, 3f);        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;        public cSpotlightTest()        {            Tick += onTick;            Interval = 0;            LastGameTime = CurrentGameTime;        }        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 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;                }                // Clamp the light angle between LightPitchMin and LightPitchMax 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);                // Calculate the rotations                float dirX = (float)Math.Cos(Deg2Rad(lightHeading));                float dirY = (float)Math.Sin(Deg2Rad(lightHeading));                float dirZ = (float)Math.Sin(Deg2Rad(LightAngle));                World.DrawSpotLightWithShadow(lightPosition + LightOffset, new Vector3(dirX, dirY, dirZ), Color.White, Distance, Brightness, Roundness, Radius, Fadeout);            }        }        // COnverts Degrees to Radians        private float Deg2Rad(float deg)        {            return deg * 0.01745329f;        }    }}`
Edited by LeeC2202

##### Share on other sites
Posted (edited)

Sorry, yesterday it was my b-day. I wasn't able to answer

Well its a really great script. Its like the police search light from the LSPD mod. But it rotates around the dummy. My question was, how to get the light from a straight different position. I mean. I want to move the light to the left from the dummy position without having issues. It would be great to have a little script like you posted that would save the important settings. But with real X Y Z movement. So i can use the settings then for having the light on the right position, angle and such.

But from what i said on the usefriendly ini settings, i was meaning to let the use choose the settings of how far the light moves sideways from the real dummy and how the light points and that setting different for any vehicle model.

But don't expect i was asking you to write that for me. I was only want to understand how this works to make this maybe possible you know?

I want simply lets say. let the user choose, how far goes the light away to the left side from the original dummy position, and where the light points at, and what vehicle model it is.

For that, i need to know from your code, what i need to change to have the light starting a little bit on away from the original postion on the Y axis without having alot of other things changed.

I think my english is too bad to tell you what i want to ask you

Maybe i should tell you my script idea in PM, so you can understand that a little bit more.

Edited by MrGTAmodsgerman

##### Share on other sites

Well after trying and failing at a couple of options, the best I ended up with was:

`float OffsetX = 3;float OffsetY = 1;`

Those would be the two offsets defined in the ini file to adjust the X and Y position, then:

`Vector3 spotLightOffset = (playerVehicle.RightVector.Normalized * OffsetX)+ (playerVehicle.ForwardVector.Normalized * OffsetY) + LightOffset;World.DrawSpotLightWithShadow(lightPosition + spotLightOffset, new Vector3(dirX, dirY, dirZ), Color.White, Distance, Brightness, Roundness, Radius, Fadeout);`

That will make the spotlight rotate around the offset position instead of the dummy location.

##### Share on other sites

Well after trying and failing at a couple of options, the best I ended up with was:

`float OffsetX = 3;float OffsetY = 1;`

Those would be the two offsets defined in the ini file to adjust the X and Y position, then:

`Vector3 spotLightOffset = (playerVehicle.RightVector.Normalized * OffsetX)+ (playerVehicle.ForwardVector.Normalized * OffsetY) + LightOffset;World.DrawSpotLightWithShadow(lightPosition + spotLightOffset, new Vector3(dirX, dirY, dirZ), Color.White, Distance, Brightness, Roundness, Radius, Fadeout);`

That will make the spotlight rotate around the offset position instead of the dummy location.

Thank you for this

Now i got a little question.

Do you think that is possible to use a bone with a custom name? For example i create car mod with a custom bone name and use that instead of the existing one. I tried this before but it doesn't work because the game have generated a hash for the bones. Do you think its possible to generate it by scripting or something? That would be so awesome to have, because everyone can only script with existing props. But it noone was able to find where all these are defined to make it to work.

##### Share on other sites

I have never seen a car with a custom bone, so I honestly couldn't answer that.

You can search for a bone by string name, so that makes me think it might be possible. I will try and add a custom bone to a vehicle and check. ZM3 is not my thing though, so there's no guarantees I'll get anywhere. If you know how to do that, just change the string used in this code I have posted to the name of the bone (or dummy) and see if it recognises it.

##### Share on other sites
Posted (edited)

I have never seen a car with a custom bone, so I honestly couldn't answer that.

You can search for a bone by string name, so that makes me think it might be possible. I will try and add a custom bone to a vehicle and check. ZM3 is not my thing though, so there's no guarantees I'll get anywhere. If you know how to do that, just change the string used in this code I have posted to the name of the bone (or dummy) and see if it recognises it.

I know this and i tried to use by string already. It does not to do anything thats why i am asking. There a lot car mods out there with some model part names that aren't supported by the game.

I tried now to use Game.GenerateHash. But execept of Menyoo settings got reset, i didn't found anything else.

If you want to have such as vehicle with custom bone name, then i can make it for you really quick.

__________________________________________________________________________________________________________________

But back to thread. I have a big problem which i didn't realise before. Any R* Editor records with my script can't get load. I get instant loading screen. Idk if this really related to the spot light, but it would make sense, because the spot light is instant rendered while playing. Its not like attached, the game still create the spotlight all the time at the position caculated from the bone. I really need to do something to it to fix that, otherwise my whole thing is useless or does not really makes sense.

The other problem is, i still have the problem that the light don't move sideways with the vehicle. So if my chassis is lifting down on one site, because suspension is bumping, then the light will stay in position. So any rotation like, driving on 2 wheels, doesn't affect the light rotation. Idk what i need to do. Heading should do it or not?

Edited by MrGTAmodsgerman

##### Share on other sites

The R* Editor doesn't record any scripted lights, I had the same problem with the bus lights mod I created. I have just tried recording something to test and it didn't cause any problems, it just didn't record the light.

The other problem is, i still have the problem that the light don't move sideways with the vehicle. So if my chassis is lifting down on one site, because suspension is bumping, then the light will stay in position. So any rotation like, driving on 2 wheels, doesn't affect the light rotation. Idk what i need to do.

That, unfortunately, is a consequence of my limited knowledge of maths and Vectors. I have just had a quick look at the code and I honestly have no idea where to even start with that. My head knows what should be happening but I just can't translate that into code.

The whole way I have done this is probably wrong, which is why this problem exists. The frustrating part is that for a 3D programmer, this is probably a 2-minute problem/fix but instead, you're left with my hacked-together answers. I don't know what else I can offer, so I can only apologise.

I post code sometimes in the hope that people's eagerness to correct anything I write, makes them provide an answer but it doesn't always work. I am more than happy to take a hit of being embarrassed, if it gets someone the answer they need.

##### Share on other sites
Posted (edited)

The R* Editor doesn't record any scripted lights, I had the same problem with the bus lights mod I created. I have just tried recording something to test and it didn't cause any problems, it just didn't record the light.

The other problem is, i still have the problem that the light don't move sideways with the vehicle. So if my chassis is lifting down on one site, because suspension is bumping, then the light will stay in position. So any rotation like, driving on 2 wheels, doesn't affect the light rotation. Idk what i need to do.

That, unfortunately, is a consequence of my limited knowledge of maths and Vectors. I have just had a quick look at the code and I honestly have no idea where to even start with that. My head knows what should be happening but I just can't translate that into code.

The whole way I have done this is probably wrong, which is why this problem exists. The frustrating part is that for a 3D programmer, this is probably a 2-minute problem/fix but instead, you're left with my hacked-together answers. I don't know what else I can offer, so I can only apologise.

I post code sometimes in the hope that people's eagerness to correct anything I write, makes them provide an answer but it doesn't always work. I am more than happy to take a hit of being embarrassed, if it gets someone the answer they need.

Don't worry, you did a very good way and i think that this problem only needs one more line to fix.

But i wonder if we can script that the R* editor records those lights. I found that we are able to create a RenderCamera. How to use it? Idk. Well i am that 3D Artist guy, but idk what i need to look for-

Edited by MrGTAmodsgerman

##### Share on other sites

I spent another 3 hours on that spotlight thing today and I almost got it working. I've just hit a brick wall, so I need a break from it.

Unless someone manages to run scripts inside the editor, there's not going to be anyway to get scripted stuff running in it. That's why looped particles don't work, because they have to be managed by script. That's also why I have spent the last few weeks writing my own replay system, so I can do things like this:

But even that wouldn't record something where you have used controller input to move a light, the lights in that mod are all script controlled with no user input. That would probably be the same with the R* editor, even if someone did manage to get scripts running. It all depends on what the R* editor actually records, it might record all inputs but it might just record all entity states instead... not really sure.

##### Share on other sites
Posted (edited)
But even that wouldn't record something where you have used controller input to move a light, the lights in that mod are all script controlled with no user input. That would probably be the same with the R* editor, even if someone did manage to get scripts running. It all depends on what the R* editor actually records, it might record all inputs but it might just record all entity states instead... not really sure.

I am not trying to make the light controllable with a controller input like you posted above. I only want to let the user decide the properties of the position in a INI file. So will that change the fact on what is possible?

Edited by MrGTAmodsgerman

##### Share on other sites

But even that wouldn't record something where you have used controller input to move a light, the lights in that mod are all script controlled with no user input. That would probably be the same with the R* editor, even if someone did manage to get scripts running. It all depends on what the R* editor actually records, it might record all inputs but it might just record all entity states instead... not really sure.

I am not trying to make the light controllable with a controller input like you posted above. I only want to let the user decide the properties of the position in a INI file. So will that change the fact on what is possible?

If someone gets scripts running in the R* Editor, then just having a light like that would be doable no problem. What you need is one of the experienced C++ guys to really dig into the idea, to see if it's even possible. I haven't noticed anyone mention the idea for a long time now though, so maybe people have given up on the idea.

##### Share on other sites

But even that wouldn't record something where you have used controller input to move a light, the lights in that mod are all script controlled with no user input. That would probably be the same with the R* editor, even if someone did manage to get scripts running. It all depends on what the R* editor actually records, it might record all inputs but it might just record all entity states instead... not really sure.

I am not trying to make the light controllable with a controller input like you posted above. I only want to let the user decide the properties of the position in a INI file. So will that change the fact on what is possible?

If someone gets scripts running in the R* Editor, then just having a light like that would be doable no problem. What you need is one of the experienced C++ guys to really dig into the idea, to see if it's even possible. I haven't noticed anyone mention the idea for a long time now though, so maybe people have given up on the idea.

Do you think that this was scripted into the Editor? https://de.gta5-mods.com/scripts/extended-video-export

##### Share on other sites

I think that just hooks into the "Render Movie" process to render out the video frames. I don't think it's doing anything actually inside the editor.

I was just thinking about this mod while I was having some food and one thing crossed my mind, where is this light going to physically come from? Do you have a prop that is going to be at the light source location, or is it going to just shine from an invisible point in the world? Because if you don't have a light prop, it's going to look really odd. I just did a quick test with my replay mod, I deliberately turned out the car headlights so you can see the full effect. I think having no "cause" for the effect means it doesn't really look like the light is coming from the car.

If you had a light prop, you could solve a lot of problems because that prop could move relative to the car and the spotlight could inherit all the rotations from the prop. That would mean that as the car changed angle, the light would move with it.

##### Share on other sites
Posted (edited)
If you had a light prop, you could solve a lot of problems because that prop could move relative to the car and the spotlight could inherit all the rotations from the prop. That would mean that as the car changed angle, the light would move with it.

Yea but i need to adjust the settings, otherwise i will get a problem. I was thinking about if there is some way to attach the spotlight like a prop. Because it is an entity.

The other idea of making it would be duplicating the one vehicle bone light, but that is not possible i think.

Edited by MrGTAmodsgerman

##### Share on other sites

Jesus, this is such a hackfest. The spotlight prop "prop_spot_01" is facing backwards, so if you try and inherit the vehicle rotation, it's facing the wrong way. Then the light rotation values are different to prop rotation values. It's like they got three groups of people and said "Right guys, do your own thing and no communicating... got it?". For example, the vehicle might be heading at 0,0,0, the prop-spotlight needs to be at 0,0,180 to match that but 0,0,0 points the light-spotlight at the ground.

But having said that, placing the prop there makes a huge difference to how it looks. Interesting that if you spawn the spotlight as a prop, you get the light cone, if you attach it to the vehicle, you lose the light cone.

If I get some more time at some point, I will look further into this. What it needs is a function that translates the prop rotation to a light rotation value. But I really need to get some more cameras added to my replay system, so I can't spend any more time on this right now, sorry. I'll tidy this mess of code up and post another block of code later.

Pity nobody else wants to chip in with some ideas/suggestions.

##### Share on other sites
Posted (edited)

Didn't get chance to post anything last night, bug chasing in my menu... anyway, turns out that if you add a prop, life is so much easier. Basically now, wherever the prop is and which ever direction it is facing in, the light will draw based on that. As you can see, this massively reduces the amount of code required.

Interesting thing I found with the spotlight prop, if you create it at 0,0,0 and then attach it to the vehicle, you don't get that big white ugly cone. If you create it in the correct position in the world, you get the cone. So how you create it, dictates how it appears, which I found very odd.

But... and here's the decision you have to make, if you do create it in a way that you get the cone, that cone is also the light source, so you don't need to use the World.DrawSpotLightWithShadow call... but you don't get the shadows with it. I will try and create a small video later to show the difference.

If I was choosing, I'd go for the no cone option, I think the cone looks ugly and there seems to be no way to adjust the opacity.

Anyway, hope this is of some use...

Edit: In case this line is confusing "Draw the light from slightly behind the spotlight prop so the prop doesn't cause problems", it's because the prop faces backwards, so to draw the light in front of the emissive parts, you're actually drawing behind them, not in front of them. I have no idea why R* did that with this prop.

`using System;using System.Drawing;using GTA;using GTA.Math;namespace PropLightSync{    public class cPropLightSync : Script    {        private Ped PlayerPed;        private Vehicle PlayerVehicle;        private Prop SpotlightProp;        const string SpotName = "prop_spot_01";        private Vector3 SpotlightPropRotation;        private Vector3 SpotlightPropOffset = new Vector3(0, 0, 1.5f);        private float Distance = 200f;        private float Brightness = 5f;        private float Roundness = 1f;        private float Radius = 10f;        private float Fadeout = 5f;        public cPropLightSync()        {            Tick += onTick;            Aborted += onAborted;            Interval = 0;        }        private void onTick(object sender, EventArgs e)        {            // Exits from the loop if the game is loading            if (Game.IsLoading) return;            // A collection of lines that process the player's current state, along with the player's vehicle            if (PlayerPed != Game.Player.Character) PlayerPed = Game.Player.Character;            if (PlayerPed.IsSittingInVehicle() && PlayerVehicle != PlayerPed.CurrentVehicle) SetNewVehicle();            if (PlayerPed.IsOnFoot && PlayerVehicle != null) PlayerVehicle = null;            if (PlayerPed.IsSittingInVehicle())            {                SpotlightProp.AttachTo(PlayerVehicle, PlayerVehicle.GetBoneIndex("chassis_dummy"), SpotlightPropOffset, SpotlightPropRotation);                // Get the spotlight rotation values based on the prop rotation                float rotX = (float)Math.Cos(Deg2Rad(SpotlightProp.Rotation.Z - 90f));                float rotY = (float)Math.Sin(Deg2Rad(SpotlightProp.Rotation.Z - 90f));                float rotZ = (float)Math.Sin(Deg2Rad(-SpotlightProp.Rotation.X));                // Draw the light from slightly behind the spotlight prop so the prop doesn't cause problems                World.DrawSpotLightWithShadow(SpotlightProp.GetOffsetInWorldCoords(new Vector3(0, -1f, 0)), new Vector3(rotX, rotY, rotZ), Color.White, Distance, Brightness, Roundness, Radius, Fadeout);            }            else            {                if (SpotlightProp != null)                {                    SpotlightProp.MarkAsNoLongerNeeded();                    SpotlightProp.Delete();                }            }        }        private void SetNewVehicle()        {            PlayerVehicle = PlayerPed.CurrentVehicle;            if (SpotlightProp == null) SpotlightProp = World.CreateProp(SpotName, Vector3.Zero, false, false);            SpotlightProp.AttachTo(PlayerVehicle, PlayerVehicle.GetBoneIndex("chassis_dummy"), SpotlightPropOffset, new Vector3(10, 0, 180f - PlayerVehicle.Rotation.Z));            SpotlightPropRotation = new Vector3(SpotlightProp.Rotation.X, SpotlightProp.Rotation.Y, SpotlightProp.Rotation.Z);        }        private float Deg2Rad(float deg)        {            return deg * 0.01745329f;        }        private void onAborted(object sender, EventArgs e)        {            if (SpotlightProp != null)            {                SpotlightProp.MarkAsNoLongerNeeded();                SpotlightProp.Delete();            }        }    }}`
Edited by LeeC2202

##### Share on other sites
Posted (edited)

Okay, here's the video I mentioned.

The first section is created with this code:

`if (SpotlightProp == null) SpotlightProp = World.CreateProp(SpotName, Vector3.Zero, false, false);SpotlightProp.AttachTo(PlayerVehicle, PlayerVehicle.GetBoneIndex("chassis_dummy"), SpotlightPropOffset, new Vector3(10, 0, 180f - PlayerVehicle.Rotation.Z));`

You can see that creates the prop at 0,0,0 and then attaches it with the correct offset to place it on the vehicle.

The second section is created with this code:

`Vector3 spotPos = PlayerVehicle.GetBoneCoord("chassis_dummy") + SpotlightPropOffset;if (SpotlightProp == null) SpotlightProp = World.CreateProp(SpotName, spotPos, false, false);SpotlightProp.AttachTo(PlayerVehicle, PlayerVehicle.GetBoneIndex("chassis_dummy"), SpotlightPropOffset, new Vector3(10, 0, 180f - PlayerVehicle.Rotation.Z));`

There the position is pre-calculated and the prop spawned directly at the location it will be used.

Weird how the two methods get different end results.

Edited by LeeC2202

##### Share on other sites

Didn't get chance to post anything last night, bug chasing in my menu... anyway, turns out that if you add a prop, life is so much easier. Basically now, wherever the prop is and which ever direction it is facing in, the light will draw based on that. As you can see, this massively reduces the amount of code required.

Interesting thing I found with the spotlight prop, if you create it at 0,0,0 and then attach it to the vehicle, you don't get that big white ugly cone. If you create it in the correct position in the world, you get the cone. So how you create it, dictates how it appears, which I found very odd.

But... and here's the decision you have to make, if you do create it in a way that you get the cone, that cone is also the light source, so you don't need to use the World.DrawSpotLightWithShadow call... but you don't get the shadows with it. I will try and create a small video later to show the difference.

If I was choosing, I'd go for the no cone option, I think the cone looks ugly and there seems to be no way to adjust the opacity.

Anyway, hope this is of some use...

Edit: In case this line is confusing "Draw the light from slightly behind the spotlight prop so the prop doesn't cause problems", it's because the prop faces backwards, so to draw the light in front of the emissive parts, you're actually drawing behind them, not in front of them. I have no idea why R* did that with this prop.

Well thanks this is good. I was thinking about why the game need a prop for the light. A prop have its own rotation and his own state of dynamic or not. But a simple spotlight always need coordinates to stand at. It have no foots or something.

I will test this later. But i asked me if

`        private Vector3 SpotlightPropOffset = new Vector3(0, 0, 1.5f);`

this is the part that makes me wanna decide the own distance between the vehicle bone like you posted before.

Also i need to really have the prop invisible. I know that R* Editor have problems by showing or rendering things that are was set invisible.

## Create an account

Register a new account