【问题标题】:How can a make the camera follow a rolling ball from behind?如何让相机从后面跟随一个滚动的球?
【发布时间】:2019-10-29 13:44:13
【问题描述】:

我使用的是 unity 2018.3.5f1,所以这个问题的一些解决方案不起作用。

从 Roll-A-Ball 演示项目开始,我想扩展相机脚本以跟随玩家,同时跟踪移动方向。例如:如果球开始向一边移动,我需要摄像机围绕玩家的轴旋转到他身后,然后开始跟随他。

我尝试让相机成为我要控制的球的孩子,并且我尝试制作脚本,但它无法正确跟随球,它不断旋转相机和球(我我使用这段代码并尝试修改它,因为它是唯一有效的)

这是我目前拥有的代码:

using UnityEngine;
using System.Collections;

public class CompleteCameraController : MonoBehaviour
{

    public GameObject player;        //Public variable to store a reference to the player game object


    private Vector3 offset;            //Private variable to store the offset distance between the player and camera

    // Use this for initialization
    void Start()
    {
        //Calculate and store the offset value by getting the distance between the player's position and camera's position.
        offset = transform.position - player.transform.position;
    }

    // LateUpdate is called after Update each frame
    void LateUpdate()
    {
        // Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
        transform.position = player.transform.position + offset;
    }                                                                         
}

我明白它为什么会旋转,但没有任何研究可以帮助我找出如何锁定相机,以便它一直在看球的后面

【问题讨论】:

  • 这看起来很接近我。你说相机在旋转?
  • 是的,当球移动时,相机会围绕球旋转,但我试图让相机跟随它并保持在它后面而不旋转
  • 你确定相机不是玩家的孩子吗?
  • 我确定相机不是玩家的孩子
  • @KurtisGibson 你必须确保相机旋转应该随着球的旋转而改变(尤其是 Y 轴)。正如您所提到的,当球向左或向右旋转时,相机应该在球的后面,与球的角度相同。

标签: c# unity3d


【解决方案1】:

您是否尝试过为您的相机添加一些限制?如果只是想跟着球转动,可以冻结2个轴(x和z)的位置,只让相机绕球的y轴旋转。

您可以使用 Inspector 上的限制,或在相机脚本中使用 GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotationX;

(编辑)抱歉,如果游戏对象有刚体,你会这样做,但如果它没有刚体,想法是一样的。

当您设置相机的位置或旋转时,请考虑您实际想要更改位置/旋转的哪些分量 (x,y,z)。 使用 offset = new Vector3 (offset.x, offset.y, offset.z) 并将不应更改的任何内容替换为常量。

此外,当您将相机设置为球的子级时,您可以进行设置,以便相机在每一帧都更新,使其不会随球滚动。您可以通过将代码放入将组件 x、y 或 z 设置为某个常数的相机的更新方法中来做到这一点。地平面在哪个轴上可能是正确的选择。

【讨论】:

  • 相机不旋转,我不知道如何限制相机
  • 我仍然不知道你在说什么
  • 所以它跟随它,但只从一个角度看它脚本,因为使它成为球的孩子,使其在球旋转时围绕球旋转
【解决方案2】:

如果您希望在跟随球时摄像机应遵循相同的摄像机角度(类似于 Temple Run),请尝试以下代码:

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

public class CompleteCameraController : MonoBehaviour
{
    public Transform Player;
    public float distanceFromObject = 3f;
    void Update()
    {
        Vector3 lookOnObject = Player.position - transform.position;
        lookOnObject = Player.position - transform.position;
        transform.forward = lookOnObject.normalized;
        Vector3 playerLastPosition;
        playerLastPosition = Player.position - lookOnObject.normalized * distanceFromObject;
        playerLastPosition.y = Player.position.y + distanceFromObject / 2;
        transform.position = playerLastPosition;
    }

当球向左或向右移动时,相机将跟随球移动的相同角度。

【讨论】:

  • monobehavior 和 transform 没有被识别
  • @KurtisGibson 添加using UnityEngine; using System.Collections;
  • 好的,所以它可以工作,但它像疯了一样旋转,当相机不是孩子时什么都不做,仍然到处旋转
  • 它旋转是因为它跟随球,对吧?尝试使用立方体并让相机跟随立方体。您手动移动立方体,看看相机是否抖动太多。如果是这样,那么在代码的最后一行快速使用 Lerp 函数。这将减轻相机的移动。
  • 我想扩展相机脚本以跟随玩家,同时也跟踪移动的方向。就像如果球开始向一侧移动,我需要摄像机围绕玩家的轴旋转以定位在他身后,然后开始跟随他。但不要在 z 或 x 轴上旋转相机,所以如果我向右击球并且它向右滚动,我希望相机能看到球的去向(对不起,我真的很难解释我在做什么问)
猜你喜欢
  • 1970-01-01
  • 2016-04-18
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
  • 2015-09-05
  • 2015-12-21
  • 1970-01-01
  • 2022-01-20
相关资源
最近更新 更多