Planet Gravity For Player

I am currently working on a space game with planets, so I made my own character controller, gravity and camera controller. The code is probably not the best but it works fine (I think) and it only works on keyboard right now but I think I’ll do a controller support.

Important :

  • Workspace gravity has to be set to 0
  • The character has to use Jump Power and set it to 0 (But I think it will work fine if you don’t)
  • The Folder “KeyCodeOptions” contains the Key Code for all the movement
  • The Player Camera has to be set to Classic (or it’s a REAL mess)
  • All the option to define the gravity are childrens to “PlayerControl”

I think that’s all, if you have any questions or any bug please let me know I’ll try to answer you (there’s a chance I just forgot to)

Gravity.rbxl (1.8 MB)

2 Likes

You haven’t actually specified what you need help with or what problems you’ve encountered and need support solving. You can’t just provide a file and expect people to know what you’ve tried and what you actually need them to fix.

Maybe edit your post to be more specific.

He’s asking for feedback on his creation. That is the category he put it in, afterall.

1 Like

first of all: i really want to applaud you for what youve done! youve created something pretty good!

now just from my basic first impressions of the code, here are some minor points

  1. i think the value instances should be replaced with attributes
  2. i think you should you should space out the code a bit more
    example: instead of
	local Forward,Backward,Left,Right,Jump = GetInput.GetInput()
	local direction,gravity
	for _,k in ipairs(bodyPart) do
		local Part = char:WaitForChild(k)
		Part.CanCollide = true
		Part.CustomPhysicalProperties = PhysicalProperties.new(0.7,.1,0,1,100)
	end
	if seated==false then
		Human.PlatformStand = true
		if enabled.Value == true then
			direction = (GravityPoint.Value-HRP.CFrame.Position).Unit
			gravity = magnitude.Value * direction
			GravityForce.Force = gravity
		else
			GravityPoint.Value = HRP.CFrame.Position-(HRP.CFrame.UpVector)*10
			direction = (GravityPoint.Value-HRP.CFrame.Position).Unit
			GravityForce.Force = Vector3.new(0,0,0)
		end
	else
		direction = (GravityPoint.Value-HRP.CFrame.Position).Unit
		Human.JumpPower = 0
		Anim.StopAnimation()
		if Jump==true then
			Human.Sit = false
		end
	end

you should do

	local Forward,Backward,Left,Right,Jump = GetInput.GetInput()
	local direction,gravity

	for _,k in ipairs(bodyPart) do
		local Part = char:WaitForChild(k)
		Part.CanCollide = true
		Part.CustomPhysicalProperties = PhysicalProperties.new(0.7,.1,0,1,100)
	end

	if seated == false then
		Human.PlatformStand = true

		if enabled.Value == true then
			direction = (GravityPoint.Value-HRP.CFrame.Position).Unit
			gravity = magnitude.Value * direction
			GravityForce.Force = gravity
		else
			GravityPoint.Value = HRP.CFrame.Position-(HRP.CFrame.UpVector)*10
			direction = (GravityPoint.Value-HRP.CFrame.Position).Unit
			GravityForce.Force = Vector3.new(0,0,0)
		end
	else
		direction = (GravityPoint.Value-HRP.CFrame.Position).Unit
		Human.JumpPower = 0
		Anim.StopAnimation()

		if Jump == true then
			Human.Sit = false
		end
	end

that way the code is split up into intuitive blocks

  1. i think you should get a more clever solution for GetInput (but you dont have to! it works just fine and is very readable the way it is, it just looks a bit scuffed hahahaha)
1 Like

Yeah that’s what I meant by “not the best but it works fine”, most of the time I code something that works but is a little bit janky and not easy to read :sweat_smile:

1 Like

And thanks for the advices I’ll try to apply them whenever I have time !