Quantcast
Channel: かずきのBlog@hatena
Viewing all articles
Browse latest Browse all 1387

Unityで空中浮遊するものを操作したい

$
0
0

上下と前後左右に移動できて、ちょっとふわふわして、移動方向に少し傾くビヘイビア

using UnityEngine;
using System.Collections;

publicclass FlyingObject : MonoBehaviour
{
    privateconstfloat G = 9.9f;

    // Use this for initializationvoid Start()
    {

    }

    // Update is called once per framevoid Update()
    {
        var velocity = new Vector3(0, 0, 0);
        velocity.x += Input.GetAxis("Horizontal");
        velocity.y += Input.GetAxis("Vertical");
        if (Input.GetButton("Fire1"))
        {
            velocity.z += 1.0f;
        }
        if (Input.GetButton("Fire2"))
        {
            velocity.z += -1.0f;
        }

        this.rigidbody.velocity = velocity * 500 * Time.deltaTime + new Vector3(0, G + Random.Range(0.0f, 10.0f), 0) * Time.deltaTime;
        var originalRotation = this.rigidbody.rotation;
        var zEular = 0.0f;
        if (this.rigidbody.velocity.x > 0)
        {
            zEular = -5.0f;
        }
        elseif (this.rigidbody.velocity.x < 0)
        {
            zEular = 5.0f;
        }

        var xEular = 0.0f;
        if (this.rigidbody.velocity.z > 0)
        {
            xEular = 5.0f;
        }
        elseif (this.rigidbody.velocity.z < 0)
        {
            xEular = -5.0f;
        }
        var eular = new Vector3(xEular, 0, zEular);
        this.rigidbody.rotation = Quaternion.Euler(eular);
    }
}

rotationのQuaternion型を一回Vector3に戻して角度を再設定してるところとかポイント?


Viewing all articles
Browse latest Browse all 1387

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>