-
Notifications
You must be signed in to change notification settings - Fork 12
/
TestVehicle.cs
113 lines (96 loc) · 3.17 KB
/
TestVehicle.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Authors: Artur Filipowicz
Version: 0.9
Copyright: 2016
MIT License
*/
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using GTA;
using GTA.Native;
using GTA.Math;
// Controls:
// NumPad0 - spawns in a new test vehicle
// I - mounts rendering camera on vehicle
// O - restores the rendering camera to original control
public class TestVehicle : Script
{
// camera used on the vehicle
Camera camera = null;
Vehicle vehicle;
public TestVehicle()
{
UI.Notify("Loaded TestVehicle.cs");
// create a new camera
World.DestroyAllCameras();
camera = World.CreateCamera(new Vector3(), new Vector3(), 50);
camera.IsActive = true;
GTA.Native.Function.Call(Hash.RENDER_SCRIPT_CAMS, false, true, camera.Handle, true, true);
// attach time methods
Tick += OnTick;
KeyUp += onKeyUp;
}
// Function used to create a new test vehicle and set its properties.
public void spawnVehicle()
{
vehicle = World.CreateVehicle(VehicleHash.Adder, Game.Player.Character.Position + Game.Player.Character.ForwardVector * 3.0f, Game.Player.Character.Heading + 90);
vehicle.CanTiresBurst = false;
vehicle.CanBeVisiblyDamaged = false;
vehicle.CanWheelsBreak = false;
vehicle.PrimaryColor = VehicleColor.MetallicBlack;
vehicle.SecondaryColor = VehicleColor.MetallicOrange;
vehicle.PlaceOnGround();
vehicle.NumberPlate = " P17 ";
}
// Function used to take control of the world rendering camera.
public void mountCameraOnVehicle()
{
if (Game.Player.Character.IsInVehicle())
{
GTA.Native.Function.Call(Hash.RENDER_SCRIPT_CAMS, true, true, camera.Handle, true, true);
}
else
{
UI.Notify("Please enter a vehicle.");
}
}
// Function used to allows the user original control of the camera.
public void restoreCamera()
{
UI.Notify("Relinquishing control");
GTA.Native.Function.Call(Hash.RENDER_SCRIPT_CAMS, false, false, camera.Handle, true, true);
}
// Function used to keep camera on vehicle and facing forward on each tick step.
public void keepCameraOnVehicle()
{
if (Game.Player.Character.IsInVehicle())
{
// keep the camera in the same position relative to the car
camera.AttachTo(Game.Player.Character.CurrentVehicle, new Vector3(0f, 2f, 0.4f));
// rotate the camera to face the same direction as the car
camera.Rotation = Game.Player.Character.CurrentVehicle.Rotation;
}
}
// Test vehicle controls
private void onKeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.NumPad0)
{
spawnVehicle();
}
if (e.KeyCode == Keys.I)
{
mountCameraOnVehicle();
}
if (e.KeyCode == Keys.O)
{
restoreCamera();
}
}
void OnTick(object sender, EventArgs e)
{
keepCameraOnVehicle();
}
}