- Home /
Camera zooms in and out through objects
Hello guys, I am currently having a problem with my camera zoom in and out. For example if i am standing next to a wall in my game and there is a room on the other side I can zoom out through the wall into the other room and similarly I can zoom in form the second room to the room I am currently in. So can anybody help because I am new to programming in Unity and I still don't know a lot of stuff so I can't figure out how to do it.
Here is my code for the Camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonCamera : MonoBehaviour {
public Transform playerCam;
public Transform character;
public Transform centerPoint;
private float mouseY;
private float mouseX;
public float mouseSensitivity = 10f;
public float zoom;
public float zoomMin = -2f;
public float zoomMax = -10f;
public float rotationSpeed = 5f;
// Use this for initialization
void Start () {
zoom = -3;
}
// Update is called once per frame
void Update () {
zoom += Input.GetAxis ("Mouse ScrollWheel") * 2;
if (zoom > zoomMin) {
zoom = zoomMin;
}
if (zoom < zoomMax) {
zoom = zoomMax;
}
playerCam.transform.localPosition = new Vector3 (0, 0, zoom);
if (Input.GetMouseButton (1)) {
mouseX += Input.GetAxis ("Mouse X");
mouseY += Input.GetAxis ("Mouse Y");
}
mouseY = Mathf.Clamp (mouseY, -60f, 60f);
playerCam.LookAt (centerPoint);
centerPoint.localRotation = Quaternion.Euler (mouseY, mouseX, 0);
centerPoint.position = new Vector3 (character.position.x, character.position.y + 1, character.position.z);
if (Input.GetAxis ("Vertical") > 0 | Input.GetAxis("Vertical") < 0) {
Quaternion turnAngle = Quaternion.Euler (0, centerPoint.eulerAngles.y, 0);
character.rotation = Quaternion.Slerp (character.rotation, turnAngle, Time.deltaTime * rotationSpeed);
}
}
}
Comment