//tips
//アイテム所持
アイテムにタッチまたはクリックするとプレイヤーの特定部分に所持されるようにする。
厳密にこの動作を行わせようとするとblenderでボーンを作り込むなどのデザインの領域に入っていくため、なるべくシンプルに理解し、デザインの部分はアセット導入で対応する。
今回はプレイヤーが触ったオブジェクトをプレイヤーの子要素にポジション移動または生成する方法でアイテムの所持を考えていく。
まずはオブジェクトを子要素にする方法を考えていく。
this.transform.parentを使用してOnTriggerのタイミングで子要素に変えてみた。また子要素になったタイミングでポジションを変更することでアイテムを所持しているように変えた。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OnTriggerParent : MonoBehaviour
{
//OnTriggerStay関数
void OnTriggerStay(Collider other)
{
//接触しているオブジェクトのタグが"Player"のとき
if (other.CompareTag("Player"))
{
this.transform.parent = other.transform;
Transform myTransform = this.transform;
// 座標を取得
Vector3 pos = myTransform.position;
pos.x = 0.7f; // x座標へ加算
pos.y = 1.5f; // y座標へ加算
pos.z = -3.3f; // z座標へ加算
myTransform.position = pos; // 座標を設定
}
}
}
Playerの動きの向きも一緒に考えた方が良さそうなのでそちらも考える。
カプセルで考えると、ゲームオブジェクトの正面の向きはわかりづらいが、シーンエディタのz軸ベクトルに相当する。
下記のスクリプトで動作させてみた。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMoveR : MonoBehaviour
{
public float speed; //プレイヤーの動くスピード
private Vector3 Player_pos; //プレイヤーのポジション
private Rigidbody rigd;
void Start()
{
Player_pos = GetComponent<Transform>().position; //最初の時点でのプレイヤーのポジションを取得
rigd = GetComponent<Rigidbody>(); //プレイヤーのRigidbodyを取得
}
void FixedUpdate()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
rigd.velocity = new Vector3(x * speed, 0, z * speed); //プレイヤーのRigidbodyに対してInputにspeedを掛けた値で更新し移動
Vector3 diff = transform.position - Player_pos; //プレイヤーがどの方向に進んでいるかがわかるように、初期位置と現在地の座標差分を取得
if (diff.magnitude > 0.01f) //ベクトルの長さが0.01fより大きい場合にプレイヤーの向きを変える処理を入れる
{
transform.rotation = Quaternion.LookRotation(diff); //ベクトルの情報をQuaternion.LookRotationに引き渡し回転量を取得しプレイヤーを回転させる
}
Player_pos = transform.position; //プレイヤーの位置を更新
}
}
真っ正面にトサカをつけているはずなのにオブジェクトの正面からずれた位置を示しているので、パソコンの矢印でオブジェクトを移動させているから回転がうまく取れていないと考え、Input.GetKey(KeyCode.UpArrow)のそれぞれに違った処理を行わせるように
if (Input.GetKey(KeyCode.UpArrow))
{
m_Rigidbody.velocity = transform.forward * m_Speed;
}
として動かしたらシーンのz軸とは異なる方向に動いてしまった。
この要因はplayerのrotationの初期値がきちんと0になっていなかったからでこちらを0にすると意図したように動くようになった。
プレイヤーが棒を持っている状態でspaceキーを押したら棒が回転するアニメーションを追加する。
まずはCylinderにAnimationコンポーネントをアタッチして、回転するアニメーションクリップをコンポーネントに付与。
棒に付与しているスクリプトを下記に変更。プレイヤーが棒を取得した後、スペースボタンを押したらアニメーションが行われるようにした。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OnTriggerParent : MonoBehaviour
{
bool flag = false;
Animation anim;
private void Update()
{
if (flag)
{
if (Input.GetKeyDown(KeyCode.Space))
{
anim = this.GetComponent<Animation>();
anim.Play();
}
}
}
void OnTriggerStay(Collider other)
{
//接触しているオブジェクトのタグが"Player"のとき
if (other.CompareTag("Player"))
{
this.transform.parent = other.transform;
Transform myTransform = this.transform;
// 座標を取得
Vector3 pos = myTransform.position;
pos.x = 0.7f; // x座標へ加算
pos.y = 1.5f; // y座標へ加算
pos.z = -3.3f; // z座標へ加算
myTransform.position = pos; // 座標を設定
flag = true;
}
}
}
無事にできたので、Animationの方にもプレイヤーの向いている方向を認識させていく。