Math.Clamp Help please!

Hi guys, i have a problem and trying to fix it since 2 days.

I have a space runner game which my player move forward, up, and down. However i want to make a limit with math.clamp for up and down movement.

Everything works fine but when i go up(or down) my player stops for a second (something happening like crashing an obstacle but its not a crash). I dont want it stop i just want that my player have limits but that limits shouldn’t effect my player positive or negative.

I don’t know if i did make a good explanation.

Thank you for your helps and love for everyone!

Hello!

Maybe it will be good to share the part of the relevant code and also a video to demonstrate the issue, so we will get a better understanding of your issue

1 Like
void Update()
    {
      
        float directionY = Input.GetAxisRaw("Vertical");
        float directionX = rb.velocity.x;
        forward = new Vector2(directionX,0).normalized;
        forward.x = playerSpeed;
        playerDirection = new Vector2(0,directionY).normalized;
        transform.position = new Vector3(transform.position.x, Mathf.Clamp( transform.position.y, -10, 10f), transform.position.z);

        PowerUpTimerOptions();
  
    }



    void FixedUpdate()
    {
        rb.velocity = new Vector2(forward.x,playerDirection.y * movingSpeed);
    }

This is my script for to controle and move my main character. When player try to cross that Math.Clamp limits, something happening like crashing wall and stops for a sec.

I don’t want my player cross the limits also i want my player goes on like nothing happening. I don’t know how to share video actually but i will learn now and i will send you an example of it.

Well, you’re not supposed to modify the transform manually if you’re using physics. You haven’t decided if you’re moving your object yourself or if physics is supposed to move it.

Either manipulate the object via collisions and forces, or set its position all by yourself, you can’t do both because the two systems constantly vie for control over position.

In other words, you either make a snappy platformer and only use collisions (to detect physical contact), but simulate the forces and gravity yourself, clamp the coordinates etc, or you let Unity resolve all physics-related stuff for you, no meddling with it – place the walls, set up your colliders properly and only regulate the forces affecting the body.

There are ways to mix the two approaches, but you need to know what you’re doing.

1 Like

Thank you for the knowladge you giving me bro !! but even if i use colliders to fix the problem, i still wondering why object stops when its trying to cross the limits of Clamp.

It probably gets stuck within the collider itself, or something like that, you probably glitch out the physics engine. Physics is more than you see, it needs to understand and keep track of the continuity of the motion (i.e. moment of inertia and so on), and when you teleport your objects around, it gets lost in its computation, because something completely unholy has just happened. There are ways to work around this, for example you can turn kinematic mode on, or you can even turn off physics, or configure its algorithms differently, but normally if you constantly tell physics how to do its job, it wasn’t designed to handle this and is likely to start glitching.