Преобразование 2D-позиции / вращения в 3D - PullRequest
0 голосов
/ 10 февраля 2019

У меня есть объект player, а игрок и камера прикреплены к нему как childs.

Я хотел бы повернуть камеру по кругу вокруг игрока так, чтобы она всегда была обращена к игроку (которыйпо центру 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;
 }
 }

1 Ответ

0 голосов
/ 11 февраля 2019

Ну, одним из подходов может быть определение вектора вверх и затем вращение вокруг соответствующей оси.

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);
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...