我正在使用Unity制作游戏,但我遇到了数学问题。 我有一个半径为10且中心为(0,0,0)的球体。 我希望相机在那个球体周围移动,但我找不到任何方法来做我想做的事情。 我在X轴和Y轴上移动相机(因此在球体外面得到一个点)我想设置它的Z轴,这样相机就会回到球体上,我正在使用这个等式:r ^ 2 = x ^ 2 + y ^ 2 + z ^ 2 => z ^ 2 = r ^ 2 - x ^ 2 - y ^ 2 但它没有用......请帮助我
修改
这是我的代码(在c#中):
private void OnMouseDrag()
{
var newX = mainCameraTransform.position.x + Input.GetAxis("Mouse X");
var newY = mainCameraTransform.position.y + Input.GetAxis("Mouse Y");
var maxDistance = 10.0f;
newX = Mathf.Clamp(newX, -maxDistance * 0.85f, maxDistance * 0.85f);
newY = Mathf.Clamp(newY, 1.0f * 0.85f, maxDistance * 0.85f);
var newZ = Mathf.Sqrt(Mathf.Abs(maxDistance * maxDistance - newX * newX - newY * newY));
mainCameraTransform.position = new Vector3(newX, newY, newZ);
mainCameraTransform.LookAt(Vector3.zero);
}
正如你所看到的,我使用Clamp来保持X和Y小于半径,但它没有帮助......
答案 0 :(得分:1)
您必须按圆圈限制2D坐标
len = Mathf.Sqrt(newX * newX + newY * newY);
//perhaps you have Len or Hypot function in your Math library
if len > maxDistance then
newX = maxDistance * newX / len
newY = maxDistance * newY / len;
答案 1 :(得分:1)
这还没有经过测试,但它应该非常接近
value Payment-method ::= credit-card : cb
答案 2 :(得分:1)
将此脚本拖放到相机上,使用鼠标右键将其围绕目标进行环绕
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OrbitAroundObject : MonoBehaviour {
public Transform target;
public float distance = 10.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
public float distanceMin = .5f;
public float distanceMax = 15f;
public float smoothTime = 2f;
public float zoomSpeed = 1;
float rotationYAxis = 0.0f;
float rotationXAxis = 0.0f;
float velocityX = 0.0f;
float velocityY = 0.0f;
// Use this for initialization
void Start() {
Vector3 angles = transform.eulerAngles;
rotationYAxis = angles.y;
rotationXAxis = angles.x;
// Make the rigid body not change rotation
if (GetComponent<Rigidbody>()) {
GetComponent<Rigidbody>().freezeRotation = true;
}
}
void LateUpdate() {
if (target) {
if (Input.GetMouseButton(1)) {
velocityX += xSpeed * Input.GetAxis("Mouse X") * 0.02f;
velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
}
//distance -= (Input.mouseScrollDelta.y*Time.deltaTime);
distance = Mathf.Lerp(distance, distance-(Input.mouseScrollDelta.y*zoomSpeed) , Time.deltaTime * smoothTime);
distance = Mathf.Clamp(distance, distanceMin, distanceMax);
rotationYAxis += velocityX;
rotationXAxis -= velocityY;
rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
//Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
Quaternion rotation = toRotation;
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);
}
}