Unity 使用另一个具有函数的脚本的值更改相机位置
Unity change camera position with value from another script with functions
我是 Unity 的新手,我想了解为什么这不起作用。我想要做的是将带有 Vector3.Lerp 的相机从当前相机位置移动到对象位置。这是我想通过两个脚本中的函数来完成的。因此我有两个脚本,一个用于相机,另一个用于立方体。
这是相机的脚本
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?还是我应该先分配一个游戏对象?
谢谢
我在 CubeScript 中编辑了一点,并在按下空格键时检查它是否适合我。我为我更改的内容写了评论...通过它...
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 and my comment in this 了解更多信息。
我是 Unity 的新手,我想了解为什么这不起作用。我想要做的是将带有 Vector3.Lerp 的相机从当前相机位置移动到对象位置。这是我想通过两个脚本中的函数来完成的。因此我有两个脚本,一个用于相机,另一个用于立方体。
这是相机的脚本
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?还是我应该先分配一个游戏对象?
谢谢
我在 CubeScript 中编辑了一点,并在按下空格键时检查它是否适合我。我为我更改的内容写了评论...通过它...
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 and my comment in this