LoginSignup
0
0

More than 5 years have passed since last update.

unity > トルクゲー > 回転3くらいで着地せよ > Mathf.Abs() / Mathf.Epsilon / Ridigbody.angularVelocity.y / AddForce() / AddTorque()

Last updated at Posted at 2015-08-01
動作確認
Unity 5.1.2 on MacOS X

トルクを調整してPlaneにCubeを着地させるゲーム。

code

CubeControl.cs
using UnityEngine;
using System.Collections;

public class CubeControl : MonoBehaviour {

    public Rigidbody myRigid;
    private int score = 0;
    private bool gameEnd = false;

    void Judge() {
        float torq;
        torq = Mathf.Abs( myRigid.angularVelocity.y );
        print (torq.ToString ());
        if (Mathf.Abs(torq - 3.0f) > Mathf.Epsilon) {
            score = (int)(100000.0f / Mathf.Abs(torq - 3.0f));
        } else {
            score = 10000000;
        }
    }

    void OnCollisionEnter() {
        gameEnd = true;
        Judge ();
    }

    void OnGUI() {
        Rect rect1 = new Rect (10, 10, 100, 30);
        GUI.TextField (rect1, score.ToString ());
    }

    void Update () {
        if (gameEnd) {
            return;
        }
        if (Input.GetKey (KeyCode.UpArrow)) {
            myRigid.AddForce(Vector3.up);
        }
        if (Input.GetKey (KeyCode.DownArrow)) {
            myRigid.AddForce(Vector3.down);
        }

        if (Input.GetKey(KeyCode.LeftArrow)) {
            myRigid.AddTorque(Vector3.up);
        }
        if (Input.GetKey(KeyCode.RightArrow)) {
            myRigid.AddTorque(Vector3.down);
        }   
    }
}

準備

  1. Planeを追加 (Y: -0.5)
  2. Cubeを追加 (Y: 2.0)
  3. CubeにRigidBodyを追加 (Use Gravity:OFF)
  4. Cubeに上記スクリプトを関連づける

遊び方

上下カーソルでCubeが上下移動。
左右カーソルでCubeが左右回転。
左右回転をさせた状態でPlaneに着地させることで、判定が行われる。
トルクの絶対値が3付近で高得点。

0
0
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
0