- Home /
Zoom in when player is accelerating/ if not zoom out
This is the code i have written - the player movement is fine but I can't get the orthographic size to lerp to Maximum Orthographic size/Minimum Orthographic size.
using UnityEngine;
using System.Collections;
public class playerMovement : MonoBehaviour {
//orthographic size for zoom Effect
public float maxOrthographicSize = 40f;
public float minOrthographicSize = 25f;
public float Smoothing = 1f;
public float maxSpeed = 200f; // Maximum speed - will be used to LIMIT PLAYER SPEED.
public float aceleration = 5f; // Player acceleration Speed.
public float rotateSpeed = 180f; // Player Rotation Speed
bool onMove; // Use this to check for Player Input to Move Forward
// Do GRAPHICS and INPUTS updates
void Update () {
//// ROTATING THE PLAYER SHIP
// Grab our rotation quaternion
Quaternion rotate = transform.rotation;
// Grab the Z euler angle
float z = rotate.eulerAngles.z;
// Change the Z angle based on input
z -= Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime;
// Recreate the quaternion
rotate = Quaternion.Euler(0, 0, z);
// Feed the quaternion into our rotation
transform.rotation = rotate;
// When The player is Holding Down the Input set the Boolean to "True".
if (Input.GetButtonDown("Move")){
onMove = true;
AccelerationZoomOut();
}
// if the button is not being Held down the Boolean is set to "False".
if (Input.GetButtonUp("Move")) {
onMove = false;
NoAccelerationZoomIn();
}
}
// Do the Physics Engine updates here!
void FixedUpdate() {
//When the the Move Button is down then Player MOVES
// if the Bool is False then the Rigidbody will fall
if (onMove == true)
{
// Pushed Playership RIGIDBODY
rigidbody2D.AddForce(transform.up * aceleration* Time.deltaTime);
// limit the Player speed from turning into a torpedo
rigidbody2D.velocity = Vector3.ClampMagnitude(rigidbody2D.velocity, maxSpeed);
}
}
void AccelerationZoomOut()
{
//When Player is Accelerating the camera zooms out from the player
Camera.main.orthographicSize = Mathf.Lerp(maxOrthographicSize, minOrthographicSize, Time.deltaTime * Smoothing);
}
void NoAccelerationZoomIn()
{
//When Player is not accelerating the camera zooms in to the player
Camera.main.orthographicSize = Mathf.Lerp(minOrthographicSize, maxOrthographicSize, Time.deltaTime * Smoothing);
}
}
GetButtonUp is only going to call the function once, when you lift your finger off of the button. So I don't believe that's enough times to get your camera to lerp properly to it's position. I would just use on$$anonymous$$ove bool and do an if / else if statement in your Update function to call AccelerationZoomOut() and NoAccelerationZoomIn(). Would make it easier that way.
Your answer
Follow this Question
Related Questions
How to use vuforia AR Camera for Zoom 1 Answer
Simple Camera ZoomIn Out Script 1 Answer
Camera Changing size fluidly depending on An Objects y position 0 Answers
Changing camera position to shoot through scope on gun 1 Answer
Lining up Fov's 0 Answers