ランスタのブログ

気になった事の実験や検証、Cinema4Dでのモデリングや使い方、Unityのゲームアプリ制作に関する記事を書いています。

【スポンサーリンク】

【Unity】キーボードのキー操作でCubeオブジェクトを移動、回転、大きさを変更するやり方【使い方】

【スポンサーリンク】

キーボードのキー操作を使用してC#スクリプトでCubeオブジェクトを移動、回転、大きさを変える方法

 

まずは新規でプロジェクトを作成して下さい。
今回はCubeを移動しているのを分かるようにさせる為、ヒエラルキーにあるMain Cameraの位置と角度を少し斜めにさせます。
 
まずはMain Cameraオブジェクトをクリックして、PositionのYを4にして、RotationのXを10にして下さい。

f:id:Lanstar:20170528133302p:plain

 
その後、メニューからGameObject→3D Object→Cubeを生成して下さい。
Cubeを作成したら次はC#スクリプトを作成します。
 
ProjectのCreateボタンからC# Scriptを選択して、名前をCubeにします。
そしたらC# ScriptをダブルクリックしてMonoDevelopを起動させます。
 
今回はキーボードのWASDを使用してCubeを移動させるコードを書いて行きます。
Wキーで前進、Dキーで後退、Dキーで右移動、Aキーで左移動させます。
起動したら以下のコードを付け足して記入して下さい。(間違えないように注意して下さい。間違えるとエラーが表示されます)
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
using UnityEngine;
using System.Collections;
public class Cube : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.W)){
transform.position += new Vector3(0,0,1 * Time.deltaTime);
}
if(Input.GetKey(KeyCode.S)){
transform.position += new Vector3(0,0,-1 * Time.deltaTime);
}
if(Input.GetKey(KeyCode.D)){
transform.position += new Vector3(1 * Time.deltaTime,0,0);
}
if(Input.GetKey(KeyCode.A)){
transform.position += new Vector3(-1 * Time.deltaTime,0,0);
}
}
}
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
 
書いたらビルド(commandキー+B)してUnity側にスクリプトの情報を反映させます。
そしてUnity側に戻りCubeと書かれたC#スクリプトをクリックしたままヒエラルキーにあるCubeオブジェクトにドラッグ&ドロップしてCubeにスクリプトを着けます。
 
次に真ん中上にある再生ボタンをクリックします。
キーボードのWを押し続けるとZ軸に前進します。
キーボードのSを押し続けるとZ軸に後退します。
キーボードのDを押し続けるとX軸に右移動します。
キーボードのAを押し続けるとX軸に左移動します。
 
WとDなど2つ同時にキーを押すと斜めに移動する事も出来ます。
 
次は回転させるコードを書きたいと思います。
回転はTFGHのキーを使用して回転させます。
Tキーは前に回転
Fキーは後ろに回転
Gキーは左に回転
Hキーは右に回転
 
先ほど書いた移動のコードは消さずにそのままで以下のコードを確認して新たに付け足して下さい。
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
using UnityEngine;
using System.Collections;
public class Cube : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.W)){
transform.position += new Vector3(0,0,1 * Time.deltaTime);
}
if(Input.GetKey(KeyCode.S)){
transform.position += new Vector3(0,0,-1 * Time.deltaTime);
}
if(Input.GetKey(KeyCode.D)){
transform.position += new Vector3(1 * Time.deltaTime,0,0);
}
if(Input.GetKey(KeyCode.A)){
transform.position += new Vector3(-1 * Time.deltaTime,0,0);
}
//↓ここから回転コードを付け足しています
if(Input.GetKey(KeyCode.T)){
transform.Rotate(50 * Time.deltaTime,0,0);
}
if(Input.GetKey(KeyCode.G)){
transform.Rotate(-50 * Time.deltaTime,0,0);
}
if(Input.GetKey(KeyCode.H)){
transform.Rotate(0,0,-50 * Time.deltaTime);
}
if(Input.GetKey(KeyCode.F)){
transform.Rotate(0,0,50 * Time.deltaTime);
}
}
}
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
 
記入したらビルドしてUnity側に戻り、再生ボタンを押してそれぞれのキーを確認してみて下さい。
次は大きさを変えるコードを書いて行きます。
 
移動と回転させるコードはそのまま消さずに大きさを変えるコードを付け足します。
使用するキーはIJKLで、
Iキーは前と後ろが伸びて行きます。
Kキーは前と後ろが小さくなります。
Lキーは右と左が伸びて行きます。
Jキーは右と左が小さくなります。
 
では以下のコード確認して付け足して下さい。
---------------------------------------------------------
using UnityEngine;
using System.Collections;
public class Cube : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.W)){
transform.position += new Vector3(0,0,1 * Time.deltaTime);
}
if(Input.GetKey(KeyCode.S)){
transform.position += new Vector3(0,0,-1 * Time.deltaTime);
}
if(Input.GetKey(KeyCode.D)){
transform.position += new Vector3(1 * Time.deltaTime,0,0);
}
if(Input.GetKey(KeyCode.A)){
transform.position += new Vector3(-1 * Time.deltaTime,0,0);
}
if(Input.GetKey(KeyCode.T)){
transform.Rotate(50 * Time.deltaTime,0,0);
}
if(Input.GetKey(KeyCode.G)){
transform.Rotate(-50 * Time.deltaTime,0,0);
}
if(Input.GetKey(KeyCode.H)){
transform.Rotate(0,0,-50 * Time.deltaTime);
}
if(Input.GetKey(KeyCode.F)){
transform.Rotate(0,0,50 * Time.deltaTime);
}
//↓から大きさを変えるコードです。付け足して下さい
if(Input.GetKey(KeyCode.I)){
transform.localScale += new Vector3(0,0,1 * Time.deltaTime);
}
if(Input.GetKey(KeyCode.K)){
transform.localScale += new Vector3(0,0,-1 * Time.deltaTime);
}
if(Input.GetKey(KeyCode.L)){
transform.localScale += new Vector3(1 * Time.deltaTime,0,0);
}
if(Input.GetKey(KeyCode.J)){
transform.localScale += new Vector3(-1 * Time.deltaTime,0,0);
}
}
}
-------------------------------------------------------------------------
 
記入したらビルドして、Unity側に戻って再生ボタンを押し、各キー確認して下さい。
 
どうでしょう
 
出来たー!
 
次回はボタンを作成してオブジェクトを移動させる事を書きたいと思います。
 

 

【スポンサーリンク】