How to play a gold miner game in Reworld

Function effect display:


Function introduction

A ray is an invisible line. Ray is used to obtain the entities in the 3D scene through the player's client input, so as to realize the function of interaction between the player and the 3D entity units. Common usage includes shooting to obtain shooting target, mouse click scene, etc.

Rays can also be used between solid objects to emit rays from an object, so as to obtain parts and roles in the ray direction. It can be used to determine whether there is a target in a certain direction of a specified object, or calculate the distance between objects.

Comparison between functions

Same: click trigger and ray to interact with entity;

Difference: only the part or role with click trigger can interact, and the ray can interact with any part.

Functional usage restrictions

When the ray hits the character, it returns the whole character.

Train of thought analysis:

The function effect is that the character clicks on the gold mine to generate a line connecting the character and the gold mine. The gold mine is pulled to the character and disappears after approaching. The number of gold mines increases and is displayed.

The role clicks the gold mine, clicks the event with the mouse, and then obtains the clicked gold mine parts, which can be obtained by using the ray function. The generation line adopts the line drawing service in the global service; The gold mine is pulled to the character and calculated using coordinates. Close to disappear and increase the number to display on the interface. You can use the destroy function and text control.

Function construction:

1. Brush a road out with the terrain



2. Use the basic parts to make a gold mine. Because the final movement needs to be realized through coordinate calculation, the gold mine should be a whole part.



3. Adjust the lens parameters to make the angle of view appropriate, and record the values:



4. Make a UI interface to display the number of gold mines:




5. Close unnecessary UI templates:




6. Set as a stand-alone game, so the code can be written in the client script. Create a client script and write the following code.

local player = Players:GetLocalPlayer()--Get player
local speed = 5--Movement speed of gold mine
local iscd = true--Have you pulled a gold mine
local goldtxt = GameUI.Golden interface.Gold number--Number of gold coins UI Interface
local goldnum = 0--Total gold coins obtained
GameUI.Golden interface.Gold number.Text = goldnum--Gold coin display initialization
WorkSpace.CurCamera.MaxZoomDistance = 30--Set camera parameters
WorkSpace.CurCamera.Distance = 15
WorkSpace.CurCamera.Pitch = 3.6
WorkSpace.CurCamera.Offset = Vector3(0,-4,0)
WorkSpace.CurCamera.Yaw = 90
WorkSpace.CurCamera.CameraType = Enum.CameraType.Fixed
local function CollectGold()--Definition method
    if iscd == true then--If there is no gold mine being collected
        iscd = false--After entering the collection status, you cannot collect again
        local ray = camera:ScreenToRay(Mouse.MousePosition) -- Emit rays according to the screen position of the mouse,  
        local e,p = WorkSpace:FindObjOnRay(ray,WorkSpace.New base plate) --Detect part objects in radiographic testing
        if e and e.Name == "gold mine" then--If you hit a gold mine
            local path = {player.Avatar.Position,e.Position}--Using roles and parts as starting points
            local dspos = e.Position
            DrawLine(path)--Draw lines using the marking service
            while Vector3.Distance(e.Position,player.Avatar.Position) > 1 do--When the distance between the gold mine and people is greater than 1, the gold mine gradually moves to the role
                local lenth = Vector3.Normalize(e.Position - player.Avatar.Position)
                e.Position = e.Position - lenth*(0.01*speed)
                UnDrawLine()--Refresh line
                local startpos = player.Avatar.Position+ Vector3(0,0.5,0)
                local uppath = {startpos,e.Position}
            e:Destroy()--When the gold mine is close enough, destroy the gold mine
            UnDrawLine()--Cancel line
            goldnum = goldnum + 1--Increase the number of gold mines
            GameUI.Golden interface.Gold number.Text = goldnum--Displays the number of gold mines
            iscd = true--You can collect again
Mouse.MouseButtonLeftDown:Connect(CollectGold)--Left click trigger

Image source: Game box

Added by Pilly on Mon, 07 Mar 2022 22:09:59 +0200