Mathematical Scripting Help

Hello,
I am in need for someone to help guide me in the right direction.

I need to find out the values ~5 studs away left and right from the tree although it needs to be relative to where the player is standing. These values would be perpendicular to where the player is standing/looking. How would I go about doing this?

I have provided an example image below:

2 Likes

Sorry if I misunderstood, but you could get the LookAt from the tree to the character (it gives the direction of where the character is originating from the tree) and then manipulate the result LookAt direction twice to give a left and right of the tree.

From this perspective, the tree’s left and right would be inverted to the player, it’s not an obstacle just simply keep that in mind and write your code accordingly.

Now, how you find the left and right lookvectors of your LookAt is a bunch of balony, so I’m going to have to send you to another post made by a much more experienced man. Skip the Vector3:Dot() section and read all about Vector3:Cross. I’ll also attach some sample code I cooked up, but it probably won’t help in your case.

local lookVector = ...-- Your lookat value (Vector3)

local rightVector = lookVector:Cross(Vector3.new(0, 1, 0))
local leftVector = -rightVector -- oposite of right (left)
2 Likes

Here’s what I came up with:

Tree Range Test (45.8 KB)

And here is the code that makes this work:

local RunService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local tree = workspace:WaitForChild("Tree")
local hitbox = tree:WaitForChild("Hitbox")

local xzPlane = Vector3.new(1, 0, 1)

RunService.Heartbeat:Connect(function()
	local char = player.Character
	
	hitbox.Min.Visible = false
	hitbox.Max.Visible = false
	
	if char then
		local pivot = char:GetPivot()
		local hitPosition = hitbox.Position
		local dist = (pivot.Position - hitPosition).Magnitude
		
		if dist < 10 then
			local look = CFrame.lookAt(pivot.Position * xzPlane, hitPosition * xzPlane).Rotation + pivot.Position
			local minRange = look * CFrame.new(-5, 0, -dist)
			local maxRange = look * CFrame.new(5, 0, -dist)
			
			hitbox.Min.WorldCFrame = minRange
			hitbox.Max.WorldCFrame = maxRange
			hitbox.Min.Visible = true
			hitbox.Max.Visible = true
		end
	end
end)

The main CFrame magic:

local xzPlane = Vector3.new(1, 0, 1)

local pivot = char:GetPivot() -- The player's position
local hitPosition = hitbox.Position -- The tree position
local dist = (pivot.Position - hitPosition).Magnitude -- The distance between the player and the tree

local look = CFrame.lookAt(pivot.Position * xzPlane, hitPosition * xzPlane).Rotation + pivot.Position -- A CFrame looking at the tree from the player on the XZ plane
local minRange = look * CFrame.new(-5, 0, -dist) --< FINAL LEFT RANGE
local maxRange = look * CFrame.new(5, 0, -dist) --< FINAL RIGHT RANGE

Is this what you were looking for?

3 Likes

Thank you this is exactly what I was looking for.

Thank you for the concepts and ideas too, I will look deeper into this and see what I can learn.