Hello folks. Today Im struggling with Distance Joint 2D Distance property. I don’t know how to stop by Physics distance getting bigger when my “head” visits ground. The way “Check if collides with ground” wont work because I want to work it by physics and if it will be in angle with ground it should slides on it since it is slippery. Distance control is done by simple Up/Down arrows keys and control speed.
Check the gif please and tell me please what can I try to prevent that behaviour.
The joint, like all other joints justr constrain. In this case the distance. It can only do that if you’re not bypassing physics by directly modifying position of the Transform or the Rigidbody2D. You don’t show how you’re moving stuff but make sure it’s via forces/velocity.
I don’t understand your other comment of “Check if collides with ground” and what it means.
All that is show is a component and some movement. The instant jumping is not caused by the joint, that’s your code doing that.
Yea sorry for not movement code.
Im probably doing it wrong, because I just modify the distance by input.
private void HandleVerticalMovement(float verticalMovement)
{
if (verticalMovement == 0f)
return;
float movementForce = verticalMovement < 0f ? GlobalSettings.VerticalDownMovementForce : GlobalSettings.VerticalUpMovementForce;
float currentAnchorY = headDistanceJoint.distance;
currentAnchorY += movementForce * Time.fixedDeltaTime * -verticalMovement;
headDistanceJoint.distance = currentAnchorY;
}
Also… The component looks like
https://postimg.cc/hfDQJmbR
So its now not physics based. How can I modify it by physics? I care about linear movement up/down.
What do you mean by this? Why is it not physics based?
You are free to change the distance, that has no effect on collisions which is what your question seemed to be about. I don’t need to see pictures of the joint, it won’t change collision detection. Joints are about constraints only.
The code you posted shows you changing the distance the joint should try to maintain. It doesn’t show how you’re moving anything as I mentioned above.
I meant I affect only distance property and its not affect colliders as u mentioned and I know its wrong.
Okay, if u need more code:
//In FixedUpdate
private void RopeMovement()
{
Vector2 movementValue = movementInput.action.ReadValue<Vector2>();
HandleHorizontalMovement(movementValue.x);
HandleVerticalMovement(movementValue.y);
}
private void HandleVerticalMovement(float verticalMovement)
{
if (verticalMovement == 0f)
return;
float movementForce = verticalMovement < 0f ? GlobalSettings.VerticalDownMovementForce : GlobalSettings.VerticalUpMovementForce;
float currentAnchorY = headDistanceJoint.distance;
currentAnchorY += movementForce * Time.fixedDeltaTime * -verticalMovement;
headDistanceJoint.distance = currentAnchorY;
}
private void HandleHorizontalMovement(float horizontalMovement)
{
bool leftSide = false; //for a while, if it still stands dont change it :)
Vector2 right = leftSide ? -headRb.transform.right : headRb.transform.right; //its reversed, because the head is downwards
headRb.AddForce(GlobalSettings.HorizontalMovementForce * horizontalMovement * Time.fixedDeltaTime * right);
}
Its all about movement. How to change collision detection?
It’s not wrong, you are free to do that and as I said, it won’t affect collision detection. Nothing you’ve shown affects collision detection.
Note that you should not scale forces by the elapsed time (fixedDeltaTime), the physics system will do that later when it integrates it into velocity; that’s what a force is. Again, this won’t affect collision detection.
The video isn’t clear to me what it’s showing either, it just jumps to another position, no idea why.
It shows that if I goes down with rope (distance) it doesnt check the colliders. When I hit the ground distance still grows and exactly we don’t want that
Thanks for note, it works only for side movement while we care about top down (distance) movement because it still grows when I go down (distance grows)
The GIF does not clearly show what’s going wrong! As I’ve tried to tell you, it looks like it’s hitting the collider then (presumably) you move it and (presumably) change the joint distance then it instantly jumps to another platform and (presumably) stops because it hits another platform.
Lots of presumptions because I don’t know what is actually happening clearly.
So please answer this; if you only increase the joint distance, does it pass through the first platform shown in the GIF? As you saying the “thing” on the end of the top never hits any ground colliders?
Ok, the thing is when I increase the joint distance (by input) it doesnt recognize I hitted a ground, it still grows linearly.
It grows, grows, still grows and it doesnt care that my head hitted a ground.
It pass through the platform because physics gets lost, while distance is still growing. Distance went too big, which should not when I hit the ground. It should slippery on the ground.
Like distance should adjust to distance beetween head and hinge?, but I dont know how.
We’re going around in circles a little here. That’s not how joints work, they don’t stop you hitting objects like that.
Can you please answer my question above?
If I increase distance only, the head (the object which moves on gif) dont pass through the platform.
And I know it doesnt work like that. And thats why I am here because I dont know what to do it in way it stop
Right, that make sense.
Sorry, I think there’s a language barrier here. What doesn’t work like that? How to stop what?
Maybe you’d be best to upload a simple reproduction case I can look at if possible as this isn’t going anywhere useful to you. If you’d like to do that privately then please DM me. If you need a private workspace to upload it to then DM me with your email and I’ll set one up for you.
Probably Im really bad in explaination, sorry
Let me try to upload another better gif at first, I will try to show it better. I need to be in home to do that so expect it later