- Home /
2d beat em up clamp player when leaving another player
I have been all over the place and its like no one has an swer for this. but im trying to make a beatem up and im trying to make a part where if the player is leaving another player behind (if they go in opposite directions or one isnt moving) the player cant leave or move the camera i thried clamping but it just gets janky (children rig is the player and group x is the virtual cam)
public void Areana(Transform player)
{
childrenRig = player.GetComponentInChildren<Rigidbody>();
float childrenRigx = childrenRig.transform.position.x;
float groupX = virtualCam.transform.position.x;
childrenRig.position = new Vector3 (
Mathf.Clamp (childrenRigx, groupX -5.436225f, 0.460665f - (groupX) ),
childrenRig.position.y,
childrenRig.position.z);
i tried to clamp it around the players current location and the min and max is bassed off the changing x value of the virtual camera. it just gets all wrong is there a better way im bout to just give up and us box colliders as borders.
Answer by MarekRimal · Nov 05, 2021 at 02:37 PM
Besides the really strange naming I guess the approach make sense. The Clamp looks suspicious though. I dont really know what are those magic numbers but it seems to me that with increasing groupX value the min value gets quickly larger then the max value. If the game is side scroller then the groupX value will increase and so it breaks.
Example:
groupX = 0, => min = -5.4, max = 0.4 (min < max)
groupX = 10, => min = 6.6, max = -9.6 (min > max)
Your answer
![](https://koobas.hobune.stream/wayback/20220613044952im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D GunPivot Bug 1 Answer
2D sidescroller shooting 1 Answer
Create 2D box colliders based on current frame of animation 0 Answers
Problem with Mathf.Clamp and Mouse Follow Script 1 Answer
2D Animation does not start 1 Answer