I’m working on a side-scrolling 3D game, so my camera is fixed and slide on left-right.
What i’m trying to achieve is that when I press W or S the camera will be pushed closer or further.
But for noob-reason, I can’t get how … any help would be appreciated.
void Update
{
if (Input.GetKeyDown(KeyCode.W))
transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x, 5f, transform.position.z), 2f);
else if (Input.GetKeyDown(KeyCode.S))
transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x, 25f, transform.position.z), 2f);
else
transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x, yDeafultAxis, transform.position.z), 2f);
}
@Mavina
Okay let me try to explain better
on W key the camera will move forward (simulating zoom), and this isn’t a problem.
the problem in on
if (Input.GetKeyUp(KeyCode.W))
I would like to restore the original position with a bit of Smooth
Do it your self… still be the best answare
void Update ()
{
if (Input.GetKey(KeyCode.A))
transform.Translate(Vector3.left * moveMult * Time.deltaTime);
if (Input.GetKey(KeyCode.D))
transform.Translate(Vector3.right * moveMult * Time.deltaTime);
if (Input.GetKey(KeyCode.W))
transform.Translate(Vector3.forward * moveMult * Time.deltaTime);
if (Input.GetKey(KeyCode.S))
transform.Translate(Vector3.back * moveMult * Time.deltaTime);
oldPos = new Vector3(transform.position.x, oldPos.y, oldPos.z);
if (transform.position != oldPos)
transform.position = Vector3.SmoothDamp(transform.position, oldPos, ref velocity, smoothTime);
}