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# 如何使2D摄影机跟随脚本只朝一个方向移动 我有一个相机跟随脚本,我想让它只朝一个方向前进,而且由于某种原因,我让它开始,当目标在屏幕的中间,然后相机推动,所以玩家在边缘 using System.Collections; using System.Collections.Generic; using UnityEngine; public class StartFollow : MonoBehaviour { public Transform follow; public Transform targetObject; private Vector3 initalOffset; private Vector3 cameraPosition; private bool test; void Start() { initalOffset = follow.position - targetObject.position; } void FixedUpdate() { if (test == true) { cameraPosition = targetObject.position + initalOffset; follow.position = cameraPosition; } } void OnTriggerEnter2D(Collider2D col) { test = true; } }_C#_Unity3d - Fatal编程技术网

C# 如何使2D摄影机跟随脚本只朝一个方向移动 我有一个相机跟随脚本,我想让它只朝一个方向前进,而且由于某种原因,我让它开始,当目标在屏幕的中间,然后相机推动,所以玩家在边缘 using System.Collections; using System.Collections.Generic; using UnityEngine; public class StartFollow : MonoBehaviour { public Transform follow; public Transform targetObject; private Vector3 initalOffset; private Vector3 cameraPosition; private bool test; void Start() { initalOffset = follow.position - targetObject.position; } void FixedUpdate() { if (test == true) { cameraPosition = targetObject.position + initalOffset; follow.position = cameraPosition; } } void OnTriggerEnter2D(Collider2D col) { test = true; } }

C# 如何使2D摄影机跟随脚本只朝一个方向移动 我有一个相机跟随脚本,我想让它只朝一个方向前进,而且由于某种原因,我让它开始,当目标在屏幕的中间,然后相机推动,所以玩家在边缘 using System.Collections; using System.Collections.Generic; using UnityEngine; public class StartFollow : MonoBehaviour { public Transform follow; public Transform targetObject; private Vector3 initalOffset; private Vector3 cameraPosition; private bool test; void Start() { initalOffset = follow.position - targetObject.position; } void FixedUpdate() { if (test == true) { cameraPosition = targetObject.position + initalOffset; follow.position = cameraPosition; } } void OnTriggerEnter2D(Collider2D col) { test = true; } },c#,unity3d,C#,Unity3d,假设你想去右边: if(test&(targetObject.position+initalOffset).x>cameraPosition.x){ ... } Asher。你可以试着用这个 private void MoveCamera() { // Get the current position Vector3 newPosition = camera.transform.position; // Only modifies the x axis newPo

假设你想去右边:

if(test&(targetObject.position+initalOffset).x>cameraPosition.x){
...
}

Asher。你可以试着用这个

private void MoveCamera()
{
    // Get the current position
    Vector3 newPosition = camera.transform.position;
    // Only modifies the x axis
    newPosition.x = target.position.x;

    transform.position = newPosition;
}
或者,如果您想让相机平稳移动,请尝试查看或使用任何其他插值函数

下面是一个例子

其中
速度
变量影响相机平滑的速度


另外,我强烈建议您使用
FixedUpadte
LateUpdate
更新您的相机,以避免任何令人不快的抖动效果

您能否详细说明“我想让它只朝一个方向运行”?让什么朝一个方向走?哪一个方向?我只想让它沿着x轴移动,使它在y轴上工作。我能把x换成a吗y@AsherHeller只需添加
newPosition.y=target.position.y你应该很好
private void MoveCamera()
{
    // Get the current position
    Vector3 newPosition = camera.transform.position;
    // Only modifies the x axis
    newPosition.x = target.position.x;

    transform.position = Vector3.Lerp(transform.position, newPosition, speed * Time.deltaTime);
}