Unityチュートリアル大会 †2015/05/10 公式チュートリアルを使います。 用語説明等に以下のスライドを使います。 チュートリアルにて使うC#スクリプト †Ball.cs using UnityEngine; using System.Collections; public class Ball : MonoBehaviour { //int speed = 20;でも良いが //[SerializeField]をつけることでUnityのエディタ上で //変数speedの中身を直接操作できるようになる。 [SerializeField]int speed; // はじめに一度だけよばれるとこ void Start () { //directionは方向を示す三次元ベクトル //ここではボールの正面方向の単位ベクトルtransform.forward //右方向の単位ベクトルのtransform.rightを足してspeed倍している。 Vector3 direction = (transform.forward + transform.right) * speed; //ここでボールに力を加えている。 gameObject.GetComponent<Rigidbody>().AddForce(direction,ForceMode.VelocityChange); } // 毎フレーム呼ばれるとこ void Update () { } } RacketControlelr?.cs using UnityEngine; using System.Collections; public class RacketController : MonoBehaviour { //Racketの動きの加速度を決めるパラメータ [SerializeField]float accel; //RacketのRigidbodyという部品(コンポーネント)を入れておく変数 Rigidbody rbody; // はじめに一度だけよばれるとこ void Start () { rbody = gameObject.GetComponent<Rigidbody>(); } // 毎フレーム呼ばれるとこ void Update () { //directionは動く方向を示す三次元ベクトル //Input.GetAxisRaw("Horizontal")で水平方向(A,D,←,→)の入力を受け取っている Vector3 direction = transform.right * Input.GetAxisRaw("Horizontal") * accel; rbody.AddForce(direction,ForceMode.Impulse); } } BlockController?.cs using UnityEngine; using System.Collections; public class BlockController : MonoBehaviour { //collisonは衝突という意味 //以下はこのスクリプトがくっついているものと、 //その他のものが物理的に衝突した時に呼ばれる void OnCollisionEnter(Collision collision){ //小文字から始まるgameObjectは //このスクリプトがくっついているもの(オブジェクト)を示す Destroy(gameObject); } } BottomWallController?.cs using UnityEngine; using System.Collections; public class BottomWallController : MonoBehaviour { void OnCollisionEnter(Collision collision){ //collision.gameObjectとすることで //このスクリプトがくっついているもの(オブジェクト)に //物理的に衝突してきた相手のもの(オブジェクト)を示す。 Destroy(collision.gameObject); } } HitPlaySound?.cs using UnityEngine; using System.Collections; public class HitPlaySound : MonoBehaviour { //エディタ上で選択できるようにする [SerializeField]AudioClip sound; //このスクリプトがくっついているものと衝突したとき void OnCollisionEnter(Collision collision){ //このスクリプトがくっついているオブジェクトの場所から音楽を再生する AudioSource.PlayClipAtPoint(sound,gameObject.transform.position); } } |