I first got behind this engine and found a script for a free camera, so I need to limit it along the z, y, and x axes. I don’t want the object to fly beyond the limits I set. The camera itself:

using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraFly : MonoBehaviour { public float mouseSensitivity = 3.0f; public float speed = 2.0f; private Vector3 transfer; public float minimumX = -360F; public float maximumX = 360F; public float minimumY = -60F; public float maximumY = 60F; float rotationX = 0F; float rotationY = 0F; Quaternion originalRotation; void Start() { originalRotation = transform.rotation; } void Update() { // ДвиТСния ΠΌΡ‹ΡˆΠΈ -> Π’Ρ€Π°Ρ‰Π΅Π½ΠΈΠ΅ ΠΊΠ°ΠΌΠ΅Ρ€Ρ‹ rotationX += Input.GetAxis("Mouse X") * mouseSensitivity; rotationY += Input.GetAxis("Mouse Y") * mouseSensitivity; rotationX = ClampAngle (rotationX, minimumX, maximumX); rotationY = ClampAngle (rotationY, minimumY, maximumY); Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up); Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left); transform.rotation = originalRotation * xQuaternion * yQuaternion; // ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Ρ‰Π΅Π½ΠΈΠ΅ ΠΊΠ°ΠΌΠ΅Ρ€Ρ‹ transfer = transform.forward * Input.GetAxis("Vertical"); transfer += transform.right * Input.GetAxis("Horizontal"); transform.position += transfer * speed * Time.deltaTime; } 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); } } 

Regarding the limitation of the review, everything is clear, there are public class variables. I would like something like Ссли позиция.ΠΊΠ°ΠΌΠ΅Ρ€Π°.Ρ… = Π½ΡƒΠΆΠ½ΠΎΠ΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠΎ x => Π΄Π²ΠΈΠ³Π°Ρ‚ΡŒ ΠΊΠ°ΠΌΠ΅Ρ€Ρƒ Π² Π½ΡƒΠΆΠ½ΠΎΠ΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠΎ x . Do something like a dead end. I will be glad to any help.

  • Did you try to do something yourself? - RiotBr3aker
  • Well, I scoured the dock on unity and did not understand something. - Skrillexazem
  • Then you need to hire a programmer, unfortunately, here they do not write code for you. - RiotBr3aker
  • I just thought maybe what page to read may be thrown off from the same dock, but ok, I will then sort it out myself. - Skrillexazem
  • one
    @Kir_Antipov, I didn’t seem to have rushed, just asked if the person did something himself or came here with a copy of the camera code. The task itself is elementary and feasible even for a newbie in a unit, the question is only in desire :) - RiotBr3aker

1 answer 1

Try this code:

 using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraFly : MonoBehaviour { public float mouseSensitivity = 3.0f; public float speed = 2.0f; private Vector3 transfer; public float minimumX = -360F; public float maximumX = 360F; public float minimumY = -60F; public float maximumY = 60F; float rotationX = 0F; float rotationY = 0F; Quaternion originalRotation; const float minCamX = -400; // минимальная позиция ΠΏΠΎ X const float maxCamX = 400; // максимальная позиция ΠΏΠΎ X const float minCamY = -100; // минимальная позиция ΠΏΠΎ Y const float maxCamY = 100; // максимальная позиция ΠΏΠΎ Y const float minCamZ = -400; // минимальная позиция ΠΏΠΎ Z const float maxCamZ = 400; // максимальная позиция ΠΏΠΎ Z void Start() { originalRotation = transform.rotation; } void Update() { // ДвиТСния ΠΌΡ‹ΡˆΠΈ -> Π’Ρ€Π°Ρ‰Π΅Π½ΠΈΠ΅ ΠΊΠ°ΠΌΠ΅Ρ€Ρ‹ rotationX += Input.GetAxis("Mouse X") * mouseSensitivity; rotationY += Input.GetAxis("Mouse Y") * mouseSensitivity; rotationX = ClampAngle(rotationX, minimumX, maximumX); rotationY = ClampAngle(rotationY, minimumY, maximumY); Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up); Quaternion yQuaternion = Quaternion.AngleAxis(rotationY, Vector3.left); transform.rotation = originalRotation * xQuaternion * yQuaternion; // ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Ρ‰Π΅Π½ΠΈΠ΅ ΠΊΠ°ΠΌΠ΅Ρ€Ρ‹ transfer = transform.forward * Input.GetAxis("Vertical"); transfer += transform.right * Input.GetAxis("Horizontal"); transform.position += transfer * speed * Time.deltaTime; RestrictionCam(); } 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); } // ΠžΠ³Ρ€Π°Π½ΠΈΡ‡Π΅Π½ΠΈΠ΅ ΠΊΠ°ΠΌΠ΅Ρ€Ρ‹ void RestrictionCam() { Vector3 cl = transform.position; cl.x = Mathf.Clamp(cl.x, minCamX, maxCamX); cl.y = Mathf.Clamp(cl.y, minCamY, maxCamY); cl.z = Mathf.Clamp(cl.z, minCamZ, maxCamZ); transform.position = cl; } } 
  • I already started cutting my crutches, I understand that something is wrong))) Thanks for throwing off, everything works fine! - Skrillexazem