Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在unity 3d游戏中,当玩家对象变大时,如何使相机移动位置?_C#_Unity3d - Fatal编程技术网

C# 在unity 3d游戏中,当玩家对象变大时,如何使相机移动位置?

C# 在unity 3d游戏中,当玩家对象变大时,如何使相机移动位置?,c#,unity3d,C#,Unity3d,这是我当前的脚本,它已经使摄影机跟随玩家对象。但随着比赛的进行,当比赛变得越来越大时,球员们的目标会越来越大。它变得很难看清。没有错误,我只是不知道如何修复它,我试图在网上找到它,但我不能 using UnityEngine; using System.Collections; public class CameraController : MonoBehaviour { public GameObject player; private Vector3 offset;

这是我当前的脚本,它已经使摄影机跟随玩家对象。但随着比赛的进行,当比赛变得越来越大时,球员们的目标会越来越大。它变得很难看清。没有错误,我只是不知道如何修复它,我试图在网上找到它,但我不能

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {

    public GameObject player;

    private Vector3 offset;

    void Start ()
    {
        offset = transform.position - player.transform.position;
    }

    void LateUpdate ()
    {
        transform.position = player.transform.position + offset;
    }
}

您可以使用的一种方法是根据玩家角色的大小将偏移向量缩放一些值

    void LateUpdate ()
    {
        transform.position = player.transform.position + (offset * GetAdjustmentValue());
    }

    private float GetAdjustmentValue() {

        // Some code to determine the amount you want to shift the camera
        // For example, if your player character is 50% bigger you might
        // want to move the offset 10% further out so you would return 1.1

        // By default, you would return 1, so that no scaling occurs.

        // The details of this calculation really depend on how you are changing
        // the size of your character and what specific effect you want that to have
        // on your camera position
    }

另一种方法是根据角色大小为相机设置固定位置。这些可能是空的游戏对象,它们是玩家角色的子对象。然后,当您的播放器增长时,您可以设置相机
transform.position=bigCharacterPositionObject.transform.position

当然取决于您的播放器“增长”的确切程度,但您可以执行以下操作:

void Start ()
{
    offset = (transform.position - player.transform.position) / player.transform.lossyScale.x;
}

void LateUpdate ()
{
    transform.position = player.transform.position + offset * player.transform.lossyScale.x * someAdditionalFactor;
}
因此偏移量与玩家对象的大小成线性增长。通过
someadditionalfactor
,例如,您可以说,如果播放器的大小增加一倍,则偏移量应乘以4(设置系数
2
)或仅乘以1.5(设置系数
0.5
)。。或者只使用
1
,或者不使用它


或者,您可以使用和直接在播放器的本地空间中存储和添加偏移量


这样,当玩家改变其缩放、旋转或位置时,会自动计算偏移量。

那么我应该在函数中输入什么代码?我明白你的意思,但我如何让它通过玩家的大小来计算呢?这取决于你如何改变玩家角色的大小。你只是在扩大规模吗?在你的问题中添加相应的代码,我可以给你一些选择。因此,玩家的放大方式基本上取决于它吸收的对象。如果玩家比触发函数的对象大,它吸收对象并获得其大小的1/5。我更感兴趣的是你是如何实现大小增加的(即,您是否直接操作对象的localScale,以及在操作过程中公开了哪些值)这就是我想查看代码的原因。有很多方法可以实现代码“使字符变大”问题是,我的游戏中的玩家是一个球体,通过对我的脚本进行编辑,它会在球移动时使相机旋转,因此你可能会使用上解;)你不能使用上解,因为你不能添加两个矢量3values@PikachuPlays当然,是的。。。您在代码中做了什么?;)请尝试一下^^对不起,我的意思是说你不能乘以向量3的值对不起xD@derHugo
void Start ()
{
    offset = player.transform.InverseTransformPoint(transform.position);
}

void LateUpdate ()
{
    transform.position = player.transform.TransformPoint(offset);
}