Part's Orientation not Rotating

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()

Video:

I mean, it is rotating in some way.

But what exactly do you want to achieve here? You want to make the cat go in the direction it’s moving? I don’t really get the problem at the moment.

I want it to face the direction it’s going, but its always facing the wrong way

If I’m not mistaken, you’re declaring LookAt but not using it anywhere.

local lookAt = CFrame.lookAt(Gato.Position, NextPos)

Try setting the tween’s goal to that one instead

2 Likes

It’s now facing the direction It’s intending to go, but unsure on how to go about moving it’s entire body to it’s next position however.

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

1 Like

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.

1 Like

I added this but It’ll reset it’s rotation and tween it back to the rotation I want to go to. I’m unsure how to fix it.

local Dur = math.floor(((NextPos - Gato.Position).Magnitude / 8)*100)/100 -- Rounds up the duration to 2 decimal places.
local FaceTo = TS:Create(Gato, TweenInfo.new(0.1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {CFrame = LookAt})
local MoveTo = TS:Create(Gato, TweenInfo.new(Dur, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {CFrame = CFrame.new(NextPos + LookAt.Rotation.Position)})
FaceTo:Play()
		
task.wait(.1)
		
MoveTo:Play()
1 Like

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:

Combine Position and Rotation:

local position, rotation = Instance.new("Vector3Value"), Instance.new("CFrameValue")
position.Value, rotation.Value = pivot.Position, pivot.Rotation

Callback.CallAndConnectToEach(function()
	mock:PivotTo(rotation.Value + position.Value)
end, {position.Changed, rotation.Changed})

Tween Position:

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.

1 Like

OK i’ll try this to translate this over to my project later on. Thank y’all again for the help!

1 Like

YOU WERE RIGHT!! THANK YOU SO MUCH, IM FREE FROM MY CHAINS!!!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.