Reputation: 85
I'm doing some 2D platformer tests, and want to figure out how I would make the camera move upwards when the upwards axis is held, until its float = 1
float Look = (Input.GetAxis("VerticalAxis"));
print(Look);
if (Look >0 )
{
}
I'm stuck right here.
Upvotes: 0
Views: 130
Reputation: 71
In case you want the camera to move up until the vertical axis is 1 you can do the following:
Add this script to your camera:
public float speed;
void Update()
{
float verticalAxis = Input.GetAxis("Vertical");
if (verticalAxis > 0 && verticalAxis < 1)
{
transform.Translate(Vector3.up * Time.deltaTime * speed);
}
}
you can replace the public variable "speed" in inspector if you would like the camera to move faster (remember to set it at least to a number higher than 0, if not the camera wont move).
If you would like to move the camera up once the vertical axis is 1 you cand change the line:
if (verticalAxis > 0 && verticalAxis < 1)
to
if (verticalAxis == 1)
if you want to move it once its higher than 0 you can change it to:
if (verticalAxis > 0)
And thats it, i hope this helped.
Upvotes: 0
Reputation: 756
Does this help:
float Look = 0.0f;
void Update() {
if(Input.GetAxis("VerticalAxis") > 0) {
if(Look < 1)
Look += 0.001f;
} else {
if(Look > 0)
Look -= 0.001f;
}
print(Look);
}
{Code is untested} but should work, if the up button is pressed it should grow toward 1 with a 0.001 increase per frame until it gets to 1, and when its released it will go down again with a 0.001 decrease pr frame, but you can always add a time variable, so it will run smoothly such as using Look += Time.deltaTime/0.1 or something like that.
Upvotes: 1
Reputation: 11933
The camera is attached to a GameObject
, which has a Transform
that is part of the physical representation of that object and contains the position, rotation and scale of the object.
So if you want to move the camera, just change the position of its transform, like this:
Camera.current.transform.position = someVector3;
Camera.current
is a "shortcut" to the main camera.
Upvotes: 0