Physics Controller VectorVelocity problem

I am currently making a custom character controller that has its own physics

I am running into a problem with my controller and my velocity is now moving in a direction I don’t expect it go.
curvemovement
I was only holding W and looking forwards but it moved to the left aswell

This doesn’t happen if my maxspeed is very high or I have no maxspeed

I clamped my maxspeed with this:

move = Vector3.new(
	math.clamp(move.X, -max_speed, max_speed),
	0,
	math.clamp(move.Z, -max_speed, max_speed)
)

I would really appreciate it if someone could show me a better way to set my max speed because it is really troubling me

Try removing Move.Z/X and see if that works

I need to keep those values so i can set my max speed, and to do this

move = move:Lerp(velocity_xz + move , move_alpha)

I’m probably not gonna understand anything but…
Could you show us the whole script?

Sure,

function Physics_Process(delta)
	if self_.camera and self_.player and self_.focused then
		local move_vel = get_movevector() -- PlayerModule:GetControls():GetMoveVector()
		local velocity_xz, velocity_y = Vector3.new(move_vel.X, 0, move_vel.Z), Vector3.new(0, 0, 0)

		if not On_Floor() then
			self_.velocity += Vector3.new(0, -(MoverController.pl.pl_gravity * delta), 0)
		else
			self_.velocity += Vector3.new(0, -self_.velocity.Y, 0)

			if UserInputService:IsKeyDown(Enum.KeyCode.Space) then
				self_.velocity += Vector3.new(0, MoverController.pl.pl_jump_height, 0)
			end
		end

		local move, jump = Vector3.new(self_.velocity.X, 0, self_.velocity.Z), Vector3.new(0, self_.velocity.Y, 0)
		local move_alpha, jump_alpha = 1, 0.5
		local move_goal = nil
		local max_speed = 250

		if not UserInputService:IsKeyDown(Enum.KeyCode.W) and not UserInputService:IsKeyDown(Enum.KeyCode.A) and not UserInputService:IsKeyDown(Enum.KeyCode.S) and not UserInputService:IsKeyDown(Enum.KeyCode.D) then
			move_vel = Vector3.zero
			velocity_xz = Vector3.zero
			move = Vector3.zero
		end

		if self_.grounded == true then
			max_speed = MoverController.limits.pl_max_speed
		else
			max_speed = MoverController.limits.pl_max_speed_air
			self_.groundedTime = 0
		end


		if not On_Floor() then
			self_.grounded = false
			if move_vel ~= Vector3.zero then
				move_alpha = MoverController.acceleration.pl_accelerate_air
			else
				move_alpha = MoverController.friction.pl_friction_air
			end

		else
			if not self_.grounded then
				Delay_Ground()
			end
			if move_vel ~= Vector3.zero then
				move_alpha = MoverController.acceleration.pl_accelerate
			else
				move_alpha = MoverController.friction.pl_friction
			end
		end
		
		move = Vector3.new( -- Problem begins here
			math.clamp(move.X, -max_speed, max_speed),
			0,
			math.clamp(move.Z, -max_speed, max_speed)
		)

		move = move:Lerp(velocity_xz + move , move_alpha)
		
		jump = jump:Lerp(jump, jump_alpha)

		self_.velocity = self_.velocity:Lerp((move + jump), move_alpha + .07)

		self_.velocity = Vector3.new(
			self_.velocity.X,
			self_.velocity.Y,
			self_.velocity.Z
		)

		self_.oldgrounded = self_.grounded

		Physics_Apply(max_speed, delta)
	end
end
1 Like

I don’t know what the problem may be, I’m sorry, I don’t understand even half of the script you’ve sent, but it may be because you are using math.clamp? I’m not sure if that’s the one but if I’m not wrong it makes it go below of one number? Not an expert sorry :sweat_smile:

The problem is the math clamp but I’m trying to figure out how to set max speed without this happening

Wait, it is math.clamp? So the problem is that you are trying to set maxVelocity without that happening, I understood only now :sweat_smile: have you tried using math.round or math.ceil not sure if it’s called that, try that or maybe even remove the math.clamp

Just a note

@DasKairo, we need you

Dude, can you stop? Its Getting very Annoying.

2 Likes

Is anyone able to help me solve this? I tried doing this and I still havent gotten a solution and it is really preventing me from finishing my controller:

move = move:Lerp(velocity_xz + move * max_speed, move_alpha)
-- or
move = move:Lerp(velocity_xz + move * max_speed * delta, move_alpha)

As I said, maybe try using math.round/floor/ceil, have you tried?

These won’t be able to solve my problem, I am trying to set a limit to how fast I can go without my character curving to the left or right. In the gif attached I’m only holding W and looking straight but I still move to the side.

I am sorry if this doesn’t make sense, I am also pretty confused why it does this…

Try clamping the magnitude instead of the components.

This was the function I used from Sleitnicks VectorUtil

local VectorUtil = {}

function VectorUtil.ClampMagnitude(vector, maxMagnitude)
	return (vector.Magnitude > maxMagnitude and (vector.Unit * maxMagnitude) or vector)
end

1 Like

Thank you so much for helping me with this, the problem has fixed now

1 Like

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