- Home /
 
               Question by 
               adit1 · Feb 13, 2013 at 04:15 AM · 
                camera2dplatformer  
              
 
              2d camera movement
I want to have a threshold where the camera wont move until the object being tracked goes beyond a certain value. My pseudo code looks like this:
 var thresholdX = 5;
 var thresholdY = 5;
 function Update(){
 if(target.position.x > thresholdX){
 //do the lerp transition
 transform.position.x = Mathf.Lerp(transform.position.x, target.transform.position.x + 20, Time.deltaTime);
 }
The problem is it's only applying the transition in one frame. Is there any other way I could achieve this? Something like path based camera.
               Comment
              
 
               
              Answer by Benproductions1 · Feb 13, 2013 at 08:41 AM
Instead of allways lerping to a certain amount, why not just clamp the camera? It might be a bit jolty, but you can allways apply smoothing form there.
 //Instead of Lerp, use clamp
 //This is just for moving along the X axis, similarly it would have to be done for other axis
 transform.position.x = Mathf.Clamp(transform.position.x, target.position.x + thresholdX, target.position - thresholdX);
Hope this helps, Benproductions1
But that's not the problem for me, the logic itself is flawed.
Sorry, my code was incorrect, but it should do exactly what you described in the firsts paragraph
Just put the two together :
     //do the lerp transition
     transform.position.x = $$anonymous$$athf.Lerp(transform.position.x, target.transform.position.x + 20, Time.deltaTime);
     // clamp the x to between preset values
     transform.position.x = $$anonymous$$athf.Clamp(transform.position.x, target.position.x + thresholdX, target.position - thresholdX);
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                