Unity C#使用Transform.RotateAround和触摸在目标周围移动相机
本文关键字:目标 周围 移动 相机 触摸 使用 Transform RotateAround Unity | 更新日期: 2023-09-27 17:58:02
所以我正在考虑更改触摸输入的Transform.RotateAround脚本。如果我去掉了"Input.GetAxis",相机会围绕目标自行旋转。我似乎不知道如何格式化代码,以获得字符串的触摸位置。如果有人有任何提示,那将是真棒!
这是代码:
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Mouse Orbit with zoom2")]
public class MouseOrbitImproved2 : MonoBehaviour {
public float speed;
public Transform target;
public float rotateSpeed;
public Transform camera = Camera.main.transform;
public Vector3 camPosition;
public float camSpeed;
public float minDistance;
public float maxDistance;
//private Vector3 moveDirection = Vector3.zero;
//private Vector3 moveDirection = target.position;
public float perspectiveZoomSpeed = 0.5f; // The rate of change of the field of view in perspective mode.
public float orthoZoomSpeed = 0.5f; // The rate of change of the orthographic size in orthographic mode.
void start() {
//camPosition = camera.transform.position;
}
public void Update() {
if (Input.touchCount == 1) {
transform.LookAt(target);
Touch touchSwipe = Input.GetTouch(0);
string position = touchSwipe.deltaPosition;
transform.RotateAround(target.position, Vector3.up, Input.GetAxis(position)* speed);
transform.RotateAround(target.position, Vector3.forward, Input.GetAxis(position)* speed);
}
transform.LookAt(target);
/*
float scroll = Input.GetAxis("Mouse ScrollWheel");
if (scroll != 0)
{
// calculate new position first...
camPosition += transform.forward * scroll * camSpeed;
// then compare to the limits:
float distanceToTarget = Vector3.Distance(target.position, camPosition);
// you can clamp the movement to min and max distances:
if (distanceToTarget > maxDistance){ // clamp at maxDistance...
camPosition = target.position - maxDistance * transform.forward;
}
if (distanceToTarget < minDistance){ // or at minDistance
camPosition = target.position - minDistance * transform.forward;
}
// finally, update the actual camera position:
transform.position = camPosition;
// set camera position
}
*/
// If there are two touches on the device...
if (Input.touchCount == 2)
{
// Store both touches.
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
// Find the position in the previous frame of each touch.
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
// Find the magnitude of the vector (the distance) between the touches in each frame.
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
// Find the difference in the distances between each frame.
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
// If the camera is orthographic...
if (Camera.main.orthographic)
{
// ... change the orthographic size based on the change in distance between the touches.
Camera.main.orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;
// Make sure the orthographic size never drops below zero.
Camera.main.orthographicSize = Mathf.Max(Camera.main.orthographicSize, 0.1f);
}
else
{
// Otherwise change the field of view based on the change in distance between the touches.
Camera.main.fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;
// Clamp the field of view to make sure it's between 0 and 180.
Camera.main.fieldOfView = Mathf.Clamp(Camera.main.fieldOfView, 0.1f, 179.9f);
}
}
}
public void ChangeToAxial (){
Camera.main.transform.position = new Vector3 (45.3f,234.5f,66.9f);
}
public void ChangeToSagright (){
Camera.main.transform.position = new Vector3 (28.13f,76.41f,222.68f);
}
public void ChangeToSagleft (){
Camera.main.transform.position = new Vector3 (49.36f,66.85f,-93.78f);
}
public void ChangeToCoronal () {
Camera.main.transform.position = new Vector3 (-71.6f,69.66f,66.29f);
}
}
我认为,如果你在团结论坛第五单元论坛上用触摸旋转相机看这个问题和答案可能是最好的。它应该使用Input.GetTouch而不是Input.GetAxis。希望这会有所帮助。请注意,脚本是用Javascript编写的,但您不需要整个脚本。