Shoot Bullet||doc136845

September 20, 2022

Shoot Bullet

image.png

image.png

image.png

The following code is written in the global main script of the client. If you are not familiar with the details, please seeGlobal script

--Create an instance of the "gui_shot" window

image.png


Get the "shotbtn" control and bind the callback function to the click event. The function function is to send the "shot" protocol to the server.

--Get the "shotbtn" window instance

Add the code to receive the "shot" protocol in the main script of the server. The callback function of the receiving protocol is to create bullets and give them initial attributes.

The following code is inServerWritten in the global main script of. If you are not familiar with the details, please seeGlobal script

--Function executed after receiving the "shoot" protocol

The place where the bullet initialization attribute is created above is equivalent to the writing below

--Function executed after receiving the "shoot" protocol

Click to start the game and test the effect.

2.gif


In the editor, we can see that in the local coordinate system of the barrel, the upward direction of the Y axis is the direction we want the bullet to launch, so we use the upward direction of the barrel to calculate the face direction and linear velocity of the bullet.

image.png

API used:

UI:CreateGUIWindow()
Create a GUI instance according to the layout path and return
Window.Parent
Determine the parent of the window
UI.Root
GUI root node when the game is running. Only GUI instances that are direct or indirect sub levels of root will be displayed in the game
Window:GetChildByName()
Returns the child window according to the window name
Window:GetEvent()
Get the event of the window control
Bindable:Bind()
Bind a callback function that receives multiple group parameters and returns a binding handle
PackageHandlers:SendToServer()
Client sends protocol to server
PackageHandlers:Receive()
Receive protocols from the other end
Instance:FindFirstChild()
Get child node instances by name
MovableNode.WorldPosition
Global coordinates
MovableNode.WorldScale
Global zoom
MovableNode.ForwardVector
Forward vector
BasePart.LinearVelocity
Linear speed of parts
BasePart.UseGravity
Does the part use gravity
MovableNode.UpVector
Up vector
Instance.new()
Create a specific type of instance object