Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
Unity. How to create a rotating camera around an object with the change of this object

I want to make a rotating camera on the map around the target object, with the first point I did it. After clicking on another object, which I also want to inspect, my camera shifts and rotates not around the object, but some other point. How to point to an object around which I want the camera to rotate with the mouse

LabsManager lab;

public Transform lookCentre;
public Transform lookZRU;
public Vector3 offset;
public float sensitivity; // чувствительность мышки
public float limit; // ограничение вращения по Y
public float zoom; // чувствительность при увеличении, колесиком мышки
public float zoomMax; // макс. увеличение
public float zoomMin; // мин. увеличение
private float X, Y;
bool activeTarget;


void Start()
{
    GameObject activ = GameObject.Find("LabsManager");
    lab = activ.GetComponent<LabsManager>();
    activeTarget = false;
}


void Update()
{
    if (lab.LabName == "Осмотр")
    {
        if (activeTarget == false)
        {
            CameraPosition(lookCentre);
            MouseLook();
        }
        LookZRU();

    }
}


void CameraPosition(Transform target)
{
    if (activeTarget == false)  
    {
        transform.position = transform.localRotation * offset + target.position;
    }
        MouseLook();
    

}

void MouseLook()
{
    if (Input.GetAxis("Mouse ScrollWheel") > 0) offset.z += zoom;
    else if (Input.GetAxis("Mouse ScrollWheel") < 0) offset.z -= zoom;
    offset.z = Mathf.Clamp(offset.z, -Mathf.Abs(zoomMax), -Mathf.Abs(zoomMin));

    if (Input.GetMouseButton(1))
    {
        X = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivity;
    }
    if (Input.GetMouseButton(0))
    {
        Y += Input.GetAxis("Mouse Y") * sensitivity;
    }
    Y = Mathf.Clamp(Y, -limit, limit);
    transform.localEulerAngles = new Vector3(-Y, X, 0);
}

void LookZRU()
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;

    if (Physics.Raycast(ray, out hit))
    {
        if (hit.collider.gameObject.GetComponent<SelectZRU>())
        {
            Debug.Log(gameObject.name + " бла бла");
            if (Input.GetKeyDown(KeyCode.R))
            {
                activeTarget = true;
                CameraPosition(lookZRU);
            }
        }
    }

    if (activeTarget == true)
    {
        MouseLook();
    }
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can use rotate around, just update "obj" value with your new clicked object position.

Vector3 obj = target.transform.position; //change this
transform.RotateAround(obj, Vector3.up, 20 * Time.deltaTime);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!