code ソーシャル

Unityアプリcodetips(16)

スポンサーリンク

//tips

//terrain上をtransform.Translateを使用し、getaxisでplayerを移動させる

Rigidbodyを使用してしまうと余計な物理演算でプレイヤーがうまく操作できなくなることがあるため、transform.Translateかつボタンの押し具合を数値で入力し、操作するgetaxisで操作する。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{

void Start()
{

}

void FixedUpdate()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
this.gameObject.transform.Translate(x, 0, z);
}
}

//terrain上をtransform.Translateを使用し、Input.GetKeyでplayerを移動させる

terrainをヒトマスずつ動かしたい場合は、実行次に1 or -1を返すInput.GetKeyの方が便利なのでそちらの振る舞いも見ていく。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController2 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

// 左に移動
if (Input.GetKey(KeyCode.LeftArrow))
{
this.transform.Translate(-1.0f, 0.0f, 0.0f);
}
// 右に移動
if (Input.GetKey(KeyCode.RightArrow))
{
this.transform.Translate(1.0f, 0.0f, 0.0f);
}
// 前に移動
if (Input.GetKey(KeyCode.UpArrow))
{
this.transform.Translate(0.0f, 0.0f, 1.0f);
}
// 後ろに移動
if (Input.GetKey(KeyCode.DownArrow))
{
this.transform.Translate(0.0f, 0.0f, -1.0f);
}
}
}

これはfpsの中で何度も判定を得ているので必ずしも1マスずつの移動にはならないことがわかる。
スクリプトを下記のように変えても同じ。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController3 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
Transform myTransform = this.transform;

// 左に移動
if (Input.GetKey(KeyCode.LeftArrow))
{
myTransform.Translate(-1.0f, 0, 0, Space.World);
}
// 右に移動
if (Input.GetKey(KeyCode.RightArrow))
{
myTransform.Translate(1.0f, 0, 0, Space.World);
}
// 前に移動
if (Input.GetKey(KeyCode.UpArrow))
{
myTransform.Translate(0, 0, 1.0f, Space.World);
}
// 後ろに移動
if (Input.GetKey(KeyCode.DownArrow))
{
myTransform.Translate(0, 0, -1.0f, Space.World);
}
}
}

//Input.GetKeyDownを使用して判定をクリックした瞬間の一回に絞り、アクションさせる

Input.GetKeyDownを使用することで判定をクリックした瞬間の一回に絞ることができるので、マス目操作には便利。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController4 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame

void Update()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
transform.Translate(-1, 0, 0);
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
transform.Translate(1, 0, 0);
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
transform.Translate(0, 0, 1);
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
transform.Translate(0, 0, -1);
}

}
}

//PlayerCubeとEnemyCubeの接触によるdestroy

1マスずつ移動するPlayerCubeが静止しているEnemyCubeと同じマスに来たときにPlayerCubeをdestroyするプログラムを組む。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyController : MonoBehaviour
{
GameObject Player;

void Start()
{
this.Player = GameObject.Find("Player");
}

void Update()
{
Vector3 p1 = transform.position;
Vector3 p2 = this.Player.transform.position;

if (p1 == p2)
{
if (Player != null)
{
Destroy(Player);
}
}

}
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController4 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame

void Update()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
transform.Translate(-1, 0, 0);
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
transform.Translate(1, 0, 0);
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
transform.Translate(0, 0, 1);
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
transform.Translate(0, 0, -1);
}

}
}

動作は問題なく行われるが、nullを組み込んでも、destroy後のオブジェクトを参照してエラーがコンソールに表示されてしまうので、そこは確認中である。

人気の記事

1

皆さん、ついに、エアラインでも、サブスクリプションが始まったのはご存じですか? まだ実験段階ですが、ANAが、定額全国住み放題サービスを提供する「ADDress」と組んで、国内線を4回まで定額利用可能 ...

2

無料でネットショップを開けるアプリとして多くの人に驚きを与えたBASE株式会社が、2019年10月25日東証マザーズに上場しました。2020年2月時点で90万店を超えるショップを抱えるまでに成長してい ...

3

2011年にサービスを開始してから圧倒的な成長率を誇るインテリア通販サイト 【FLYMEe/フライミー】を皆さんご存じでしょうか。 「自分のイメージするインテリア、本当に欲しいインテリアがどこにあるの ...

4

ついに、noteの月間アクティブユーザー数が4400万人(2020年3月時点)に到達しました。 そもそも、「note」とは、クリエイターが、文章やマンガ、写真、音声を投稿することができ、ユーザーはその ...

5

ボードゲームカフェが1日2回転で儲かるという記事をみつけたので興味を持ち、調べてみました。 まずは、需要がどれくらいあるのか、市場のようすからみていきましょう。 世界最大のボードゲーム市場はドイツで、 ...

-code, ソーシャル
-,

Copyright© BUSINESS HUNTER , 2023 All Rights Reserved Powered by AFFINGER5.