Pc-Trace

<< 2024年11月 >>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30






Pc-Trace
PC-Trace

< next Unityの記事 prev >

「Unity」 習うより慣れろ?

SketchUpと勝手の違う「Unity」に悪戦苦闘。

物一つまともに動かせない現状に、公式サイトのマニュアルを丁寧に読みこんで体裁を取らず慣れる事にした。
マニュアルに習って、実際にスクリプトを書いて慣れる両取り作戦。

これほど簡単なスクリプトでもエラー続出でヘトヘトになってしまう。

■先ずはボタンを配置し動かすことから。(移動のみのスクリプト添付)


■移動をボタン操作で行い、理解するための C# 練習スクリプト。添付ファイルの練習スクリプトと違い、回転を含む。

● Unity 5.2.3f1 (32-bit)

using UnityEngine;
using System.Collections;

// Button test pc-trace 2015/12/23

public class GUIBtn01 : MonoBehaviour {

public float DownLimitt = 0f;
public float Mspeed = 0.01f;
private float Movspeed = 0.02f;


void Update () {

Vector3 v = this.transform.position;
//------------回転-----------
if (Input.GetKey(KeyCode.UpArrow)){
this.transform.Rotate (1,0, 0);
}

if (Input.GetKey(KeyCode.DownArrow)){
this.transform.Rotate (-1,0, 0);
}

if (Input.GetKey(KeyCode.LeftArrow)){
this.transform.Rotate (0,-1, 0);
}

if (Input.GetKey(KeyCode.RightArrow)){
this.transform.Rotate (0,1, 0);
}
// 回転リセット
if (Input.GetKey(KeyCode.Keypad5)){
//全角度をリセット
transform.rotation = Quaternion.Euler(0, 0, 0) ;
}

// 上昇、下降 ---------------------
if (Input.GetKey(KeyCode.PageUp) ){
v.y += Mspeed;
}
if (Input.GetKey(KeyCode.PageDown) && transform.position.y >DownLimitt){
v.y -= Mspeed ;
}

this.transform.position = v;
}

// --------ボタンを使った前後左右の移動-------------

void OnGUI () {
// Make a background box
GUI.Box(new Rect(10,10,100,210), "移動");



// Make the first button. If it is pressed, Application.Loadlevel (1) will be executed

if(GUI.RepeatButton(new Rect(20,40,80,20), "← 左")) {
transform.position -= new Vector3(Movspeed, 0f, 0f);
}

if(GUI.RepeatButton(new Rect(20,70,80,20), "→ 右")) {
transform.position += new Vector3(Movspeed, 0f, 0f);
}


if(GUI.RepeatButton(new Rect(20,100,80,20), "↑ 前進")) {
transform.position += new Vector3(0f, 0f,Movspeed);
}

if(GUI.RepeatButton(new Rect(20,130,80,20), "↓ 後退")) {
transform.position -= new Vector3(0f, 0f,Movspeed);
}
//-----------回転リセット--------------------------

if(GUI.RepeatButton(new Rect(20,160,80,20), "回転リセット")) {
transform.rotation = Quaternion.Euler(0, 0, 0) ;
}
//  終了
if(GUI.Button(new Rect(20,190,80,20), "END")) {
Application.Quit();
}

}
}
//----------------end------------------

■とにかくカメラでモデルを見渡したいスクリプト。
もう、ソースの汚さが満開である。
Unity 5.2.3f1 (32-bit) キーボードとマウス兼用。
※スクリプト使用による一切のサポートと、保証はありません。

using UnityEngine;
using System.Collections;

public class MoveRotate2 : MonoBehaviour
{


public float cameraSensitivity = 90;
public float climbSpeed = 4;
public float normalMoveSpeed = 10;
public float slowMoveFactor = 0.25f;
public float fastMoveFactor = 3;

private float rotationX = 0.0f;
private float rotationY = 0.0f;

void Start ()
{
// Cursor.lockState = CursorLockMode.Confined;
Cursor.lockState = CursorLockMode.Confined;
Cursor.visible = false; //OSカーソル非表示
}

void Update ()
{
rotationX += Input.GetAxis("Mouse X") * cameraSensitivity * Time.deltaTime;
rotationY += Input.GetAxis("Mouse Y") * cameraSensitivity * Time.deltaTime;
rotationX += Input.GetAxis ("Horizontal") * cameraSensitivity * Time.deltaTime;
rotationY += Input.GetAxis ("Vertical") * cameraSensitivity * Time.deltaTime;

rotationY = Mathf.Clamp (rotationY, -90, 90);

transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);

if (Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift))
{
//transform.position += transform.forward * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis("Vertical") * Time.deltaTime;
// transform.position += transform.right * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis("Horizontal") * Time.deltaTime;

if (Input.GetKey (KeyCode.Keypad2)) {
transform.position -= transform.forward * (normalMoveSpeed * fastMoveFactor) * Time.deltaTime;
}
if (Input.GetKey (KeyCode.Keypad8)) {
transform.position += transform.forward * (normalMoveSpeed * fastMoveFactor) * Time.deltaTime;
}


if (Input.GetKey (KeyCode.Keypad4)) {
transform.position -= transform.right * (normalMoveSpeed * fastMoveFactor) * Time.deltaTime;
}
if (Input.GetKey (KeyCode.Keypad6)) {
transform.position += transform.right * (normalMoveSpeed * fastMoveFactor) * Time.deltaTime;
}


}
else if (Input.GetKey (KeyCode.LeftControl) || Input.GetKey (KeyCode.RightControl))
{
//transform.position += transform.forward * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis("Vertical") * Time.deltaTime;
//transform.position += transform.right * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis("Horizontal") * Time.deltaTime;

if (Input.GetKey (KeyCode.Keypad2)) {
transform.position -= transform.forward * (normalMoveSpeed * slowMoveFactor) * Time.deltaTime;
}
if (Input.GetKey (KeyCode.Keypad8)) {
transform.position += transform.forward * (normalMoveSpeed * slowMoveFactor) * Time.deltaTime;
}


if (Input.GetKey (KeyCode.Keypad4)) {
transform.position -= transform.right * (normalMoveSpeed * slowMoveFactor) * Time.deltaTime;
}
if (Input.GetKey (KeyCode.Keypad6)) {
transform.position += transform.right * (normalMoveSpeed * slowMoveFactor) * Time.deltaTime;
}

}
else
{
// transform.position += transform.forward * normalMoveSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
// transform.position += transform.right * normalMoveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;

if (Input.GetKey (KeyCode.Keypad2)) {
transform.position -= transform.forward * normalMoveSpeed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.Keypad8)) {
transform.position += transform.forward * normalMoveSpeed * Time.deltaTime;
}

if (Input.GetKey (KeyCode.Keypad4)) {
transform.position -= transform.right * normalMoveSpeed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.Keypad6)) {
transform.position += transform.right * normalMoveSpeed * Time.deltaTime;
}

}

//--------------------------------------------------------------
if (Input.GetMouseButton(0)) {
transform.position += transform.forward * normalMoveSpeed * Time.deltaTime;
}

if (Input.GetMouseButton(1)) {
transform.position -= transform.forward * normalMoveSpeed * Time.deltaTime;
}

if (Input.GetKey (KeyCode.Keypad8)) {
transform.position += transform.forward * normalMoveSpeed * Time.deltaTime;
}

if (Input.GetKey (KeyCode.Keypad2)) {
transform.position -= transform.forward * normalMoveSpeed * Time.deltaTime;
}


if (Input.GetKey (KeyCode.Keypad4)) {
transform.position -= transform.right * normalMoveSpeed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.Keypad6)) {
transform.position += transform.right * normalMoveSpeed * Time.deltaTime;
}

//--------------------------------------------------------------
if (Input.GetKey (KeyCode.PageUp)) {transform.position += transform.up * climbSpeed * Time.deltaTime;}
if (Input.GetKey (KeyCode.PageDown)) {transform.position -= transform.up * climbSpeed * Time.deltaTime;}
if (Input.GetKey(KeyCode.Escape)) {
Application.Quit();
}


}
}
添付ファイル 添付ファイル


< next prev >