Is there a way to smoothly change a float number between two numbers when a key is pressed?
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.
you need to change to c#. unity will dump "unityscript" quite soon (see their blog entries)
Thanks for the heads up. I started learning C# but I need to rewrite everything in C#. Plus I was told C# runs better. Something to do with Javascript/Unityscript "doing things in the background" from what I was told.
$$anonymous$$aking a GameObject a child of another automatically 'welds' the position with its parent. So you're moving it and Unity engine is. That's why it is stuttering.
Oh I see what you are saying. Before I didn't have a camera script following the player and just had the camera a child of the player ins$$anonymous$$d. I made a pixel perfect script that adjusts the camera for different resolutions because I bought a asset from the asset store that was supposed to do the same. Their script cause stuttering and so I placed it in the notes in case I forget. I could take out the camera script and make the Camera GameObject a child of the Player GameObject but then I would have to use Vector3.Lerp and I was having trouble using that one as well.
I thought changing a number slowly to adjust the camera would be easier from what is made already in my game project.
Answer by z7x9r0 · Dec 25, 2015 at 01:32 AM
Figured it out. Not sure if this is overkill but it does the job, and I guess I didn't have to use Lerp.
#pragma strict
var target : Transform;
var currentTransform : float;
currentTransform = 2;
function LateUpdate () {
transform.position.x = target.position.x + currentTransform;
}
function FixedUpdate () {
if(PlayerScript.facingRight == true && currentTransform < 1.9){
currentTransform = currentTransform += 6 * Time.deltaTime;
}
if(PlayerScript.facingRight == true && currentTransform >= 1.9){
currentTransform = 2;
}
if(PlayerScript.facingRight == false && currentTransform > -1.9){
currentTransform = currentTransform -= 6 * Time.deltaTime;
}
if(PlayerScript.facingRight == false && currentTransform <= -1.9){
currentTransform = -2;
}
}
Your answer
Follow this Question
Related Questions
I want to make my camera follow the player ONLY when the player is on solid ground [2D] 0 Answers
2D platformer: how to stop camera at the end of the map? 0 Answers
I want to make my camera follow the player ONLY when the player is on solid ground [2D] 1 Answer
Changing gravity depending on character/camera rotation 0 Answers