Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Sync Position How to xy

Discussion in 'Scripting' started by wechat_os_Qy06U4-4WRqwOqeFdJSRbcG-I, Apr 25, 2022.

  1. wechat_os_Qy06U4-4WRqwOqeFdJSRbcG-I

    wechat_os_Qy06U4-4WRqwOqeFdJSRbcG-I

    Joined:
    Jan 11, 2021
    Posts:
    4
    Code (CSharp):
    1.        
    2. protected Vector3 _cameraPivotPosition;
    3.         protected Vector3 CalcCameraPosition(float x, float y, float distance) {
    4.             Vector3 offset = Vector3.forward * distance;
    5.             Quaternion rotYaxis = Quaternion.AngleAxis(y, Vector3.left);
    6.             Quaternion rotXaxis = Quaternion.AngleAxis(x, Vector3.up);
    7.             Quaternion rotation = rotXaxis * rotYaxis;
    8.             return _cameraPivotPosition + rotation * offset;
    9.         }
    10.  
    Code (CSharp):
    1.        
    2.       public Camera m_Camera;      
    3.         public void SyncPosition(Transform newPosition, Transform resultPosition)
    4.         {
    5.             DisableUpdate = true;
    6.  
    7.             var cameraLookAt = _cameraPivotPosition;
    8.             m_Camera.transform.position = newPosition.position;
    9.             m_Camera.transform.rotation = newPosition.rotation;
    10.             m_Camera.transform.LookAt(cameraLookAt);
    11.  
    12.            
    13.             var CameraDir = newPosition.position - cameraLookAt;
    14.             // calc distance
    15.             var distance = CameraDir.magnitude;
    16.  
    17.             // calc X,Y
    18.             // var  rx = ???  How to calculate
    19.             // var  ry = ???  How to calculate
    20.  
    21.             resultPosition.position = CalcCameraPosition(rx, ry, distance);
    22.             resultPosition.LookAt(cameraLookAt);
    23.         }
    24.  
    set camera new position, how calc rx and ry, thank you very much!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    Camera stuff is pretty tricky... you may wish to consider using Cinemachine from the Unity Package Manager.

    Beyond that, I have no idea what you mean by "set new position."

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220
     
  3. wechat_os_Qy06U4-4WRqwOqeFdJSRbcG-I

    wechat_os_Qy06U4-4WRqwOqeFdJSRbcG-I

    Joined:
    Jan 11, 2021
    Posts:
    4
    this is Camera control scrpit:
    upload_2022-4-25_14-22-33.png

    upload_2022-4-25_14-27-50.png
    Code (CSharp):
    1.  
    2. public class CameraPostionTest : MonoBehaviour
    3. {
    4.     //
    5.     public Transform lastPosition;
    6.     public Transform cameraNewPosition;
    7.     public Transform resultPosition;
    8.     public CameraCtrl camera;
    9.     public float DelayTime = 6f;
    10.     private float triggerDelay = 0;
    11.  
    12.     private void OnTriggerEnter(Collider other)
    13.     {
    14.         if (cameraNewPosition == null || camera == null || lastPosition == null)
    15.         {
    16.             Debug.Log("please set cameraNewPosition and camera and lastPosition.");
    17.             return;
    18.         }
    19.  
    20.         if (camera.DisableUpdate) return;
    21.         lastPosition.position = cameraNewPosition.position;
    22.         camera.SyncPosition(cameraNewPosition, resultPosition);
    23.         triggerDelay = 0;
    24.     }
    25.  
    26.     private void Update()
    27.     {
    28.         if (cameraNewPosition != null)
    29.         {
    30.             if (triggerDelay < DelayTime)
    31.             {
    32.                 triggerDelay += Time.deltaTime;
    33.                 return;
    34.             }
    35.             camera.DisableUpdate = false;
    36.            
    37.             cameraNewPosition.RotateAround(transform.position, Vector3.up,  30*Time.deltaTime);
    38.         }  
    39.     }
    40. }