gpt4 book ai didi

c# - 在 Unity 2D 中移动简单对象

转载 作者:太空狗 更新时间:2023-10-29 18:05:54 26 4
gpt4 key购买 nike

我试图在 Unity 中移动一个简单的 Object 但我收到以下错误消息:

不能修改unityengine.transform.position的返回值,因为itar是不可变的

这是我的代码:

using UnityEngine;
using System.Collections;

public class walk : MonoBehaviour {
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

float movespeed = 0.0f;
movespeed++;
transform.position.x = transform.position.x + movespeed;

}
}

最佳答案

您不能直接在 position 上分配 x 值,因为它是从属性 getter 返回的值类型。 (参见:Cannot modify the return value error c#)

相反,您需要分配一个新的 Vector3 值:

transform.position = new Vector3(transform.position.x + movespeed, transform.position.y);

或者,如果您要保持大部分坐标值相同,则可以使用 Translate 方法来相对移动:

transform.Translate(movespeed, 0, 0)

关于c# - 在 Unity 2D 中移动简单对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22467674/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com