Quantcast
Channel: Questions in topic: "javascipt"
Viewing all articles
Browse latest Browse all 1875

Is there a way to smoothly change a float number between two numbers when a key is pressed?

$
0
0
Building a 2D platformer. Making the camera follow the player but provide more room in front of the player's character so that there is less screen space behind them. Important details to know are that the camera can't be a child of the player, this causes stuttering for some reason. I have two scripts. The PlayerScript.js has a static variable called "facingRight" so that other scripts in my scene can see if the player is facing right or not. Left and right keys make the player change directions. I need a way to make the "camerasCurrentXTranform" variable change from where it is to the value "camerasNextTransform" is set to. An example would be to have the camera's X transform start at 2 and when then left key is hit it slowly changes to -2 and if the player faces right again before the camera reaches -2 to start from the camera's current X transform to slowly change back to 2. Code without notes to look cleaner and shorter: #pragma strict var target : Transform; var camerasCurrentXTransform : float; var camerasNextTransform : float; camerasCurrentXTransform = 2; function LateUpdate () { transform.position.x = target.position.x + camerasCurrentXTransform; } function FixedUpdate () { if(PlayerScript.facingRight == true && camerasCurrentXTransform != 2){ camerasCurrentXTransform += 2 * Time.deltaTime; } if(PlayerScript.facingRight == false && camerasCurrentXTransform == 2){ camerasCurrentXTransform -= 2 * Time.deltaTime; } } The code I created so far with notes: #pragma strict var target : Transform; //Transform of the player for the camera to follow. For some reason making a camera child of the player caused problems. var camerasCurrentXTransform : float; // The X transform of the camera. var camerasNextTransform : float; // The X transform the camera needs to change into. camerasCurrentXTransform = 2; // This helps setup the camera when the level/scene starts. (Player facing right makes camera start with more space on the right side.) function LateUpdate () { transform.position.x = target.position.x + camerasCurrentXTransform; //cameras X transform to equal the players position plus the cameras transform number which is either +2 or -2. } function FixedUpdate () { if(PlayerScript.facingRight == true && camerasCurrentXTransform != 2){ camerasCurrentXTransform +=2 * Time.deltaTime; //NOT WORKING. Needs to smoothly move/shift/position camera over to the left and right side of the player if the player is facing left or right. } if(PlayerScript.facingRight == false && camerasCurrentXTransform == 2){ camerasCurrentXTransform -=2 * Time.deltaTime; ///NOT WORKING. Needs to smoothly move/shift/position camera over to the left and right side of the player if the player is facing left or right. } } I will work on it in the mean time and post back if I find a solution. Looking at documentation I can see that time.time is not what is needed but Mathf.Lerp and time.deltaTime seems to be the 2 tools I need to use to get this to work.

Viewing all articles
Browse latest Browse all 1875

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>