Jump to content
    1. Welcome to GTAForums!

    1. GTANet.com

    1. GTA Online

      1. Los Santos Drug Wars
      2. Updates
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Red Dead Online

      1. Blood Money
      2. Frontier Pursuits
      3. Find Lobbies & Outlaws
      4. Help & Support
    3. Crews

    1. Grand Theft Auto Series

      1. Bugs*
      2. St. Andrews Cathedral
    2. GTA VI

    3. GTA V

      1. Guides & Strategies
      2. Help & Support
    4. GTA IV

      1. The Lost and Damned
      2. The Ballad of Gay Tony
      3. Guides & Strategies
      4. Help & Support
    5. GTA San Andreas

      1. Classic GTA SA
      2. Guides & Strategies
      3. Help & Support
    6. GTA Vice City

      1. Classic GTA VC
      2. Guides & Strategies
      3. Help & Support
    7. GTA III

      1. Classic GTA III
      2. Guides & Strategies
      3. Help & Support
    8. Portable Games

      1. GTA Chinatown Wars
      2. GTA Vice City Stories
      3. GTA Liberty City Stories
    9. Top-Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    1. Red Dead Redemption 2

      1. PC
      2. Help & Support
    2. Red Dead Redemption

    1. GTA Mods

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Red Dead Mods

      1. Documentation
    3. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    4. Featured Mods

      1. Design Your Own Mission
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Rockstar Games

    2. Rockstar Collectors

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Movies & TV
      5. Music
      6. Sports
      7. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Announcements

    2. Support

    3. Suggestions

Happy Holidays from the GTANet team!

[C#] Create lights and attach light to vehicle


stef538
 Share

Recommended Posts

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

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.

  • Like 2
Link to comment
Share on other sites

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

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 by LeeC22
Link to comment
Share on other sites

This is wonderful! Thank you very much once again!

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.