Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CubeController : MonoBehaviour {
  6.     // 回転速度
  7.     Vector3 velRotaion = new Vector3(0, 0, 0);
  8.     // 進行方向をZ軸正とした速度
  9.     Vector3 velForward = new Vector3(0, 0, 0);
  10.  
  11.     // Use this for initialization
  12.     void Start () {
  13.     }
  14.    
  15.     // Update is called once per frame
  16.     void Update () {
  17.         // キー入力(回転)
  18.         float keyH = Input.GetAxis("Horizontal");
  19.         float keyV = Input.GetAxis("Vertical");
  20.  
  21.         // 回転の減速
  22.         velRotaion -= velRotaion * 3.0f * Time.deltaTime;
  23.  
  24.         if (keyH != 0) {
  25.             // Z軸を中心に左右に傾ける
  26.             velRotaion.z += keyH * -100.0f * Time.deltaTime;
  27.         }
  28.         if (keyV != 0) {
  29.             // X軸を中心に上下に傾ける
  30.             velRotaion.x += keyV * 100.0f * Time.deltaTime;
  31.         }
  32.  
  33.         // 速度の減速
  34.         velForward -= velForward * 1.0f * Time.deltaTime;
  35.  
  36.         if (Input.GetKey(KeyCode.Space)) {
  37.             // 進行方向へ加速
  38.             velForward.z += 50.0f * Time.deltaTime;
  39.         }
  40.     }
  41.  
  42.     private void FixedUpdate() {
  43.         // プレイヤーの回転を更新
  44.         Quaternion q = transform.rotation;
  45.         // 回転速度を加える
  46.         q = q * Quaternion.Euler(velRotaion * Time.deltaTime);
  47.  
  48.         transform.rotation = q;
  49.  
  50.  
  51.         // プレイヤーの位置を更新
  52.         Vector3 pos = transform.position;
  53.         // 速度ベクトルを回転
  54.         pos = pos + q * velForward * Time.deltaTime;
  55.  
  56.         transform.position = pos;
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement