Recently, I’ve been trying to make a part that will randomly move in a random direction when no player is around it. I got it to move and even calculate how long it should take it to get to a certain point, however it will never rotate and I’ve been at it, researching different ways for the past 4 hours to no avail. I’m unsure on why the orientation isn’t calculating correctly and was hoping someone could help me out with it.
script:
local Players = game:GetService("Players")
local TS = game:GetService("TweenService")
local Parts = game.Workspace.Parts
local Gato = script.Parent.PrimaryPart
local MinWait = 1
local MaxWait = 5
local WaitTime = math.random(MinWait,MaxWait)
local Radius = 20
local DetectionRadius = 250
local function CheckForComputer()
-- Not relevant
end
local function GetClosestPlayer()
-- Not relevant
end
local function StartUp()
while true do
local ComputerNearby, ComputerPos = CheckForComputer()
local ClosestPlr, PlrDis = GetClosestPlayer()
local NextPos
local LookAt
-- Checks if a computer is nearby
if ComputerNearby then
print("Computer nearby")
--elseif ClosestPlr then -- Checks if a player is nearby
-- print("Player is nearby!")
else -- Goes to a random position if the player is not around.
print("No one is around.")
NextPos = Vector3.new(Gato.Position.X + math.random(-Radius, Radius), Gato.Position.Y, Gato.Position.Z + math.random(-Radius, Radius))
LookAt = Gato.CFrame + CFrame.lookAt(Gato.CFrame.Position, NextPos)
end
local Dur = math.floor(((NextPos - Gato.Position).Magnitude / 8)*100)/100 -- Rounds up the duration to 2 decimal places.
local MoveTo = TS:Create(Gato, TweenInfo.new(Dur, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {CFrame = CFrame.new(NextPos)})
print("Move to ["..tostring(NextPos).."] in "..tostring(Dur).." seconds, facing ["..tostring(LookAt).."]")
MoveTo:Play()
-- Randomizes the wait time before the gato moves again
WaitTime = Dur + math.random(MinWait,MaxWait)
task.wait(WaitTime)
end
end
StartUp()
Im not sure about this but maybe at first tween the direction it faces and to move the whole thing you try seting an CFrame for the coordinates by calculating how far it should go but again im not sure how to do that ill get back to you if i find how to
Take the Rotation from the LookAt variable and add NextPos to it by using LookAt.Rotation + NextPos so that you have the rotated CFrame at the right Position. If you want to have Position and Rotation Tween separately, I can help you figure out how to do it with ValueBase Objects, but this solution should still be fine.
The CFrame for the second Tween has a logic error. It should be LookAt.Rotation + NextPos, not CFrame.new(NextPos + LookAt.Rotation.Position). Also, there is a more flexible method for doing this that is a bit more complex, and you would need to ensure that the “steps” that Gato takes don’t overlap, but you can Tween Rotation and Position on separate “channels” by mixing a CFrameValue and Vector3Value Object. This would let you Tween Rotation quickly while Position is also being Tweened at the same time as normal, so you can make your animation better. All you have to do is something like this, which I use for a Construct (or Placement) Tool in one of my projects:
local position = GetResolvedPosition()
if not position:FuzzyEq(Pivot.Position, EPSILON) then
Pivot = Pivot.Rotation + position
TweenService:Create(Position, TweenParameters2, {Value = position}):Play()
end
Tween Rotation:
if rotation then
Pivot = rotation * Pivot.Rotation + Pivot.Position
TweenService:Create(Rotation, TweenParameters3, {Value = Pivot.Rotation}):Play()
end
Again, this is from one of my projects, so it isn’t a drag-and-drop solution for you, but hopefully, this can give you a better general idea of how to improve your animation or at least get it working properly.