我创建了新的Unity项目,在中心添加了一个立方体,现在想让玩家通过滑动和/或鼠标拖动来围绕这个立方体旋转相机。
请详细说明实施此操作或关键字以找到答案或在何处阅读相关内容?
答案 0 :(得分:3)
public Transform Target;
public float distance = 2.0f;
public float xSpeed = 20.0f;
public float ySpeed = 20.0f;
public float yMinLimit = -90f;
public float yMaxLimit = 90f;
public float distanceMin = 10f;
public float distanceMax = 10f;
public float smoothTime = 2f;
float rotationYAxis = 0.0f;
float rotationXAxis = 0.0f;
float velocityX = 0.0f;
float velocityY = 0.0f;
void Update()
{
if (Input.GetMouseButton(0))
{
velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
}
rotationYAxis += velocityX;
rotationXAxis -= velocityY;
rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + Target.position;
transform.rotation = rotation;
transform.position = position;
velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
}
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);
}
取自https://answers.unity.com/questions/1257281/how-to-rotate-camera-orbit-around-a-game-object-on.html
如果您只想围绕特定的轴旋转,例如围绕Y,您基本上可以这样做
this.transform.RotateAround(Target.transform.position, Vector3.up, Input.GetAxis("Mouse X")*20.0f);
答案 1 :(得分:0)
创建一个名为&#34; CameraRotate&#34;的新C#脚本,打开它并粘贴此脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraRotate : MonoBehaviour {
public Transform cube;
public float sensitivityX;
public float sensitivityY;
GameObject pivot;
void Start () {
pivot = new GameObject ("pivot");
pivot.transform.position = cube.position;
transform.SetParent (pivot.transform);
}
void Update () {
pivot.transform.eulerAngles += new Vector3 (Input.GetAxis ("Mouse Y") * -sensitivityX * Time.deltaTime, Input.GetAxis ("Mouse X") * sensitivityY * Time.deltaTime, 0);
}
}
将此脚本分配给您的相机,然后在Inspector选项卡中选择相机,将多维数据集(拖动)指定到&#34; cube&#34;插槽,并为灵敏度字段分配一个值(我使用了大约500的东西),按播放和测试。
这个脚本有什么作用? 它在立方体的中心创建一个新的GameObject作为旋转的参考,它被称为枢轴。因此,它将Camera设置为枢轴的子项,并根据鼠标轴旋转轴。
答案 2 :(得分:0)
我这样写道:
float mouseX = -Input.GetAxis("Mouse X");
float mouseY = -Input.GetAxis("Mouse Y");
float magnitude = transform.position.magnitude;
Vector3 mouseSwipe = new Vector3(mouseX, mouseY, 0);
Vector3 startPoint = new Vector3((float)camera.pixelWidth / 2, (float)camera.pixelHeight / 2, magnitude - 1);
Vector3 startPointWorld = camera.ScreenToViewportPoint(startPoint);
Vector3 endPointWord = camera.ScreenToViewportPoint(startPoint + mouseSwipe);
Vector3 mouseSwipeWord = endPointWord - startPointWorld;
float dragLat = mouseSwipeWord.y;
float dragLng = mouseSwipeWord.x;
Vector3 oldPosition = transform.position / magnitude;
float lat = Mathf.Asin(oldPosition.y);
float rsmall = Mathf.Acos(oldPosition.y);
float lng = Mathf.Atan2(oldPosition.z / rsmall, oldPosition.x / rsmall);
lat += dragLat * 10 * 2 * Mathf.PI;
if( lat*180/Mathf.PI > 80 )
{
lat = 80 * Mathf.PI / 180;
}
else if( lat*180/Mathf.PI < -80)
{
lat = -80 * Mathf.PI / 180;
}
lng += dragLng * 10 * 2 * Mathf.PI * 2;
float y = Mathf.Sin(lat);
rsmall = Mathf.Cos(lat);
float x = rsmall * Mathf.Cos(lng);
float z = rsmall * Mathf.Sin(lng);
Vector3 newPosition = new Vector3(x, y, z);
newPosition *= magnitude;
transform.position = newPosition;
LookAtTarget();
目标是通过拖动模拟鼠标是旋转对象。