繁体   English   中英

我的 Vector3 变量不适用于 Unity 的 transform.pos

[英]My Vector3 variable does not work with the transform.pos of Unity

我正在尝试将我的立方体 go 设置为他原来的 position ,即 0、0、0 但是当我运行代码时,一条错误消息告诉我

Assets\Code\GameManager.cs(18,41):错误 CS0103:当前上下文中不存在名称“originalPosition”

这是我的代码:

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

public class GameManager : MonoBehaviour
{

    public GameObject player;

    // Start is called before the first frame update
    void Start()
    {
        Vector3 originalPosition = new Vector3();
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown("r"))
        {
            player.transform.position = originalPosition;
        }
    }
}

谢谢

嗯,是的,因为整数元组和向量之间没有隐式转换。

因此,至少,您需要调用Vector3的构造函数之一:

Vector3 originalPosition = new Vector3(0,0,0);

但是由于它都是零,这和使用它的默认构造函数一样:

Vector3 originalPosition = new Vector3();

...但是等一下,那里有些可疑。 您正在声明一个局部变量,然后什么也不做。 也许您想初始化您的班级字段?

originalPosition = new Vector3();

你知道什么是当地的贵重物品吗?

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

public class GameManager : MonoBehaviour
{

    public GameObject player;
    Vector3 originalPosition;//here

    // Start is called before the first frame update
    void Start()
    {
        originalPosition = new Vector3();
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown("r"))
        {
            player.transform.position = originalPosition;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM