//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後のオブジェクトを参照してエラーがコンソールに表示されてしまうので、そこは確認中である。