Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Mouse.Hit targeting relative to character?

Good day! I'm currently working on an isometric camera game, where your character turns towards your mouse. The issue is, only method I found to do this was by using TargetFiltering to make the mouse react only to certain parts. So while you're on the level of ground, everything is okay, but going to a buildings second floor, character turning becomes a huge issue like this: https://i.gyazo.com/abd34ff724dfd1ea915519893bff3fc0.mp4

I'd need the mouse to find X and Z axels relative to Y axel of characters root part. Any ideas how to do this? Here's how I made it work so far:

local player = game.Players.LocalPlayer
local character = player.Character
local gyro = nil
local mouse = player:GetMouse()
mouse.TargetFilter = game.Workspace.Environment
local runService = game:GetService('RunService')
 
local function onCharacterAdded(character)
	local torso = character:WaitForChild('HumanoidRootPart')
	gyro = Instance.new('BodyGyro', torso)
	gyro.P = 500000
	gyro.MaxTorque = Vector3.new(0, 500, 0)
 
	local humanoid = character:WaitForChild('Humanoid')
	humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
	humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, true)
	humanoid.AutoRotate = false
end

local function isnan(x) return x ~= x end
 
local function onRenderStep()
	if gyro then
		--local mouseHit = mouse.Hit.p
		local mouseHit = Vector3.new(mouse.Hit.x,character.HumanoidRootPart.Position.Y,mouse.Hit.z)
		print(mouseHit)
		if not (isnan(mouseHit.X) or isnan(character.HumanoidRootPart.Position.Y) or isnan(mouseHit.Z)) then
			gyro.CFrame = CFrame.new(player.Character.HumanoidRootPart.Position, mouseHit)
		end
	end
end
 
while not player.Character do wait() end
onCharacterAdded(player.Character)
player.CharacterAdded:connect(onCharacterAdded)
 
runService:BindToRenderStep('TrackMouse', Enum.RenderPriority.Input.Value, onRenderStep)

Thanks for reading!

1 answer

I'm guessing that you're having trouble with making sure the character doesn't go ballistic when the mouse points at an object higher than the player's current Y position. If so,

What if instead of calculating the Y value at all, you just use the player's HumanoidRootPart's Y position, and the mouse's X and Z Hit Positions?

11 comments

0

That was my first attempt trying to fix this, and didn't quite do anything. Mouse still falls through characters height and finds X and Z from the physical part it hits. Or is that what you mean?

0

Well the Z and X coordinates are unaffected by the height of an object, however the Y is. So if you control the Y value and set it to the Player's height, it shouldn't make a difference where the mouse points.

0

If you still have issues though you could try pasting the code you had before in that form and I can look at it?

0

The mouse reacts only to basepart, so being way above the basepart makes turning your character work relative to where your mouse is on the surface of the baseplate. https://gyazo.com/d0ab0b30dfb4d16721d666c8f03d83fe

0

Added the whole mouse controlling code to the thread, ya can now take a look at it. And thanks!

0

I can't remember if this affects animations or not, it probably does, but have you tried CFrames?

0

You mean replacing Vector3 at line 25 with CFrame? That only returns an error "Workspace.Gybron.CharacterControls:28: bad argument #2 to 'new' (Vector3 expected, got CFrame)"

0

The thing is that it rotates character to where your mouse hits a surface, so making the Y axel relative to characters torso does no change, since the mouse still takes X and Z from the part it hits below you.

0

Honestly not sure then, sorry I couldn't help.

0

No problem, many thanks anyway!

This discussion is preserved as it was. New answers, comments, and votes are closed.