我有一个玩家对象,以及播放器和连接到它作为孩子的一个摄像头。
我想在周围玩家一个圆圈旋转摄像头,使其始终面向玩家(这是在0,0,0中心)。
我有一个2D的做法,我需要3D转换。
将这个脚本是什么样的3D?
谢谢。
using UnityEngine;
using System.Collections;
public class circularMotion : MonoBehaviour {
public float RotateSpeed;
public float Radius;
public Vector2 centre;
public float angle;
private void Start()
{
centre = transform.localPosition;
}
private void Update()
{
angle += RotateSpeed * Time.deltaTime;
var offset = new Vector2(Mathf.Sin(angle), Mathf.Cos(angle)) * Radius;
transform.localPosition = centre + offset;
}
}
那么,一个方法可以是定义一个向上载体中,然后围绕相应轴线旋转。
using UnityEngine;
public class circularMotion : MonoBehaviour
{
public float RotateSpeed = 1;
public float Radius = 1;
public Vector3 centre;
public float angle;
public Vector3 upDirection = Vector3.up; // upwards direction of the axis to rotate around
private void Start()
{
centre = transform.localPosition;
}
private void Update()
{
transform.up = Vector3.up;
angle += RotateSpeed * Time.deltaTime;
Quaternion axisRotation = Quaternion.FromToRotation(Vector3.up, upDirection);
// position camera
Vector3 offset = axisRotation * new Vector3(Mathf.Sin(angle), 0, Mathf.Cos(angle)) * Radius;
transform.localPosition = centre + offset;
// look towards center
transform.localRotation = axisRotation * Quaternion.Euler(0, 180 + angle * 180 / Mathf.PI, 0);
}
}