Unity:使用鼠标位置为我的相机生成角度以围绕对象旋转
Unity: Using Mouse Position To Make Angles For My Camera To Rotate Around An Object
我正在尝试制作面向鼠标的 3d Agar.io。现在,我正在研究基本的运动和相机调整。我希望相机能够围绕一个物体旋转,但这里有一个问题:我只想使用鼠标移动物体和相机,并旋转相机。但是,我能够使用两次距离公式和余弦定律来配置此逻辑。我基于原点 (0, 0)。我确信我所做的过程没有任何问题;但是,我知道它的输出方式是错误的。
每次你向左或向右移动鼠标时,我希望相机在之后立即旋转。我尝试使用 FixedUpdate 和 LateUpdate 事件来尝试实现这一点,但都没有成功。我尝试使用的相机旋转代码是这样的:
void LateUpdate ()
{
// Move Camera
transform.position = new Vector3(camX, 10.5f, camZ);
//Rotate Camera
//transform.eulerAngles = getRotation;
//transform.eulerAngles = Vector3.MoveTowards(currentRotation, getRotation, speed * Time.deltaTime);
}
我拥有的完整代码(已更新):
public class CameraController : MonoBehaviour {
public float speed;
public GameObject player;
private Ray ray;
private RaycastHit hit;
private Vector3 mousePos;
private float camX;
private float camZ;
private float getA;
private float getB;
private float getC;
private float getAngle;
private Vector3 currentRotation;
private Vector3 getRotation;
void Start ()
{
}
void FixedUpdate()
{
}
void LateUpdate ()
{
if (PlayerController.movedPlayer)
{
if (Physics.Raycast(ray, out hit))
{
mousePos = new Vector3(hit.point.x, 1.3f, hit.point.z);
}
// MouseZ
if (mousePos.z >= player.transform.position.z)
{
// Camera Follow Z+
if ((player.transform.position.z >= 0 && player.transform.position.x >= 0) &&
player.transform.position.z >= player.transform.position.x)
{
camZ = player.transform.position.z - 10;
camX = 0;
}
else if ((player.transform.position.z >= 0 && player.transform.position.x < 0) &&
player.transform.position.z >= -player.transform.position.x)
{
camZ = player.transform.position.z - 10;
camX = 0;
}
else if ((player.transform.position.z >= 0 && player.transform.position.x >= 0) &&
player.transform.position.z < player.transform.position.x)
{
camX = player.transform.position.x - 10;
camZ = 0;
}
else if ((player.transform.position.z >= 0 && player.transform.position.x < 0) &&
player.transform.position.z < -player.transform.position.x)
{
camX = player.transform.position.x + 10;
camZ = 0;
}
// Camera Follow Z-
if ((player.transform.position.z < 0 && player.transform.position.x >= 0) &&
-player.transform.position.z >= player.transform.position.x)
{
camZ = player.transform.position.z + 10;
camX = 0;
}
else if ((player.transform.position.z < 0 && player.transform.position.x < 0) &&
-player.transform.position.z >= -player.transform.position.x)
{
camZ = player.transform.position.z + 10;
camX = 0;
}
else if ((player.transform.position.z < 0 && player.transform.position.x >= 0) &&
-player.transform.position.z < player.transform.position.x)
{
camX = player.transform.position.x - 10;
camZ = 0;
}
else if ((player.transform.position.z < 0 && player.transform.position.x < 0) &&
-player.transform.position.z < -player.transform.position.x)
{
camX = player.transform.position.x + 10;
camZ = 0;
}
}
//PlayerZ
else if (mousePos.z < player.transform.position.z)
{
// Camera Follow Z+
if ((player.transform.position.z >= 0 && player.transform.position.x >= 0) &&
player.transform.position.z >= player.transform.position.x)
{
camZ = player.transform.position.z + 10;
camX = 0;
}
else if ((player.transform.position.z >= 0 && player.transform.position.x < 0) &&
player.transform.position.z >= -player.transform.position.x)
{
camZ = player.transform.position.z + 10;
camX = 0;
}
else if ((player.transform.position.z >= 0 && player.transform.position.x >= 0) &&
player.transform.position.z < player.transform.position.x)
{
camX = player.transform.position.x + 10;
camZ = 0;
}
else if ((player.transform.position.z >= 0 && player.transform.position.x < 0) &&
player.transform.position.z < -player.transform.position.x)
{
camX = player.transform.position.x - 10;
camZ = 0;
}
// Camera Follow Z-
if ((player.transform.position.z < 0 && player.transform.position.x >= 0) &&
-player.transform.position.z >= player.transform.position.x)
{
camZ = player.transform.position.z - 10;
camX = 0;
}
else if ((player.transform.position.z < 0 && player.transform.position.x < 0) &&
-player.transform.position.z >= -player.transform.position.x)
{
camZ = player.transform.position.z - 10;
camX = 0;
}
else if ((player.transform.position.z < 0 && player.transform.position.x >= 0) &&
-player.transform.position.z < player.transform.position.x)
{
camX = player.transform.position.x + 10;
camZ = 0;
}
else if ((player.transform.position.z < 0 && player.transform.position.x < 0) &&
-player.transform.position.z < -player.transform.position.x)
{
camX = player.transform.position.x - 10;
camZ = 0;
}
}
// Make Triangle
getA = mousePos.x - player.transform.position.x;
getB = mousePos.z - player.transform.position.z;
getC = Mathf.Sqrt(Mathf.Pow((getA), 2) + Mathf.Pow((getB), 2));
// Get Rotating Angle.
if (getA == 0 && getB >= 0)
{
getAngle = 0;
}
else if (getA == 0 && getB < 0)
{
getAngle = 180;
}
else if (getB == 0 && getA >= 0)
{
getAngle = 90;
}
else if (getB == 0 && getA < 0)
{
getAngle = 270;
}
else
{
if (getA > 0)
{
getAngle = Mathf.Acos((Mathf.Pow((getB), 2) + Mathf.Pow((getC), 2) - Mathf.Pow((getA), 2)) /
(2 * (getB) * (getC))) * Mathf.Rad2Deg;
}
else if (getA < 0)
{
getAngle = -Mathf.Acos((Mathf.Pow((getB), 2) + Mathf.Pow((getC), 2) - Mathf.Pow((getA), 2)) /
(2 * (getB) * (getC))) * Mathf.Rad2Deg;
}
//getAngle = Mathf.Asin(getA * Mathf.Sin(90) / getC);
//Debug.Log("A: " + getA + " B: " + getB + " C: " + getC + " Angle: " + getAngle);
}
// Set Rotating Angle
currentRotation = transform.eulerAngles;
getRotation = new Vector3(45f, getAngle, 0f);
}
// Move Camera
transform.position = new Vector3(camX, 10.5f, camZ);
//Rotate Camera
//transform.eulerAngles = getRotation;
transform.eulerAngles = Vector3.MoveTowards(currentRotation, getRotation, speed * Time.deltaTime);
}
}
让我详细说明一下代码。当鼠标离它最近时,我希望相机朝向原点,反之亦然。我在这方面遇到了一些麻烦,因为我认为我设置不正确。我试图解决这个问题的尝试是使用反向逻辑(即当鼠标靠近原点时,相机距离原点最远 [鼠标 -> 对象 -> 相机)。如果鼠标是离原点最远 [相机 -> 对象 -> 鼠标]。)
既然你知道我遇到的一些问题,下面是我需要回答的问题(我希望你写一个解决方案并告诉我它是如何解决问题的。):如何修复相机旋转以便鼠标移动它会立即旋转,如何指定鼠标是否靠近原点?
更新: 我将所有代码移至 LateUpdate,因为那是您应该执行相机算法的时间。我还 "tried" 简化了鼠标定位部分,所以它不是那么多余。我还有一个 link 到我的项目的屏幕截图:https://gyazo.com/2f5ee8f94c0bc55bbdeaeafa8a8b35f4
我找到了解决方案:
transform.rotation = Quaternion.RotateTowards(transform.rotation, getRotation, speed * Time.deltaTime);
我正在尝试制作面向鼠标的 3d Agar.io。现在,我正在研究基本的运动和相机调整。我希望相机能够围绕一个物体旋转,但这里有一个问题:我只想使用鼠标移动物体和相机,并旋转相机。但是,我能够使用两次距离公式和余弦定律来配置此逻辑。我基于原点 (0, 0)。我确信我所做的过程没有任何问题;但是,我知道它的输出方式是错误的。
每次你向左或向右移动鼠标时,我希望相机在之后立即旋转。我尝试使用 FixedUpdate 和 LateUpdate 事件来尝试实现这一点,但都没有成功。我尝试使用的相机旋转代码是这样的:
void LateUpdate ()
{
// Move Camera
transform.position = new Vector3(camX, 10.5f, camZ);
//Rotate Camera
//transform.eulerAngles = getRotation;
//transform.eulerAngles = Vector3.MoveTowards(currentRotation, getRotation, speed * Time.deltaTime);
}
我拥有的完整代码(已更新):
public class CameraController : MonoBehaviour {
public float speed;
public GameObject player;
private Ray ray;
private RaycastHit hit;
private Vector3 mousePos;
private float camX;
private float camZ;
private float getA;
private float getB;
private float getC;
private float getAngle;
private Vector3 currentRotation;
private Vector3 getRotation;
void Start ()
{
}
void FixedUpdate()
{
}
void LateUpdate ()
{
if (PlayerController.movedPlayer)
{
if (Physics.Raycast(ray, out hit))
{
mousePos = new Vector3(hit.point.x, 1.3f, hit.point.z);
}
// MouseZ
if (mousePos.z >= player.transform.position.z)
{
// Camera Follow Z+
if ((player.transform.position.z >= 0 && player.transform.position.x >= 0) &&
player.transform.position.z >= player.transform.position.x)
{
camZ = player.transform.position.z - 10;
camX = 0;
}
else if ((player.transform.position.z >= 0 && player.transform.position.x < 0) &&
player.transform.position.z >= -player.transform.position.x)
{
camZ = player.transform.position.z - 10;
camX = 0;
}
else if ((player.transform.position.z >= 0 && player.transform.position.x >= 0) &&
player.transform.position.z < player.transform.position.x)
{
camX = player.transform.position.x - 10;
camZ = 0;
}
else if ((player.transform.position.z >= 0 && player.transform.position.x < 0) &&
player.transform.position.z < -player.transform.position.x)
{
camX = player.transform.position.x + 10;
camZ = 0;
}
// Camera Follow Z-
if ((player.transform.position.z < 0 && player.transform.position.x >= 0) &&
-player.transform.position.z >= player.transform.position.x)
{
camZ = player.transform.position.z + 10;
camX = 0;
}
else if ((player.transform.position.z < 0 && player.transform.position.x < 0) &&
-player.transform.position.z >= -player.transform.position.x)
{
camZ = player.transform.position.z + 10;
camX = 0;
}
else if ((player.transform.position.z < 0 && player.transform.position.x >= 0) &&
-player.transform.position.z < player.transform.position.x)
{
camX = player.transform.position.x - 10;
camZ = 0;
}
else if ((player.transform.position.z < 0 && player.transform.position.x < 0) &&
-player.transform.position.z < -player.transform.position.x)
{
camX = player.transform.position.x + 10;
camZ = 0;
}
}
//PlayerZ
else if (mousePos.z < player.transform.position.z)
{
// Camera Follow Z+
if ((player.transform.position.z >= 0 && player.transform.position.x >= 0) &&
player.transform.position.z >= player.transform.position.x)
{
camZ = player.transform.position.z + 10;
camX = 0;
}
else if ((player.transform.position.z >= 0 && player.transform.position.x < 0) &&
player.transform.position.z >= -player.transform.position.x)
{
camZ = player.transform.position.z + 10;
camX = 0;
}
else if ((player.transform.position.z >= 0 && player.transform.position.x >= 0) &&
player.transform.position.z < player.transform.position.x)
{
camX = player.transform.position.x + 10;
camZ = 0;
}
else if ((player.transform.position.z >= 0 && player.transform.position.x < 0) &&
player.transform.position.z < -player.transform.position.x)
{
camX = player.transform.position.x - 10;
camZ = 0;
}
// Camera Follow Z-
if ((player.transform.position.z < 0 && player.transform.position.x >= 0) &&
-player.transform.position.z >= player.transform.position.x)
{
camZ = player.transform.position.z - 10;
camX = 0;
}
else if ((player.transform.position.z < 0 && player.transform.position.x < 0) &&
-player.transform.position.z >= -player.transform.position.x)
{
camZ = player.transform.position.z - 10;
camX = 0;
}
else if ((player.transform.position.z < 0 && player.transform.position.x >= 0) &&
-player.transform.position.z < player.transform.position.x)
{
camX = player.transform.position.x + 10;
camZ = 0;
}
else if ((player.transform.position.z < 0 && player.transform.position.x < 0) &&
-player.transform.position.z < -player.transform.position.x)
{
camX = player.transform.position.x - 10;
camZ = 0;
}
}
// Make Triangle
getA = mousePos.x - player.transform.position.x;
getB = mousePos.z - player.transform.position.z;
getC = Mathf.Sqrt(Mathf.Pow((getA), 2) + Mathf.Pow((getB), 2));
// Get Rotating Angle.
if (getA == 0 && getB >= 0)
{
getAngle = 0;
}
else if (getA == 0 && getB < 0)
{
getAngle = 180;
}
else if (getB == 0 && getA >= 0)
{
getAngle = 90;
}
else if (getB == 0 && getA < 0)
{
getAngle = 270;
}
else
{
if (getA > 0)
{
getAngle = Mathf.Acos((Mathf.Pow((getB), 2) + Mathf.Pow((getC), 2) - Mathf.Pow((getA), 2)) /
(2 * (getB) * (getC))) * Mathf.Rad2Deg;
}
else if (getA < 0)
{
getAngle = -Mathf.Acos((Mathf.Pow((getB), 2) + Mathf.Pow((getC), 2) - Mathf.Pow((getA), 2)) /
(2 * (getB) * (getC))) * Mathf.Rad2Deg;
}
//getAngle = Mathf.Asin(getA * Mathf.Sin(90) / getC);
//Debug.Log("A: " + getA + " B: " + getB + " C: " + getC + " Angle: " + getAngle);
}
// Set Rotating Angle
currentRotation = transform.eulerAngles;
getRotation = new Vector3(45f, getAngle, 0f);
}
// Move Camera
transform.position = new Vector3(camX, 10.5f, camZ);
//Rotate Camera
//transform.eulerAngles = getRotation;
transform.eulerAngles = Vector3.MoveTowards(currentRotation, getRotation, speed * Time.deltaTime);
}
}
让我详细说明一下代码。当鼠标离它最近时,我希望相机朝向原点,反之亦然。我在这方面遇到了一些麻烦,因为我认为我设置不正确。我试图解决这个问题的尝试是使用反向逻辑(即当鼠标靠近原点时,相机距离原点最远 [鼠标 -> 对象 -> 相机)。如果鼠标是离原点最远 [相机 -> 对象 -> 鼠标]。)
既然你知道我遇到的一些问题,下面是我需要回答的问题(我希望你写一个解决方案并告诉我它是如何解决问题的。):如何修复相机旋转以便鼠标移动它会立即旋转,如何指定鼠标是否靠近原点?
更新: 我将所有代码移至 LateUpdate,因为那是您应该执行相机算法的时间。我还 "tried" 简化了鼠标定位部分,所以它不是那么多余。我还有一个 link 到我的项目的屏幕截图:https://gyazo.com/2f5ee8f94c0bc55bbdeaeafa8a8b35f4
我找到了解决方案:
transform.rotation = Quaternion.RotateTowards(transform.rotation, getRotation, speed * Time.deltaTime);