The problem is that my car has a head attached to it, and that it is the DotProduct where its gonna be there. the problem is, i can’t seem to code a dotproduct.
A script would be helpful to give me a easy example for detecting angle for dotproduct.
The problem is that my car has a head attached to it, and that it is the DotProduct where its gonna be there. the problem is, i can’t seem to code a dotproduct.
A script would be helpful to give me a easy example for detecting angle for dotproduct.
You mean you want to get the dot product of two vectors? FirstVector3:Dot(SecondVector3)
no just like, If dotproduct == 0.5 then right, if -0.5 then left, how do i something like that?
i need a code example.
30
So with an if statement?
local dot = FirstVector:Dot(SecondVector)
if dot > 0.5 then
-- right
elseif dot < -0.5 then
-- left
end
doesn’t seem to work i don’t know what first vector does and second vector.
12
FirstVector is the looking direction of the car
something like car.head.CFrame.LookVector
And SecondVector is the relative position of whatever you want to check
something like plr.Character.PrimaryPart.Position - car.head.Position
works but, it keeps printing left
Then maybe the head is pointed in the wrong direction.
You can just do car.head.CFrame.RightVector instead.
I’m not 100% sure what your trying to do but you could do this with PointToObjectSpace
here is a demo project
PointToObjectSpace.rbxl (34.6 KB)
and this is the script inside the demo project
local target = workspace:WaitForChild("Target").Position
local character = script.Parent
while true do
local cFrame = character:GetPivot()
local offset = cFrame:PointToObjectSpace(target)
if offset.X < 0 then
print("Turn Left")
elseif offset.X > 0 then
print("Turn Right")
end
task.wait(1)
end
this will tell you to turn left or right to point towards the target position
and this is how to make a arrow that points to a target
Arrow.rbxl (35.4 KB)
local runService = game:GetService("RunService")
local target = workspace:WaitForChild("Target").Position
local arrow = workspace:WaitForChild("Arrow")
local character = script.Parent
runService.Heartbeat:Connect(function(deltaTime)
local cFrame = character:GetPivot()
arrow.CFrame = CFrame.lookAt(cFrame.Position + Vector3.yAxis * 5, target)
end)
Not, what i usually wanted but. since im making a NFS game, this is helpful for map indictators where to go. what im trying to do is that the car has a vision 180 degrees vision the other 90 right, the car will steer to the waypoint and other 90 left, so basically what i want is the car to steer its pathfinding way point.
you can use atan2 with PointToObjectSpace to work out the angle you need to turn here is a demo
PointToObjectSpace.rbxl (34.7 KB)
and this is the code in the demo
local character = script.Parent
local target = workspace:WaitForChild("Target")
while true do
local offset = character:GetPivot():PointToObjectSpace(target.Position)
local angle = math.atan2(-offset.X, -offset.Z)
local degrees = math.deg(angle)
if math.abs(degrees) > 90 then
print("Target is behind")
elseif degrees < 0 then
print("Turn Right")
elseif degrees > 0 then
print("Turn Left")
end
print(degrees)
task.wait(1)
end
degrees will go from -180 up to 180 depending on the direction to the target
and angle will go from -pi to pi