Проблема с основной камерой и плеером на сферической поверхности - PullRequest
0 голосов
/ 26 августа 2018

Я пытаюсь создать круговой мир, в котором игрок может двигаться, код игрока:

public class Player : MonoBehaviour {

    public float speed = 10f;
    private Rigidbody rb;

    private float horizontal = 0f;
    private float vertical = 0f;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update () {
        horizontal = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
        vertical = Input.GetAxis("Vertical") * speed * Time.deltaTime;
    }

    private void FixedUpdate()
    {
        Vector3 origin = Vector3.zero;

        Quaternion hq = Quaternion.AngleAxis(-horizontal, Vector3.up);
        Quaternion vq = Quaternion.AngleAxis(vertical, Vector3.right);

        Quaternion q = hq * vq;

        rb.MovePosition(q * (rb.transform.position - origin) + origin);
        transform.LookAt(Vector3.zero);
    }
}

и работает отлично, сейчас я пытаюсь создать камеру, которая следует за игроком по кругумир, но не работает это:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class seguid : MonoBehaviour {

    public Transform PlayerTransform;
    private Vector3 _cameraOffset;

    [Range(0.01f, 1.0f)]
    public float SmoothFactor = 0.5f;

    // Use this for initialization
    void Start () {
        _cameraOffset = transform.position - PlayerTransform.position;
    }

    // Update is called once per frame
    void Update () {
        Vector3 newPos = PlayerTransform.position + _cameraOffset;
        transform.position = Vector3.Slerp(transform.position, newPos, SmoothFactor);
    }
}

1 Ответ

0 голосов
/ 26 августа 2018

Это должно работать.Пояснения в комментариях:

public Transform WorldCenterPosition;
...
void Start () {
    _cameraOffset = transform.position - PlayerTransform.position;
}

// Update is called once per frame
void Update () {

    // Direction from World center to Player position
    Vector3 direction = PlayerTransform.position - WorldCenterPosition.position;

    // Distance for camera to be away (distance from player to world center
    // plus distance from player to camera)
    float distance = direction.magnitude + _cameraOffset.magnitude;

    // Direction should be unit vector
    direction = direction.normalized;

    // Multiply our unit direction by calculated distance
    Vector3 newPos = WorldCenterPosition.position + (direction*distance);
    transform.position = Vector3.Slerp(transform.position, newPos, SmoothFactor);

    // I suppose camera should look at player
    transform.LookAt (PlayerTransform);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...