Align position in X and Z axis, similar to BodyPosition

I want to keep my player’s X and Z position locked to a certain position but still keep gravity affecting them. From what I have read, BodyPositions can do this by assigning different amount of forces to each axis. However, BodyPositions is deprecated and I am not sure if it is worth using a deprecated class.

Right now, I am trying to use a raycast to calculate the distance from the floor to the orignal part that has the AlignPosition, but that ends up increasing the speed of gravity by an unrealistic amount if I still want to keep the player’s location locked.

Essentially, how can I use AlignPosition to lock a player’s position in the X and Z axis. I don’t mind using another constraint if it is necessary.

2 Likes

To move an attachment on only two axes you should use a plane constraint.

You can read more about it here:

2 Likes

Some solution I can think of is to reduce the following properties in the ss so the x and z are slightly locked in place. By slightly, I mean they will be pushed around if any other force acts upon the part. Or, you can increase the gravity in the game, though that will cause all parts to be affected by gravity.

image

1 Like

From what I have read, plane constraint in 1 axis, and provides a degree of movement in the other 2. I want to limit movement in 2 axis and provide a degree of movement in 1 axis (the Y axis)

I think I need to use a prismatic constraint? I am not 100% sure about this, nor how to set it up even after checking the documentation

1 Like

After a lot of work, I managed to create the system I wanted, however, it can be a little complicated and I am sure could be optimised if I knew more about the physics going in the back end.

First, I am using a raycast to shoot a ray from the humanoid root part directly down. I am using this to find the first part underneath the player to align the position.

Then I make a variable, which I will refer as yDirection, and set it to the Y position of the humanoid root part minus the distance of the raycast

Then I set the yDirection to the yDirection plus the size of the character divided by 2:

yDirection = yDirection + character:GetExtentsSize()/2

Then I create a AlignPosition with the following properties:

local pAlignPos = Instance.new("AlignPosition",character.HumanoidRootPart)
pAlignPos.Attachment0 = pAttachment
pAlignPos.ApplyAtCenterOfMass = false
pAlignPos.RigidityEnabled = false
pAlignPos.Mode = Enum.PositionAlignmentMode.OneAttachment
pAlignPos.Responsiveness = 5
pAlignPos.MaxForce = 500000 -- Could be made smaller, I am not too sure
pAlignPos.MaxVelocity = math.huge
pAlignPos.Position = Vector3.new(character.HumanoidRootPart.Position.X,yDirection,character.HumanoidRootPart.Position.Z)

Then I create a counter vector force with the following properties:

local pVectorForce = Instance.new("VectorForce",character.HumanoidRootPart)
pVectorForce.Attachment0 = pAttachment
pVectorForce.Force = Vector3.new(0,-500000,0)

With this, it creates the affect of aligning the position of the X and Z axis whilst still allowing gravity to affect the character

There is one problem being that the final yDirection is about 0.1 units off, I am not too sure how to fix this but if I figure out I will reply later

2 Likes

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