Need to increase a var up to a point and stop

I want to first of all say this place is awesome, iv always found answers i wanted but i think i ran out of luck this time and i have to be specific with my question. I am creating an infinite run game. I want my characters speed to increase with time spent playing. I am currently multiplying by a variable i increment in a function. My problem is how do i make it stop at a certain speed. This is because i discovered that at a certain speed, the ontrigger function i use in loading my scenes in front of character fails. So i decided to stop it at a particular speed. i tried using the “if” statement but it doesnt seem to wrk for me. Here is my code

var speed : float = 3.0;
    static var viewincrease:int;
    static var increase:int=1;
    static var totalIncrease:int=1;
    private var jumpSpeed : float = 50.0;
    private var gravity : float = 50.0;  
    private var moveDirection : Vector3 = Vector3.zero;
    
   
    
   function increaseSpeed(){
    
    increase +=50;
    
    
    }  
      InvokeRepeating("increaseSpeed",3,3);
    function Update() {
    
   
        var controller : CharacterController = GetComponent(CharacterController);
        if (controller.isGrounded) {
            // We are grounded, so recalculate
            // move direction directly from axes
                
            
            Debug.Log(totalIncrease);
            totalIncrease = -120 - increase;
            moveDirection = Vector3(Input.GetAxis("Horizontal")* speed , 0, totalIncrease );
            if (totalIncrease>=300){
           		 totalIncrease=300;    
   
   				}
       
           //  Debug.Log(viewincrease);   
            if (Input.GetButton ("Jump")) {
                moveDirection.y = jumpSpeed;
            }
        }
        // Apply gravity
        moveDirection.y -= gravity * Time.deltaTime;
        
        // Move the controller
        controller.Move(moveDirection * Time.deltaTime);
    }

Please forgive my rough code writing, im kinda new to programming. And forget about my minus sign also. I know why its like that. I just need help figuring this out. I will be immensely greatful. Or if someone can suggest a better way for me to instantiate scenes.i tried time, but the whole delta time/frame rate thing made it fail constantly. Thanks alot

Why not use a rigid body and have that accelerate instead of this ?. There are many things wrong with the code, that +50 increase in speed is too much! you should increase it gradually. I’ll try to address your problem of increasing speed up to a point(300) as I understand it.

I’ve modified your script to make it work, but you should consider a better approach…

 private var speed : float = 3.0;
 static var viewincrease:int;
 private var jumpSpeed : float = 50.0;
 private var gravity : float = 50.0;  
 private var moveDirection : Vector3 = Vector3.zero;
     
     
    function increaseSpeed()
    {

     if(speed<=300)
     {
     speed +=50;
     }

     }  
     
     InvokeRepeating("increaseSpeed",3,3);
       
     function Update() 
     {
     
         var controller : CharacterController = GetComponent(CharacterController);
         if (controller.isGrounded)
          {
             // We are grounded, so recalculate
             // move direction directly from axes
                 
             
             moveDirection = Vector3(Input.GetAxis("Horizontal")* speed , 0, 0 );
            Debug.Log(speed);
        
            //  Debug.Log(viewincrease);   
             if (Input.GetButton ("Jump")) {
                 moveDirection.y = jumpSpeed;
             }
         }
         // Apply gravity
       //  moveDirection.y -= gravity * Time.deltaTime;
         
         // Move the controller
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);
     }

The easiest way to limit the maximum value of something is to use Mathf.Max(). Check it out in the manual/scripting guide.

A better way to achieve what you’re trying to do there is to have your moveDirection multiplied by some value, which you can then limit using the Mathf.Max() call.

Well if I understand right, problem with your if is that your setting totalIncrease from the start in every update, but limit it after you use it. Try moving totalIncrease before setting moveDirection.

Also you could try to stop changing increase in increaseSpeed with something like:

function increaseSpeed()
{
     if(increase>=maxIncrease)
     {
       CancelInvoke("increaseSpeed");
       return;
     }
     increase +=50;         
 }

Thanks guys, but None of the solutions are working. If there was a way to improve the efficacy of my spurn script from OntriggerEnter so it does not fail at speeds more than 300 would be nice. Thanks

You are the best, issue solved. You all were right. so was my previous code apart from the negative value.