- Home /
Why when using camera position to make zoom in/out it's not working ?
I have mouse orbit script i can rotate the camera. The script is attached to a gameobject.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseOrbit : MonoBehaviour {
public Transform target;
public float distance = 5.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
public float distanceMin = .5f;
public float distanceMax = 15f;
private Rigidbody rigidbody;
float x = 0.0f;
float y = 0.0f;
// Use this for initialization
void Start()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
rigidbody = GetComponent<Rigidbody>();
// Make the rigid body not change rotation
if (rigidbody != null)
{
rigidbody.freezeRotation = true;
}
}
void Update()
{
// Updating camera distance on every frame
distance = RayCast3.distance3;
//Setting maximum distance so the camera doesnt go too far
if (distance > 2)
{
distance = 2;
}
}
void LateUpdate()
{
if (target)
{
x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
RaycastHit hit;
if (Physics.Linecast(target.position, transform.position, out hit))
{
distance -= hit.distance;
}
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle >= 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}
And two classes to used in this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookAtCamera : MonoBehaviour {
public Transform target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.LookAt(target);
}
}
And
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RayCast3 : MonoBehaviour {
public static float distance3 = 5;
void Update()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward),out hit))
{
distance3 = hit.distance;
}
}
}
Now i have another script that attached to the main camera:
using UnityEngine;
using System.Collections;
public class CameraMover : MonoBehaviour
{
private float zoomSpeed = 5.0f;
float minFov = 15f;
float maxFov = 90f;
float sensitivity = 10f;
void Update()
{
ByPosition();
}
private void ByFieldOfView()
{
float fov = Camera.main.fieldOfView;
fov += Input.GetAxis("Mouse ScrollWheel") * sensitivity;
fov = Mathf.Clamp(fov, minFov, maxFov);
Camera.main.fieldOfView = fov;
}
private void ByPosition()
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
transform.position += this.transform.forward * scroll * zoomSpeed;
}
}
If on this script i'm calling the method ByFieldOfView it will work i can make zoom in/out with the mouse wheel. But if i'm calling the method ByPosition it does nothing, rolling the mouse wheel not changing the zoom in/out.
I tried this ByPosition on a new clean project attached it to main camera and the zoom in/out is working. I also checked on this project if i disable the mouse orbit script the ByPosition will work. But using the method ByPosition and the mouse orbit scripts same time will not work the zoom in/out.
Your answer
Follow this Question
Related Questions
How can i Instantiate on the terrain from left to right ? 0 Answers
Why the tile map scripts take almost all the cpu usage ? cpu usage is getting to 99% at times 1 Answer
How can i rotate object by pressing on key R and keep object facing to me my self ? 0 Answers
How can i spawn new gameobjects to be inside the terrain area ? 2 Answers
How can i change vertex posiiton to push it back -1 on z ? 1 Answer