Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

fivem scripting I'm working on a building script how can i make two object for example…

  1. User

    fivem scripting I'm working on a building script how can i make two object for example the floor and wall snap together

  2. 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:

    1. 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.

    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)
    
    1. Calculate the position of the wall object so that it snaps to the edge of the floor object. Since the floor object has a width of 10 and the wall object has a width of 2, you'll need to move the wall object to the right by half the value of their combined widths (10 + 2) / 2 = 6 units.
    local snapped_wall_coords = floor_coords + vector3(6, 0, 0)
    SetEntityCoords(wall_obj, snapped_wall_coords)
    
    1. You may also need to adjust the rotation of the wall object so that it aligns correctly with the floor object. For that, use the SetEntityRotation function:
    SetEntityRotation(wall_obj, vector3(0, 0, 0))
    

    This example assumes the objects are aligned on the X-axis, but you would need to adjust the coordinates according to your specific case. Remember to customize the model names and the dimensions of your objects, as well as the starting coordinates for floor_coords and wall_coords.

  3. 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

  4. 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:

    1. First, create the objects and obtain the respective object handles as shown previously.

    2. 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
    
    1. Get the player's camera direction and perform raycasting to find the floor hit position and normal:
    local playerCoord = GetEntityCoords(GetPlayerPed(-1))
    local _, direction = GetGameplayCamRot()
    local camDirection = RotToDirection(direction)
    
    local range = 20.0
    local hitPos, hitNormal, _ = Raycast(playerCoord, camDirection, range, false)
    
    1. Calculate the offset for snapping the wall to the floor based on the hit normal:
    local floor_width = 10
    local wall_width = 2
    local snap_offset = (floor_width + wall_width) / 2
    
    local snapped_wall_coords = hitPos + hitNormal * snap_offset
    
    1. Set the wall object coordinates and rotation:
    SetEntityCoords(wall_obj, snapped_wall_coords)
    SetEntityRotation(wall_obj, vector3(0, 0, 0))
    

    This script should be able to snap the wall object to the floor based on the player's camera direction with raycast hit position and normal. Note that you might need to tweak some values, like range, floor_width, and wall_width, according to your specific models and the desired snapping distance.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.