オブジェクトのポジションの変更
JavaScriptのような以下の書き方はC#だと使えないようです。
transform.position.x += 0.1f;
transform.position
に入れる値はVector3
という型に揃えないといけません。
Vector3
はUnityEngine三次元ベクトルを扱うための型です。
Unity - スクリプトリファレンス: Vector3
なので、以下のように動かします。
public class PlayerScript : MonoBehaviour { void Start () { } void Update () { //Vector3という型に一旦値を入れて、計算して入れ直す Vector3 pos = transform.position; pos.x += 0.1f; transform.position = pos; //現在のpositionに値を加えたものを入れ直す Vector3 pos = transform.position; transform.position = new Vector3(pos.x + 0.1f, pos.y, pos.z); //Vector3という値を別に作り、追加する transform.position += new Vector3(0.1f, 0.0f, 0.0f); //transform.Translateを使う transform.Translate(0.1f, 0, 0); } }
どのコードも、xを0.1ずつ移動するというコードです。
もしくはtransform.Translate
を使用する方法があります。
参考URL qiita.com docs.unity3d.com