Make an object do a 180 turn before moving forward

Hello everyone,

A game I’m working on involves the player having to cross the street to get to the objective. One way a player can die is by being run over by a car (I’m not sure if this is allowed on Roblox). The issue stems from the fact that the way the cars are scripted makes it so that they travel back and forth, continuously exiting and entering the main map. This makes the cars visually unpleasing and unnatural to look at for the players. Is there a way to make the car do a 180-degree turn before entering the map again so that it would look as if the car was being driven forward instead of backward? A short video regarding the issue is provided at the end of the paragraph.

I forgot to add the code for the car so here it is;

while true do
wait()
for i= 1, 250 do
script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0,0,-2)
wait()
	end
for i= 1, 250 do
script.Parent.CFrame = script.Parent.CFrame * CFrame.new(0,0,2)
wait()
end
end

It is very poorly made, any suggestion on changing it would be appreciated.
https://gyazo.com/bb5642aca60a3fd19e60b17b56d66eb8

1 Like

I suggest tweening the car from point to point

local tService = game:GetService("TweenService")

local car = script.Parent:WaitForChild("Car") -- Location of you car
local p1 = script.Parent:WaitForChild("Point1") -- Location of your 1st part
local p2 = script.Parent:WaitForChild("Point2") -- Location of your 2nd part


local tween1 = tService:Create(car,TweenInfo.new(2),{Position = p2.Position})
local tween2 = tService:Create(car,TweenInfo.new(2),{Position = p1.Position})
tween1:Play()

tween1.Completed:Connect(function()
	car.Orientation = Vector3.new(0,0,0)
	tween2:Play()
end)

tween2.Completed:Connect(function()
	car.Orientation = Vector3.new(0,-180,0)
	tween1:Play()
end)

I put all the stuff in a folder like this
image

2 Likes

I thank you for the reply. I’m not available to test it out in the studio at the moment, but I’ll make sure to give a reply as fast as possible.

1 Like

https://gyazo.com/64237ba2ee73671492357409722b8d7b
This doesn’t feel right.

3321imahre

You’re tweening both the orientation and position when youre meant to tween only the position and set the orientation

may I see your code?

local tService = game:GetService("TweenService")

local car = script.Parent:WaitForChild("Moving brick") -- Location of you car
local p1 = script.Parent:WaitForChild("PointA") -- Location of your 1st part
local p2 = script.Parent:WaitForChild("PointB") -- Location of your 2nd part


local tween1 = tService:Create(car,TweenInfo.new(2),{Position = p2.Position})
local tween2 = tService:Create(car,TweenInfo.new(2),{Position = p1.Position})
tween1:Play()

tween1.Completed:Connect(function()
	car.Orientation = Vector3.new(0,0,0)
	tween2:Play()
end)

tween2.Completed:Connect(function()
	car.Orientation = Vector3.new(0,-180,0)
	tween1:Play()
end)
1 Like

Instead of Handling the Cars using a for loop, you can use RunService to Determine the exact amount of Time that has passed, WIth this you can Interpolate the Car a Certain Amount of times and when it reaches a certain point, you can “change a state” to tell it to go back.

For Performance Purposes, I recommend handling this for ALL Vehicles with their own Respective Data, as Connecting Multiple of these Events can Potentially cause Lag due to how fast the run each second, but for this example, it will be based on a Single Model for Obvious Reasons.

local RunService = game:GetService("RunService")
-- The RunService Instance
-- Commonly used to Run Events within Frames

local Rotation = CFrame.Angles(0, math.pi, 0)
-- CFrames use Radians as a Unit of Measure for Rotations
-- math.pi with this logic would be exactly 180 degrees when converted
-- using math.rad() or the formula 'x(pi/180)'

local Model = script.Parent
-- This works by Interpolating the Model itself instead of a Part inside
-- of the Model

local BaseCFrame = Model:GetPivot() -- This is the Starting CFrame
local StepCFrame = CFrame.new(0,0, 500) -- This is your Expected Position
-- Your for loop is iterating 250 times
-- your CFrame steps 2 times i the Z Axis when this happens
-- 250 * 2 is 500
-- Expected Time to Reach this Point: 2.5 Seconds
-- Feel free to Modify these to your Liking


local WillReverse = false -- TO Check if we should Reverse the Value

local alpha = 0 -- This is to keep track of how much we want to interpolate
local duration = 2.5 -- the time that it takes to complete the action

RunService.Heartbeat:Connect(function(deltaTime)
	alpha = math.min(1, alpha + (deltaTime/duration))
	-- I'll Explain the math here
	-- math.min is used to set a limit to the Current Value we have
	-- so it does not go over 1, because Interpolation works by
	-- setting a position within a Time Frame (0 to 1)
	-- DeltaTime means "The Difference in Time" or in this case Frames
	-- DeltaTime/Duration has the Value move within that duration

	local FowardInterpolation = BaseCFrame:Lerp(StepCFrame, alpha) -- The Foward Interpolation (Going Foward)
	local ReverseInterpolation = StepCFrame:Lerp(BaseCFrame, alpha) -- The Reserve Interpolation (Going Back)
	
	local Current = if not WillReverse then -- This will Determine which method will be chosen if 'WillReserve' is set
		FowardInterpolation * Rotation -- a Still Touch of Rotation
		else
		ReverseInterpolation
	-- if these arent going to the Right way you want them too
	-- you can always modify the Values, or Swap these values


	Model:PivotTo(Current) -- Moves the Model based off of Interpolated Points

	if alpha == 1 then -- if the time frame is exactly 1 (meaning 2.5 Seconds has passed)
		WillReverse = not WillReverse 
		-- if this value is false, it will become true,and  vice versa
		alpha = 0 -- Resets the Timer this time going in Reverse
	end

	print(alpha, WillReverse) -- for Debugging Purposes, Feel free to remove this
end)
2 Likes

Thank you for the reply, but i seem to have a problem:

It doesn’t enter the main map; instead, it seems to be moving sideways.

https://gyazo.com/feefe8030251789bd6426ea7cf8fcbc7

1 Like

As I said:

But for the StepCFrame, you can just say:

local StepCFrame = BaseCFrame * CFrame.new(0,0, 500)

Would should fix the Issue

1 Like

The script finally functions as intended, and I thank you all for your kind efforts.

1 Like

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