fivem scripting I'm working on a building script how can i make two object for example…
- User
fivem scripting I'm working on a building script how can i make two object for example the floor and wall snap together
- Assistant
To make two objects, such as the floor and wall, snap together in a FiveM script, you'll need to use the coordinates and rotation of the objects. Here's a simple example:
-
First, make sure you have the coordinates and dimensions of your floor and wall objects. For this example, let's say the floor object has a width of 10, and the wall object has a width of 2.
-
Set up your script to create the floor and wall objects at desired locations. You can use CreateObject native function to create the objects in-game.
local floor_model = "your_floor_model" local wall_model = "your_wall_model" local floor_coords = vector3(x, y, z) local wall_coords = vector3(x, y, z) local floor_obj = CreateObject(GetHashKey(floor_model), floor_coords, true, false, false) local wall_obj = CreateObject(GetHashKey(wall_model), wall_coords, true, false, false) -
- User
I want to use raycasting to find where the wall is and if its near the floor would connect to that side. the floor is a square with 4 sides so the wall can connect to any of the 4 side and just depends on what side the player is looking
- Assistant
In that case, you can use raycasting to determine which side of the floor the player is looking at, and then snap the wall object to that side. Here's how you can achieve that:
-
First, create the objects and obtain the respective object handles as shown previously.
-
Create a function to calculate the raycast hit position and normal:
function Raycast(origin, direction, range, ignoreEntities) local rayHandle = StartShapeTestRay(origin.x, origin.y, origin.z, direction.x, direction.y, direction.z, -1, ignoreEntities and ignoreEntities or GetPlayerPed(-1), 0) local _, _, resultPos, resultNormal, entityType = GetShapeTestResult(rayHandle) return resultPos, resultNormal, entityType end -