- Home /
Camera movement help
Im trying to make my camera speed up gradually till a maximum speed of 2.5f from 0.1f. How would I go round doing this? thanks in advance/
using UnityEngine; using System.Collections;
public class CameraMovement : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate(Vector2.down * 0.16f);
if (transform.position.y == 4)
{
transform.Translate(-Vector2.down * 0.16f);
}
if (transform.position.y == -4)
{
transform.Translate(Vector2.down * 0.16f);
}
}
}
Answer by Oana · Feb 14, 2017 at 08:10 AM
That's a strange script. So if you start from higher than 4, it will go down to 4 and get stuck there, but if you start from lower than 4, it will keep going down, moving twice as fast for 1 frame when at -4? Unless the camera is upside-down or something. I'm not going to touch that part since I am not really sure what you're trying to do with it. Also, since you're using Update, is there a reason you're not using Time.deltaTime to move the camera?
Either way, to answer your question, you could do something like this to speed the camera up:
public class CameraMovement : MonoBehaviour {
float speed = 0.1f;
float speedIncrement = 1.1f; //how fast it increments. tweak this number around to make it slower or faster
void Update () {
if (speed<2.5f) speed*=speedIncrement;
if (speed>2.5f) speed = 2.5f;
transform.Translate(Vector2.down * speed);
}
}
If you want to change direction
public class Camera$$anonymous$$ovement : $$anonymous$$onoBehaviour {
float speed = 0.1f;
float speedIncrement = 1.1f; //how fast it increments. tweak this number around to make it slower or faster
void Update () {
if (speed<2.5f) speed*=speedIncrement;
if (speed>2.5f) speed = 2.5f;
if($$anonymous$$athf.Abs(transform.position.y) >= 4)
speed = speed *-1;
transform.Translate(Vector2.down * speed);
}
}
ps it looked wired, you may have to change values
Hi, I'm using 0.16f because 1f is way too fast. And the camera doesn't move when I use 0.16f with your script. Any ideas to be able to use 0.16f?
I'm confused, how are you using 0.16? Could you paste the code?
This is the code I'm using, but it isn't working... Do you know any fixes?
float speed = 0.01f;
float speedIncrement = 0.1f; //how fast it increments. tweak this number around to make it slower or faster
void Update () {
if (speed<2.5f) speed*=speedIncrement;
if (speed>2.5f) speed = 2.5f;
transform.Translate(Vector2.down * speed);
}
Your answer
Follow this Question
Related Questions
How do I add a camera bounce effect every time the player lands? 0 Answers
[2D] Camera movement causes flickering/jittering sprites 2 Answers
Player Movement using lerp 3 Answers
Slow camera move speed? 2 Answers
Movement Backwards is Slower 0 Answers