Continuously Rotating Tower||doc136844

September 20, 2022

Continuously Rotating Tower

First,use the part template ("Grassland") to create a new project. Note: The block template cannot use the part function.

image.png


Click the game settings button in the upper right corner, select the "Perspective" tab, drag to the bottom and check "Enable Fixed Perspective", set the initial viewing angle to the coordinate (0, 10, 0) position of the scene "map001", and uncheck "Swipe Screen To Rotate Perspective" option.

image.png


Drag to the top of the viewing angle page, set the initial viewing angle type to "Fixed Perspective", and set the initial rotation to rotate 90 degrees around the X axis.

image.png


Click to start the game in the upper right corner and you can see that the camera is facing the ground.

image.png


Next we use the parts to make a turret, the turret contains body parts and barrel parts. Body parts anchored, coordinates (0,2,0), scaled (1,2,1). The barrel part is anchored, cancel collision, coordinates (0, 2.5, 1), rotation (90, 0, 0), scale (0.4, 1, 0.4).

image.png

image.png


After clicking to start the game in the upper right corner, you can see a defensive tower in the center of the screen.

image.png


To keep the turret's barrel spinning, we need to write a looping timer that keeps the barrel spinning around the center. First we create a timer that runs in a loop.

The following code is in Server Global Script,If you are not familiar with the details, please see Global Script

--The function to be executed by the timer
local function gunRotate()
    print("gunRotate")
end

--Create a timer and execute it after 1 frame
local rotateTimer = Timer.new(1, gunRotate)
--Open the timer loop to achieve the effect of executing every 1 frame
rotateTimer.Loop = true
--Start timer
rotateTimer:Start()

image.png


Then we add logic for the barrel to rotate around the center point to the timer function.

--The function to be executed by the timer
local function gunRotate()
    print("gunRotate")
    --Get static scene map001
    local map = World:GetStaticMap("map001")
    --Get the part named "Gun" in the scene
    local gunPart = map.Root:FindFirstChild("Gun")
    --If the "Gun" part is not obtained, it will not be executed
    if not gunPart then
        return
    end

    --center
    local centerPoint = Vector3.new(0, 2.5, 0)
    --axis
    local axis = Vector3.Up
    --"Gun" part rotated 1 degree about center point and axis
    gunPart:RotateAround(centerPoint, axis, 1)
end

As shown in the picture, the barrel takes the red dot as the center point, takes the vertical upward direction as the axis, and makes a circular motion around the center point.

image.png

1.gif

API used:

Timer.new()
Create a timer object
Timer.Loop
Whether the timer repeats
Timer:Start()
Start timer
World:GetStaticMap()
Get static scene
Instance:FindFirstChild()
Get child node instances by name
Vector3.new()
Create a 3D vector type data
Vector3.Up
Equivalent to vector3 new(0, 1, 0)
MovableNode:RotateAround()
Rotate around an axis at a point