在Unity中围绕对象移动相机 [英] Moving camera around an object in Unity

查看:283
本文介绍了在Unity中围绕对象移动相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前的帖子似乎并没有解决我的问题.

Previous posts here did not seem to address my problem.

我正在尝试让相机在称为目标"的特定点周围移动.目标是一个空的游戏对象,位于我的游戏中心.这个想法是,相机不会靠近目标或远离目标,而只是围绕目标旋转,就好像它绕着不可见的球运动一样.相机应始终指向目标. transform.LookAt(target)可以很好地保持照相机在目标上训练,但我无法正确进行移动.无论我是沿水平轴还是垂直轴移动,它总是直接螺旋形进入目标,而不仅仅是围绕目标移动.有什么想法吗?

I'm trying to get my camera to move around a specific point called "target". Target is an empty gameobject set at the center of my game. The idea is that the camera would not move any closer to or farther from target and would simply rotate around the target as though it were moving around an invisible sphere. The camera should always point at target. transform.LookAt(target) does just fine keeping the camera trained on the target, but I cant get the movement correct. Whether I'm moving along the horizontal or vertical axes, it always spirals directly into the target rather than just moving around it. Any ideas?

public class CameraController : MonoBehaviour {

public float speed;
public Transform target;

void Update () {
    transform.LookAt(target);

    if(Input.GetAxis("Vertical") != 0)
    {
        transform.Translate(transform.up * Input.GetAxis("Vertical") * Time.deltaTime * speed); //.up = positive y
    }

    if(Input.GetAxis("Horizontal") != 0)
    {
        transform.Translate(transform.right * Input.GetAxis("Horizontal") * Time.deltaTime * speed); //.right = positive x
    }
}
}

推荐答案

要绕特定点旋转,请使用Transform.RotateAround:

To rotate around a specific point I use Transform.RotateAround:

transform.RotateAround(target.position, transform.right, -Input.GetAxis("Mouse Y") * speed);
transform.RotateAround(target.position, transform.up, -Input.GetAxis("Mouse X") * speed);

或者,如果目标移动并且希望保持相机与目标之间的距离相同,则可以使用我的answers.unity3d.com页面上的这段代码:

Or, if your target moves and you want to keep the same distance between the camera and your target, you can use this piece of code from my answers.unity3d.com page :

public class SphericalCam 
    : MonoBehaviour 
{
     public float MinDist, CurrentDist, MaxDist, TranslateSpeed, AngleH, AngleV;
     public Transform Target;

     public void Update()
     {
         AngleH += Input.GetAxis("Mouse X");
         AngleV -= Input.GetAxis("Mouse Y");
         CurrentDist += Input.GetAxis("Mouse ScrollWheel");
     }

     public void LateUpdate()
     {
         Vector3 tmp;
         tmp.x = (Mathf.Cos(AngleH * (Mathf.PI / 180)) * Mathf.Sin(AngleV * (Mathf.PI / 180)) * CurrentDist + Target.position.x;
         tmp.z = (Mathf.Sin(AngleH * (Mathf.PI / 180)) * Mathf.Sin(AngleV * (Mathf.PI / 180)) * CurrentDist + Target.position.z;
         tmp.y = Mathf.Sin(AngleV * (Mathf.PI / 180)) * CurrentDist + Target.position.y;
         transform.position = Vector3.Slerp(transform.position, tmp, TranslateSpeed * Time.deltaTime);
         transform.LookAt(Target);
     }
 }

这篇关于在Unity中围绕对象移动相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆