在Unity 2D中移动简单对象 [英] Move simple Object in Unity 2D

查看:73
本文介绍了在Unity 2D中移动简单对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Unity中移动简单的Object,但收到以下错误消息:

I'm trying to move a simple Object in Unity but I get the following error message:

cannot modify the return value of unityengine.transform.position because itar is not variable

这是我的代码:

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值,因为它是从属性获取器返回的值类型. (请参阅:无法修改返回值错误c#)

You can't assign the x value on position directly as it's a value type returned from a property getter. (See: Cannot modify the return value error c#)

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

Instead, you need to assign a new Vector3 value:

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

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

Or if you're keeping most of the coordinate values the same, you can use the Translate method instead to move relatively:

transform.Translate(movespeed, 0, 0)

这篇关于在Unity 2D中移动简单对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆