Как заставить камеру вращаться вокруг игрока в перспективе от третьего лица в Unity? - PullRequest
1 голос
/ 31 марта 2019

Я пытаюсь заставить камеру вращаться вокруг плеера, чтобы игрок всегда находился в центре экрана.

Я пытался использовать функцию Slerp().

using UnityEngine;
using System.Collections;


public class rotate : MonoBehaviour
{
    public Transform target;

    public float Speed = 1f;
    public Camera cam;
    public Vector3 offset;
    void Update()
    {
        Vector3 direction = (target.position - cam.transform.position).normalized;


        Quaternion lookrotation = target.rotation;
        Quaternion playerrotation = target.rotation;

        playerrotation.y = target.rotation.y;
        playerrotation.x = 0f;
        playerrotation.z = 0f;

        lookrotation.x = transform.rotation.x;
        lookrotation.z = transform.rotation.z;
        //lookrotation.y = transform.rotation.y;

        offset.x = -target.rotation.x * Mathf.PI;


        transform.rotation = Quaternion.Slerp(transform.rotation, playerrotation, Time.deltaTime * Speed);

        transform.position = Vector3.Slerp(transform.position, target.position + offset, Time.deltaTime * 10000);
    }
}

Это сработало, но плеер не был в центре экрана.

1 Ответ

0 голосов
/ 31 марта 2019

Следующий скрипт будет вращать gameObject, к которому он прикреплен, так, чтобы Target gameObject находился в центре экрана и камера смотрела в том же направлении, что и цель.

public Transform Target;
public float Speed = 1f;
public Vector3 Offset;
void LateUpdate()
{
    // Compute the position the object will reach
    Vector3 desiredPosition = Target.rotation * (Target.position + Offset);

    // Compute the direction the object will look at
    Vector3 desiredDirection = Vector3.Project( Target.forward, (Target.position - desiredPosition).normalized );

    // Rotate the object
    transform.rotation = Quaternion.Slerp( transform.rotation, Quaternion.LookRotation( desiredDirection ), Time.deltaTime * Speed );

    // Place the object to "compensate" the rotation
    transform.position = Target.position - transform.forward * Offset.magnitude;
}

Примечание: Никогда, никогда никогда не манипулируют компонентами кватерниона, если вы действительно не знаете, что делаете . Кватернионы не хранят значения, которые вы видите в инспекторе. Это сложные объекты, используемые для представления поворотов, с 4 значениями.

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