Closed weilifang25 closed 2 months ago
I just answered the same question. Does this solve the problem? https://github.com/BattlehubCode/RTE_Docs/issues/117
Have you tried the SetCameraPositionAndPivot method? https://github.com/BattlehubCode/RTE_Docs/issues/117#issuecomment-2326108856
"I need to find an object in the scene, click a button, and then move the camera so that the object is in the center of the screen"
To answer this part of the question, there is a function called Focus(). You can use it to do exactly this -> move the camera so that the object is in the center of the screen.
using Battlehub.RTCommon;
using Battlehub.RTHandles;
using UnityEngine;
public class FocusExample : MonoBehaviour
{
[SerializeField]
private GameObject m_target;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
var rte = IOC.Resolve<IRTE>();
rte.Selection.activeGameObject = m_target;
RuntimeSceneComponent sceneComponent = FindObjectOfType<RuntimeSceneComponent>();
sceneComponent.Focus(FocusMode.Default);
// or
// sceneComponent.Focus(objPosition: m_target.transform.position, objSize: 1);
}
}
}
Your reply just now still can't solve the problem.
https://github.com/user-attachments/assets/34447064-adb0-42a1-8c4b-1e40f27cc518
我现在去试试你的脚本
You can also try this method (add MoveCamera to game object in the scene, set target field, use Up and Down arrows on keyboard)
using Battlehub.RTHandles;
using UnityEngine;
public class MoveCamera : MonoBehaviour
{
[SerializeField]
private GameObject m_target;
private RuntimeSceneComponent m_sceneComponent;
private Vector3 m_cameraPositon;
void Start()
{
m_sceneComponent = FindAnyObjectByType<RuntimeSceneComponent>();
m_cameraPositon = m_sceneComponent.CameraPosition;
}
void Update()
{
if (m_target == null)
{
return;
}
Vector3 pivot = m_target.transform.position;
m_cameraPositon += (pivot - m_cameraPositon) * Input.GetAxis("Vertical") * Time.deltaTime;
m_sceneComponent.SetCameraPositionAndPivot(m_cameraPositon, pivot);
}
}
https://github.com/user-attachments/assets/a8acf98a-ef26-4b4b-a43a-8b16a34dd9a7
SetCameraPositionAndPivot,The method works for me, thank you very much for your help
You're welcome!
Thanks for your reply, but this doesn't solve my problem. I want to ask why I can't move the main camera? I need to find an object in the scene, click a button, and then move the camera so that the object is in the center of the screen. The problem now is that the camera position is fixed and can't be changed.