Positioning the mining cart according to the player's position and orientation

Hey!

In connection with a new small project, it would be necessary to create a mining cart that I can push. I decided that the appropriate way to do this is to have the cart welded to the player when the player grabs it.

The problem is that before welding the cart to the player, the location and orientation of the cart model would need to be set based on the location and orientation of the player.
For example, that the cart is 1 stud ahead of the player and the orientation is parallel to the player.

I haven’t really worked with CFrame before, and I lack a lot of knowledge about it. A search for a similar problem does not return any answers.
At the moment, I have a script that moves the model, but not in front of the player, but to a completely random place on the map, not to mention its orientation.

So the help I’m asking for is: How to set the position and orientation of this model with the player’s HumanoidRootPart (A little ahead of the player)
I’ve been trying for a long time now and I’m starting to lose hope lol.

Thanks!

(PrimaryPart: CartArea)

local Cart = script.Parent.Parent.Parent
local CartArea = script.Parent.Parent.Parent.CartArea
script.Parent.Triggered:Connect(function(plr)
	if not CartArea:FindFirstChild("ConnectionWeld") then
		local CharacterCFrame = plr.Character.HumanoidRootPart.CFrame
		Cart:PivotTo(CFrame.new(CharacterCFrame*CharacterCFrame.LookVector*2))
	--CreateWeld
	local WeldC = Instance.new("WeldConstraint")
	WeldC.Name = "ConnectionWeld"
	WeldC.Parent = CartArea
	WeldC.Part0 = CartArea
	WeldC.Part1 = plr.Character.HumanoidRootPart
	end
end)

Workspace:
Pic3

Visual desired result:

Cframes are certainly one of the harder things to pick up on, but are honestly game changing once you do understand them. You are actually on the right track in your own code with:

Cart:PivotTo(CFrame.new(CharacterCFrame*CharacterCFrame.LookVector*2))

You simply need to rewrite it to this:

Cart:PivotTo(CharacterCFrame+CharacterCFrame.LookVector*2)

Excellent! It works just as it should.
But can you also tell me how I can move it down a bit, as it is floating at the moment?
image

You can simply expand out your expression to include the UpVector as well.

Cart:PivotTo((CharacterCFrame+CharacterCFrame.LookVector*2)+CharacterCFrame.UpVector*-1)

I seperated the additions with parantheses just incase the interpreter would add the 2 vectors first, as that would move the cart around the player semi randomly.

Works exactly as I wanted. Thanks!

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