How to make my character able to turn corners?

Hello,
I dont know how to name the title other than this.
What is what im trying to achieve?
Well what i want to achieve is that the player will turn around the corner and able to prevent clipping.

Short clips of my problem

https://gyazo.com/53e0521a2d037b916cc6585d86811730 – going out of bounds
https://gyazo.com/4f00c35913ce402ea8701eb3cc155179 – clipping through walls

Im not asking for a full script as thats against the rules, i want to know how i can achieve such

Scripts and what i have done so far

The script that handles going from left to right, is a local script and looks like this:

local function Right(_, state)
	if state == Enum.UserInputState.Begin then
		if canMove() then
			if not hD then
				hD = true
				while hD == true do
					hrp.CFrame *= CFrame.new(leftRightIncrements, 0, 0)
					canManoeuvre(OriginPart.CFrame.RightVector)
					task.wait()
				end
			end
		end
	elseif state == Enum.UserInputState.End then
		hD = false
	end
end

The canManoeuvre() function is what i believe a failed attempt on trying to detect a corner, it looks like this:

local function canManoeuvre(dir)
	local origin = OriginPart.Position
	local params2 = RaycastParams.new()
	params2.FilterDescendantsInstances = {char, OriginPart}
	local Dir = dir*9
	local ray = workspace:Spherecast(origin, 10, Dir, params2)
	visual:SphereCast(origin, 3, Dir, params2)
	if ray then
		local hitPart = ray.Instance
		if hitPart then
			visualPartPlsHelp.Position = ray.Position
		end
	end
end
Couple things about the scripts and the meaning of the things inside of them

visualPartPlsHelp is the visualization of my failed canmanouevre() function

hD is short for holding D basically a boolean for if the player is holding the D key

OriginPart is a part in the middle of the character that acts like a humanoid root part but is like another anchor point

Whats wrong or how do i make the dude turn corners? Is there some mathematical formula or such?
I would like to hear back.
Thank you for reading.

Are you trying to make a ‘click to move’ thing? If you want the player to go around obstacles you’re gonna have to research pathfinding. Character Pathfinding | Documentation - Roblox Creator Hub

No.
Im trying to make a climbing like system and doing so i thought it was a good idea to make it using raycasting and sphere casting. The player moves by using WASD as main moving keys.

W makes the player move up
A makes the player move to the left
S makes the player move down
D makes the player move to the right

So you’re trying to make the player go around the obstacle when climbing? Or what are you trying to achieve?

Or are you trying to make it so that the player collide with the obstacle?

I want it so when the player is near a ledge, they will move around it and not go out of bounds as seen in the first dropdown in the main topic thingy.

I thought of sphere casting and seeing if it detects a ledge and rotating the humanoid root part with 90 degrees based on which ledge.

So if the player was first moving to the left then we turn the hrp with 90 else -90

Oh, I see. Well first of all you need to break down the problem into separate parts:

  1. Detect the obstacle - Cast a ray in the direction where the player is heading
  2. Rotate the RootPart as you wish
  3. Find the path around it (you could use simple pathfinding based on trial and error)
  4. Move the player according to the path, you can play an animation if you want
  5. Reset the orientation of the RootPart to what it was before (optional)

As for ledges, cast a ray pedpendicular to the wall (facing the wall) then check when that ray stops colliding. When it stops, it means that the player has reached a corner. Turn the player 90 degrees, and that should be about it.

Yes i understand, it was my first thing to go too.

But what type of raycasting should i use?
We have: Raycasting, shapecasting, blockcasting and spherecasting.
My original go to was to use spherecasting as it was round and i looked betted to me. But i have no idea as to how.

How would i do this. This was my original question, sorry for not asking it like this.

1 Like

Well with raycasting you know exactly where you’re pointing to, therefore you can check if there is an obstacle, in front, behind, on top, or below the player in a much more simple way. I’ve never used spherecasting, however I believe that it’s much simpler to use separate rays.

Well, firstly just cast a ray in the direction that the player is facing, if they are already facing the wall. Then keep casting that ray, until it stops intersecting the wall. When it does stop intersecting, that means that the player has hit a ledge, and you need to turn the player 90 degrees, then optionally move the player a little more towards the wall so theyre not sitting exactly at the corner.

Do you need example code?

Example code will be very appreciated, im too frustrated to think

After forcing myself to think:

local function canManoeuvre(dir)
	local origin = OriginPart.Position
	local params2 = RaycastParams.new()
	params2.FilterDescendantsInstances = {char, OriginPart}
	local Dir = OriginPart.CFrame.LookVector * 5
	local ray = workspace:Raycast(origin, Dir, params2)
	visual:Raycast(origin, Dir, params2)
	if ray then
		local hitPart = ray.Instance
		if hitPart then
			visualPartPlsHelp.Position = ray.Position
		end
	else
		--hrp.CFrame *= CFrame.Angles(0, math.rad(-90), 0) * CFrame.new(3, 0, 0)
		hrp.CFrame = visualPartPlsHelp.CFrame * CFrame.Angles(0, math.rad(-90), 0)
	end
end

This my new function and this works better, i will further enhance it tomorrow, good luck finding something out of this topic for people in the future.

1 Like

To cast a ray in the direction the player is facing, you do the following:

local character = player.Character // or something else if youre doing it server side
local rayOrigin = character.HumanoidRootPart.Position
local rayDirection = character.HumanoidRootPart.CFrame.LookVector

local length = 5 // in studs, change to your desired length
local raycastResult = workspace:Raycast(rayOrigin, rayDirection*length)

then check if there is a raycast result:

if raycastResult then
    // there was an intersection

    // turn player 90 degrees
    // you might need to put an if here to change whether player rotates clockwise or counterclockwise
    local newCFrame = character.HumanoidRootPart.CFrame * CFrame.fromEulerAnglesXYZ(0, math.rad(90), 0)

    character.CFrame = newCFrame
end

This is very simplified by the way, sorry I haven’t coded in roblox in a couple months

edit: man i just spent like 5 minutes writing this

local ray = workspace:Raycast(origin, Dir, params2)
visual:Raycast(origin, Dir, params2)

You don’t need to do that second line. The declaration on the first line already fires the raycast.

Also for the love of me please set some better variable names this is unreadable

My variable names are fine, they all have a meaning to me.
hrp is short for Humanoid root part.
So on.

sorry for wasting ur time.

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