LoginSignup
0
2

More than 5 years have passed since last update.

unity > ジャンプゲー > Bounciness / Instantiate() / グラフ描画 / Is Kinematic / List<> / Random.Range(A,B);

Last updated at Posted at 2015-08-02

準備

Sphereを作る
- Rigidbodyをつける
- Mass: 0.001
- Use Gravity: ON
- position > Y: 0

Sphere ColliderにPhysics Materialをつける
- 名前: Bouncy
- Bounciness: 0.9
- Friction Combine: Minimum
- Bounce Combine: Maximum

Cubeを作る (Prefabにするもの)
- enemyという名前にする
- Rigidbodyをつける
- Mass: 1
- Use Gravity: OFF (Sphereとぶつかった時に止まらないように)
- Is Kinematic: ON
- Prefabにする
- deleteする

Planeを作る
- position > Y: -0.5

code

Main Cameraに以下の.csを関連づける

GenerateLR.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic; // for List<>

public class GenerateLR : MonoBehaviour {

    public GameObject enemyPrefab;
    public Rigidbody PlayerRig;
    private int score = 0;

    // line draw
    private GameObject lineGroup; // for grouping
    private List<Vector2> my2DPoint; // for 2D data
    private float xstep = 0.2f;

    private float accumTime_sec = 0.0f;
    private float enemyTime_sec = 0.0f;
    private GameObject enemyObjPri = null;
    private bool gameEnd = false;

    void DrawLine(List<Vector2> my2DVec, int startPos) {
        List<Vector3> myPoint = new List<Vector3>();
        for(int pos = startPos; pos <= startPos+1; pos++) {
            myPoint.Add(new Vector3(my2DVec[pos].x, my2DVec[pos].y, 0.0f));
        }

        GameObject newLine = new GameObject ("Line" + startPos.ToString() );
        LineRenderer lRend = newLine.AddComponent<LineRenderer> ();
        lRend.SetVertexCount(2);
        lRend.SetWidth (0.1f, 0.1f);
        lRend.SetPosition (0, myPoint[0]); // start
        lRend.SetPosition (1, myPoint[1]); // end

        newLine.transform.parent = lineGroup.transform; // for grouping
    }

    void initData() {
        my2DPoint = new List<Vector2> ();
        for (int idx=0; idx<100; idx++) {
            my2DPoint.Add (new Vector2 (-10 + xstep * idx, Random.Range(0.0f, 5.0f)));
        }
    }

    void updateData() {
        my2DPoint.RemoveAt (0); // remove 1st point

        for (int idx=0; idx < my2DPoint.Count; idx++) { // shift x position 
            Vector2 vec = new Vector2(my2DPoint[idx].x - xstep, my2DPoint[idx].y);
            my2DPoint[idx] = vec;
        }
        if (enemyObjPri != null) {
            Vector3 pos = enemyObjPri.transform.position;
            Vector3 vec = new Vector3(pos.x - xstep, pos.y, pos.z);
            if (vec.x < -5.0) {
                Destroy(enemyObjPri);
            } else {
                enemyObjPri.transform.position = vec;
            }
        }

        int last = my2DPoint.Count;
        float ypos = PlayerRig.transform.position.y / 4.0f; // to [0.0, 5.0]
        if (ypos < -1.0f) {
            gameEnd = true;
        }
        my2DPoint.Add (new Vector2 (-10 + xstep * last, ypos));
    }

    void Start () {
        initData ();
    }

    void OnGUI() {
        Rect rect1 = new Rect (10, 10, 100, 30);
        GUI.TextField (rect1, "score:" + score.ToString());
        if (gameEnd) {
            Rect rect2 = new Rect (10, 40, 100, 30);
            GUI.TextField (rect2, "GAME END");
        }
    }

    void CheckScore() {
        if (enemyObjPri == null) {
            return;
        }
        Vector3 player1 = PlayerRig.transform.position;
        Vector3 enemy1 = enemyObjPri.transform.position;

        if (Mathf.Abs (player1.x - enemy1.x) < 0.5f) {
            float diff = Mathf.Abs (player1.y - enemy1.y);
            score += (int)(100.0f / diff );
        }
    }

    void Update () {
        if (gameEnd) {
            return;
        }

        if (Input.GetKey (KeyCode.UpArrow)) {
            PlayerRig.AddForce(Vector3.up * 0.05f);
        }

        enemyTime_sec += Time.deltaTime;
        if (accumTime_sec < 0.3f) { // each 300 msec
            accumTime_sec += Time.deltaTime;
            return;
        }
        accumTime_sec = 0.0f;

        if (enemyTime_sec > 5.0f && enemyObjPri == null) {
            enemyTime_sec = 0.0f;
            enemyObjPri = Instantiate(enemyPrefab, new Vector3(5.0f, 0.0f, 0.0f), Quaternion.identity) as GameObject;
        }

        CheckScore ();
        updateData ();

        if (lineGroup != null) {
            Destroy(lineGroup);
        }
        lineGroup = new GameObject ("LineGroup");
        for (int idx=0; idx < my2DPoint.Count - 1; idx++) {
            DrawLine (my2DPoint, /* startPos=*/idx);
        }
    }
}

Main Camera > Generate LR (Script)
- Enemy Prefab : enemyを関連付ける
- Player Rig : Sphere(Rigidbody)を関連付ける

遊び方

カーソルキー上: Sphereを上昇させる。
定期的にCubeが右から来る。Cubeに跳ね飛ばされないようにSphereを上昇させる。
Cubeに近い時にCubeとの高さが近いとScoreが大きく加算される。
跳ね飛ばされてPlaneから落ちたらゲームエンド。

Untitled_-_150802_lineRenderer_-_WebGL__Personal_.jpg

公開 

以下で実行できます。(Unity web playerが必要です)
http://unitygameuploader.jpn.org/game/5754.html

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2