ひつじを動かそう のバックアップソース(No.7)

#contents
*はじめに [#x7454d9b]
&size(18){[[Unity 初学者のすゝめ]]};の続きです。
#br
「[[Unity 初学者のすゝめ]]」では重力操作や当たり判定などの簡単な物理演算による操作を勉強しました。
しかし、さらに複雑な操作をしようと思うとプログラミングでの操作が必要になります。
そこで、このページではUnity上でのC#のプログラミングの基本を勉強します。
#br
*ひつじをC#で動かしてみる [#a2e972d3]
[[前章>Unity 初学者のすゝめ]]
AddComponent>NewScriptを押します。そして、
すると、プロジェクトのアセットに「text.cs」というのが追加されました。
これをダブルクリックします。
#br
#geshi(csharp,number){{
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

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

    }
}
}}

#br
このようなファイルが開いたかと思います。
ここにいろいろ書き足して、実際にひつじを動かしてみましょう。
#br
#geshi(csharp,number){{
…
    void Start()
    {
        Vector3 pos = this.transform.position;

        pos.x = 3;
        pos.y = 4;

        this.transform.position = pos;
    }
…
}
}}
#br
「void Start()」の後に続く「{}」の中に上の通りに書き込んでください。
#br
少し説明すると、

#geshi(csharp,number){{
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    { 
        Vector3 pos = this.transform.position;

        pos.x += 0.01f;
        pos.y += 0.01f;

        this.transform.position = pos;
    }
}
}}

#geshi(csharp,number){{
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    public float speed = 0.01f;

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    { 
        Vector3 pos = this.transform.position;

        pos.x += speed;
        pos.y += speed;

        this.transform.position = pos;
    }
}
}}

#geshi(csharp,number){{
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    public float speed = 0.01f;

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    { 
        if (this.transform.position.x > 5 || this.transform.position.x < -5)
        {
            speed = -speed;
        }

        Vector3 position = this.transform.position;
        position.x += speed;
        position.y += speed;
        this.transform.position = position;
    }
}
}}