Hi, I struggle a lot with one basic thing : clamp my camera position.
The issue is that my camera controller is a bit complex, so I can’t just use mathf.calmp.
My camera is inside a game object I named “Camera Controller”, so I can move it forward without taking to account the X rotation of the camera.
For the x and z axis, the clamp is done with mathf.clamp, because the movement is only on those axis. However, I added a mouse scroll movement that zoom on the z axis of the camera (not the camera controller), so in global world, it moves the y and z axis, because of the rotation of the camera.
Now I can’t just clamp the y value of that mouse scroll, because the z still moves.
Here is the code :
using UnityEngine;
public class CameraController : MonoBehaviour
{
public float panSpeed = 50f;
public float rotSpeed = 50f;
//marge pour le déplacement à la souris
public float scrollSpeed = 5f;
public float dragSpeed = 5f;
private Vector3 dragOrigin;
[Header("Y")]
public float minY = 10f;
public float maxY = 80f;
[Header("X")]
public float minX = 10f;
public float maxX = 80f;
[Header("Z")]
public float minZ = 10f;
public float maxZ = 80f;
public GameObject cam;
void Update()
{
Vector3 newPos = transform.position;
Vector3 newCamPos = cam.transform.localPosition;
if(GameManager.gameIsOver)
{
//plus pouvoir controller la cam lors du game over
this.enabled = false;
return;
}
//déplacement avant
if(Input.GetKey(KeyCode.W))
{
newPos += new Vector3(this.transform.forward.x,0,this.transform.forward.z) * panSpeed * Time.unscaledDeltaTime;
}
//déplacement arrière
if(Input.GetKey(KeyCode.S))
{
newPos += new Vector3(-this.transform.forward.x,0,-this.transform.forward.z) * panSpeed * Time.unscaledDeltaTime;
}
//déplacement gauche
if(Input.GetKey(KeyCode.A))
{
newPos += -transform.right * panSpeed * Time.unscaledDeltaTime;
}
//déplacement droite
if(Input.GetKey(KeyCode.D))
{
newPos += transform.right * panSpeed * Time.unscaledDeltaTime;
}
//rotation droite
if(Input.GetKey(KeyCode.Q))
{
transform.Rotate(Vector3.down * rotSpeed * Time.unscaledDeltaTime, Space.World);
}
//rotation gauche
if(Input.GetKey(KeyCode.E))
{
transform.Rotate(Vector3.up * rotSpeed * Time.unscaledDeltaTime, Space.World);
}
//chopper la molette (molette = un axis de la souris aussi)
float scroll = Input.GetAxis("Mouse ScrollWheel");
//zoomer/dezoomer
newCamPos -= cam.transform.forward * scroll * 1000 * scrollSpeed * Time.unscaledDeltaTime;
//bloquer la cam entre le min et le max
newPos.x = Mathf.Clamp(newPos.x, minX, maxX);
newPos.z = Mathf.Clamp(newPos.z, minZ, maxZ);
transform.position = newPos;
cam.transform.localPosition = newCamPos;
UpdateDrag();
}
void UpdateDrag()
{
if (Input.GetMouseButtonDown(2))
{
dragOrigin = Input.mousePosition;
}
if (Input.GetMouseButton(2))
{
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
Vector3 move = new Vector3(pos.x * dragSpeed, 0, pos.y * dragSpeed);
transform.Translate(-move, Space.Self);
dragOrigin = Input.mousePosition;
}
}
}
Has anyone any idea ? Thanks in advance.