Камера Unity 2018 для слежения за объектом, но панорамирование клавишами со стрелками - PullRequest
0 голосов
/ 07 апреля 2019

Я очень новичок в Unity (началось несколько дней назад).

Я пытаюсь создать функциональность, при которой камера будет всегда следовать за вращающимся шариком, но камера должна управляться клавишами со стрелкамитакже, все еще фокусируясь на главном объекте.

У меня есть следующий код, который следует за фактическим объектом:

using UnityEngine;

public class FollowPlayer : MonoBehaviour {

    public Transform player;
    private Vector3 offset = new Vector3(0, 3, -5);

    // Update is called once per frame
    void Update() {
        transform.position = player.position + offset;
    }
}

Приведенный выше код работает отлично, и он следует за объектом при его перемещении.

Но я попробовал следующее для перемещения камеры:

using UnityEngine;

public class FollowPlayer : MonoBehaviour {

    public Transform player;
    public GameObject MainCamera;
    public Vector3 offset = new Vector3(0, 3, -5);

    private Vector3 RotateOffset;
    public float rotateJump = 1f;

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

        //Rotate Camera 90 degrees Left
        if (Input.GetKey("left")) {
            RotateOffset = new Vector3(transform.position.x - rotateJump, 0, transform.position.z - rotateJump);
            transform.position = player.position + RotateOffset;
        }

        //Rotate Camera 90 degrees Right
        if (Input.GetKey("right")) {
            RotateOffset = new Vector3(transform.position.x + rotateJump, 0, transform.position.z + rotateJump);
            transform.position = player.position + RotateOffset;
        }
    }
}

Но это только перемещает камеру по прямой линии из положения A в B, как показано ниже, без вращения:

Положение A: enter image description here

Положение B: enter image description here

Как заставить камеру следить за объектоми все еще находиться под контролем пользователя?

Заранее спасибо!


Мой код для перемещения объекта по доске выглядит так:

using UnityEngine;

public class PlayerMovement : MonoBehaviour {

    public Rigidbody rb;
    public float MovementForce = 200f;

    void FixedUpdate() {
        //Move Forward
        if (Input.GetKey("w")) {
            rb.AddForce(0, 0, MovementForce * Time.deltaTime);
        }

        //Move Left
        if (Input.GetKey("a")) {
            rb.AddForce(-MovementForce * Time.deltaTime, 0, 0);
        }

        //Move Back
        if (Input.GetKey("s")) {
            rb.AddForce(0, 0, - MovementForce * Time.deltaTime);
        }

        //Move Right
        if (Input.GetKey("d")) {
            rb.AddForce(MovementForce * Time.deltaTime, 0, 0);
        }
    }
}

Редактировать:

Я обновил свой скрипт FollowPlayer, чтобы он выглядел какКе это:

using UnityEngine;

public class FollowPlayer : MonoBehaviour {

    public Transform player;
    private Vector3 offset = new Vector3(0, 3, -5);

    private float RotateVal = 2f;

    private Vector3 VPanAmount = new Vector3(2, 0, 0);
    private Vector3 HPanAmount = new Vector3(0, 2, 0);

    private Vector3 VRotatoinOffset;
    private Vector3 HRotatoinOffset;

    private void Start() {

    }

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

        if (Input.GetKey("left")) {
            VRotatoinOffset = player.position + offset - VPanAmount;
            transform.Rotate(0, -RotateVal, 0);
        }

        if (Input.GetKey("right")) {
            VRotatoinOffset = player.position + offset + VPanAmount;
            transform.Rotate(0, RotateVal, 0);
        }

        if (Input.GetKey("up")) {
            HRotatoinOffset = player.position + offset + HPanAmount;
            transform.Rotate(-RotateVal, 0, 0);
        }

        if (Input.GetKey("down")) {
            HRotatoinOffset = player.position + offset + HPanAmount;
            transform.Rotate(RotateVal, 0, 0);
        }

        transform.position = player.position + offset - VRotatoinOffset - HRotatoinOffset;
    }
}

Камера вращается и перемещается при перемещении объекта, но фокус камеры не остается на объекте, и камера перемещается ниже игрового поля.

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