Model Vector that responds to rotation (Video example)

What’s the part/vector that responds to the player/model rotation? or how can I access it within a script? I though it was HumanoidRootPart but it has just a fixed orientation

2 Likes

You’re sure it’s not the RootPart?

Yes, I spawn mobs based on the forward vector orientation (HumanoidRootPart.Position.Z) in which I expected to spawn them in front of me every time, though when I face different direction they always spawn in the same position, instead of spawning in front of me

Could you send the script you’re using?

You can change the HumanoidRootPart orientation

local distanceOfSpawn = 20
	local character: Model = player.Character or player.CharacterAdded:Wait()
	local boar = game.ReplicatedStorage.Boar

	for i = 1,5,1 do
		local xRand = math.random(-distanceOfSpawn, distanceOfSpawn)
		local zRand = math.random(5, distanceOfSpawn)
		local posVector = Vector3.new(character.HumanoidRootPart.Position.X + xRand, character.HumanoidRootPart.Position.Y, character.HumanoidRootPart.Position.Z + zRand)
		local lookAtVector = character.HumanoidRootPart.Position
		local spawnLocation = CFrame.new(posVector, lookAtVector)
		local mob = boar:Clone()
		mob.Parent = workspace
		mob:PivotTo(spawnLocation)
		print("Spawning")	
	end
	print("Spawned")

Use HumanoidRootPart.CFrame.LookVector or something like that instead of HumanoidRootPart.Position

Is it supposed to look st the player or look away from the player? If the last then you should use -character.HumanoidRootPart.CFrame.LookVector

I tried this:

local distanceOfSpawn = 20
	local character: Model = player.Character or player.CharacterAdded:Wait()
	local boar = game.ReplicatedStorage.Boar

	for i = 1,5,1 do
		local xRand = math.random(-distanceOfSpawn, distanceOfSpawn)
		local zRand = math.random(5, distanceOfSpawn)
		local posVector = Vector3.new(character.HumanoidRootPart.Position.X + xRand, character.HumanoidRootPart.Position.Y, character.HumanoidRootPart.CFrame.LookVector + Vector3.new(0, 0, zRand))
		local lookAtVector = character.HumanoidRootPart.Position
		local spawnLocation = CFrame.new(posVector, lookAtVector)
		local mob = boar:Clone()
		mob.Parent = workspace
		mob:PivotTo(spawnLocation)
		print("Spawning")	
	end
	print("Spawned")

And the mobs still spawned in the same wrong position but this time farther away

First of all, you don’t have to index HumanoidRootPart position each time. It could create unexpected position mismatches when players move. Save it into a variable at the start of the loop like that

local HRPPos = character.HumanoidRootPart.Position

and instead of indexing the position over and over again each time by calling

character.HumanoidRootPart.Position

just use the variable in the each part of the code where you want the HRP’s position.

Did this:

local distanceOfSpawn = 20
	local character: Model = player.Character or player.CharacterAdded:Wait()
	local boar = game.ReplicatedStorage.Boar
	local root = character.HumanoidRootPart.Position

	for i = 1,5,1 do
		local xRand = math.random(-distanceOfSpawn, distanceOfSpawn)
		local zRand = math.random(5, distanceOfSpawn)
		local posVector = Vector3.new(root.X + xRand, root.Y, root.Z + zRand)
		local lookAtVector = character.HumanoidRootPart.Position
		local spawnLocation = CFrame.new(posVector, lookAtVector)
		local mob = boar:Clone()
		mob.Parent = workspace
		mob:PivotTo(spawnLocation)
		print("Spawning")	
	end
	print("Spawned")

nothing changed

No thats not what I meant, you have to change:
local lookAtVector = character.HumanoidRootPart.Position

To:
local lookAtVector = character.HumanoidRootPart.CFrame.LookVector

I think I found it, I need to actively fetch the player position, instead of just at the beginning of the script

Well, local lookAtVector is just a variable that controls the mob orientation not position so it won’t work

Could you clarify what exactly your script is meant to do? I am pretty sure the problem here is that you’re not converting your vector from Object space into World space.

To convert a Vector3 describing movements in object space, so movements relative to your parts rotation, into a Vector3 that describes your parts movement in global space (The parts .Position attribute is a global position for example) you can use the :VectorToWorldSpace() method.

My script is meant to spawn boars in front of the character and shifted either to the right or to the left of the player, to demonstrate:

Whenever I change the direction of entrance into zone, the boars should still spawn in front of me, the script I provided earlier is the event that fires whenever I enter the zone

I read about it and found this:


So I used CFrame.LookVector, and now all boars spawn at the origin of the map no matter where im looking

In that case you’ll want to change your script to apply your random offset on the x and z axis relative to your characters rotation. You can achieve this by using the :VectorToWorldSpace() method on your HumanoidRootParts CFrame.

local distanceOfSpawn = 20
	local character: Model = player.Character or player.CharacterAdded:Wait()
	local boar = game.ReplicatedStorage.Boar
	local root = character.HumanoidRootPart.Position

	for i = 1,5,1 do
		local xRand = math.random(-distanceOfSpawn, distanceOfSpawn)
		local zRand = math.random(5, distanceOfSpawn)
		local posVector = root + character.HumanoidRootPart.CFrame:VectorToWorldSpace(
            Vector3.new(xRand, 0, zRand)  
        )
		local lookAtVector = character.HumanoidRootPart.Position
		local spawnLocation = CFrame.new(posVector, lookAtVector)
		local mob = boar:Clone()
		mob.Parent = workspace
		mob:PivotTo(spawnLocation)
		print("Spawning")	
	end
	print("Spawned")

Using VectorToWorldSpace on your HumanoidRootParts CFrame turns your “local” vector describing the offset relative to your HumanoidRootParts x and z axis into a “global” vector describing that exact offset relative to your workspaces x and z axis, which you can then use to set your boars position in your workspace.

1 Like

Oh that explains it all, I didn’t understand :VectorToWorldSpace much at the beginning, this actually worked, though I also had to change “local posVector” to this:

local posVector = root + character.HumanoidRootPart.CFrame:VectorToWorldSpace(
			Vector3.new(xRand, 0, -zRand)  
		)

Thank you!

1 Like