Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CubeController : MonoBehaviour {
- // 回転速度
- Vector3 velRotaion = new Vector3(0, 0, 0);
- // 進行方向をZ軸正とした速度
- Vector3 velForward = new Vector3(0, 0, 0);
- // Use this for initialization
- void Start () {
- }
- // Update is called once per frame
- void Update () {
- // キー入力(回転)
- float keyH = Input.GetAxis("Horizontal");
- float keyV = Input.GetAxis("Vertical");
- // 回転の減速
- velRotaion -= velRotaion * 3.0f * Time.deltaTime;
- if (keyH != 0) {
- // Z軸を中心に左右に傾ける
- velRotaion.z += keyH * -100.0f * Time.deltaTime;
- }
- if (keyV != 0) {
- // X軸を中心に上下に傾ける
- velRotaion.x += keyV * 100.0f * Time.deltaTime;
- }
- // 速度の減速
- velForward -= velForward * 1.0f * Time.deltaTime;
- if (Input.GetKey(KeyCode.Space)) {
- // 進行方向へ加速
- velForward.z += 50.0f * Time.deltaTime;
- }
- }
- private void FixedUpdate() {
- // プレイヤーの回転を更新
- Quaternion q = transform.rotation;
- // 回転速度を加える
- q = q * Quaternion.Euler(velRotaion * Time.deltaTime);
- transform.rotation = q;
- // プレイヤーの位置を更新
- Vector3 pos = transform.position;
- // 速度ベクトルを回転
- pos = pos + q * velForward * Time.deltaTime;
- transform.position = pos;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement