Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Math.Clamp Help please!

Discussion in 'Scripting' started by hakanmes, May 4, 2023.

  1. hakanmes

    hakanmes

    Joined:
    Jun 8, 2022
    Posts:
    21
    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!
     
  2. DavidTeo

    DavidTeo

    Unity Technologies

    Joined:
    Apr 1, 2021
    Posts:
    69
    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
     
    Bunny83 likes this.
  3. hakanmes

    hakanmes

    Joined:
    Jun 8, 2022
    Posts:
    21
    Code (CSharp):
    1.  
    2. void Update()
    3.     {
    4.      
    5.         float directionY = Input.GetAxisRaw("Vertical");
    6.         float directionX = rb.velocity.x;
    7.         forward = new Vector2(directionX,0).normalized;
    8.         forward.x = playerSpeed;
    9.         playerDirection = new Vector2(0,directionY).normalized;
    10.         transform.position = new Vector3(transform.position.x, Mathf.Clamp( transform.position.y, -10, 10f), transform.position.z);
    11.  
    12.         PowerUpTimerOptions();
    13.  
    14.     }
    15.  
    16.  
    17.  
    18.     void FixedUpdate()
    19.     {
    20.         rb.velocity = new Vector2(forward.x,playerDirection.y * movingSpeed);
    21.     }
    22.  
    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.
     
    Last edited: May 5, 2023
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    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.
     
    Kurt-Dekker likes this.
  5. hakanmes

    hakanmes

    Joined:
    Jun 8, 2022
    Posts:
    21

    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.
     
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    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.