【问题标题】:Unity change camera position with value from another script with functionsUnity使用另一个脚本的值更改相机位置
【发布时间】:2016-01-17 14:03:18
【问题描述】:

我对 Unity 还是很陌生,我正在尝试理解为什么这不起作用。我想要做的是将带有 Vector3.Lerp 的相机从当前相机位置移动到对象位置。我想从两个脚本中的函数中做到这一点。因此,我有两个脚本,一个用于相机,另一个用于 Cube。

这是相机的脚本

using UnityEngine;
using System.Collections;

public class CameraScript : MonoBehaviour {

    private Vector3 positionCamera;
    public Camera camera;

    void Start () {
        camera = GetComponent<Camera>();
        positionCamera = camera.transform.position;
    }

    void Update () {
        GoToTarget();
    }

    public void GoTo(Vector3 position)
    {
        positionCamera = Vector3.Lerp(positionCamera, position, Time.deltaTime);
    }

    public void GoToTarget()
    {
        Vector3 newpos = positionCamera;
        camera.transform.position = newpos;
    }
}

还有 Cube 的脚本

using UnityEngine;
using System.Collections;

public class CubeScript : MonoBehaviour {


    public CameraScript cameraScript;
    private GameObject cube;
    private Vector3 cubePosition;
    private Vector3 newPosition;

    void Start () {
        cameraScript = new CameraScript();

        cube = this.gameObject;

        cubePosition = cube.transform.position;
        newPosition = transform.position;

    }

    // Update is called once per frame
    public void Update () {
        UpdatePosition();
    }

    public void UpdatePosition()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            cameraScript.GoTo(newPosition);
        }

    }
}

我从立方体脚本传递位置并将其发送到相机脚本中的 GoTo 函数。然后相机应该使用 Vector3.Lerp 将相机移动到立方体的位置。然而这不起作用。

我怎样才能使它与函数一起工作? 我是否为相机正确使用 translate.position ?还是我应该先分配一个游戏对象?

谢谢

【问题讨论】:

    标签: unity3d camera position transform


    【解决方案1】:

    我在 CubeScript 中进行了编辑,并在按下空格键时检查了它对我来说是否正常工作。我为我更改的内容编写了 cmets...通过它...

    using UnityEngine;
    using System.Collections;
    
    public class CubeScript : MonoBehaviour {   
    
        [SerializeField]
        private CameraScript cameraScript = null;// Changes here
    
        private GameObject cube;
        private Vector3 cubePosition;
        private Vector3 newPosition;
    
        void Start () {
            if (this.cameraScript == null) {
                this.cameraScript = this.gameObject.GetComponent<CameraScript>(); // Changes here
            }
    
            cube = this.gameObject;
    
            cubePosition = cube.transform.position;
            newPosition = transform.position;
        }
    
        // Update is called once per frame
        public void Update () {
            UpdatePosition();
        }
    
        public void UpdatePosition()
        {
            if (Input.GetKey(KeyCode.Space))
            {
                cameraScript.GoTo(newPosition);
            }
        }
    }
    

    有不同的方法可以调用函数或变量等,从一个脚本到另一个脚本,check here 和我在此question 中的评论以获取更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多