//tips
//カメラから例を飛ばしEthanを移動させる
AIをランダム移動させるのではなく、カメラから飛ばすレイの方向に移動させる。
Walktargetの新しいスクリプトを作成。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookMoveTo : MonoBehaviour
{
public GameObject ground;
void Update()
{
Transform camera = Camera.main.transform;
Ray ray;
RaycastHit hit;
GameObject hitobject;
Debug.DrawRay(camera.position, camera.rotation * Vector3.forward * 100.0f);
ray = new Ray(camera.position, camera.rotation * Vector3.forward);
if (Physics.Raycast(ray, out hit))
{
hitobject = hit.collider.gameObject;
if (hitobject == ground)
{
Debug.Log("Hit(x,y,z):" + hit.point.ToString("F2"));
transform.position = hit.point;
}
}
}
}
カメラがどの方向を向いているのかを読み取り、読み取った方向にレイを飛ばし、rayとgroundとの衝突位置情報を取得、そこをWalkTargetに設定する。
レイは原点 から、設定した方向に向けて光線 を無限に飛ばすことができ、
ray = new Ray(camera.position, camera.rotation * Vector3.forward);
により光線の方向を設定。
Physics.Raycastでは、飛ばすレイとレイが衝突したオブジェクトをhitがセットされる。
Debug.DrawRay()はレイをシーンに描画する。
Walk targetの位置が透明だとわかりづらいので見えるようにしていく。
Walk targetの子要素にCylinderを作成。 見やすいサイズにscaleを変更し、インスペクターのcapture colliderも外しておく。
さらに、不要なmesh rendererでcash shadows, receive shadows, use light probes, reflectionをオフにし、マテリアルで色付けする。
目印をtargetに組み込みEthanを動かしていると、障害物周りでの挙動でよく詰まるのを見かける。
これはif (hitobject == ground)で条件を課しているからだとわかる。
Physics.RaycastAllを用いて、衝突した情報のリストを取り出し、その中から地面に衝突したものを選び出す。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookMoveTo : MonoBehaviour
{
public GameObject ground;
void Update()
{
Transform camera = Camera.main.transform;
Ray ray;
RaycastHit[] hits;
GameObject hitobject;
Debug.DrawRay(camera.position, camera.rotation * Vector3.forward * 100.0f);
ray = new Ray(camera.position, camera.rotation * Vector3.forward);
hits = Physics.RaycastAll(ray);
for (int i = 0; i < hits.Length; i++)
{
RaycastHit hit = hits[i];
hitobject = hit.collider.gameObject;
if (hitobject == ground)
{
Debug.Log("Hit(x,y,z):" + hit.point.ToString("F2"));
transform.position = hit.point;
}
}
}
}
//Ethanをハンティングする
レイの仕組みを利用すればEthanを攻撃することもできるようになるので、そちらも作成していく。
GameControllerを作成し、KillTargetというスクリプトをアタッチし、動きを作っていく。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KillTarget : MonoBehaviour
{
public GameObject target;
public ParticleSystem hitEffect;
public GameObject killEffect;
public float timeToselect = 3.0f;
public int score;
private ParticleSystem.EmissionModule hitEffectEmission;
private float countDown;
void Start()
{
score = 0;
countDown = timeToselect;
hitEffectEmission = hitEffect.emission;
hitEffectEmission.enabled = false;
}
void Update()
{
Transform camera = Camera.main.transform;
Ray ray = new Ray(camera.position, camera.rotation * Vector3.forward);
RaycastHit hit;
if(Physics.Raycast(ray,out hit) && (hit.collider.gameObject == target))
{
if(countDown > 0.0f)
{
countDown -= Time.deltaTime;
hitEffect.transform.position = hit.point;
hitEffectEmission.enabled = true;
}
else
{
Instantiate(killEffect, target.transform.position, target.transform.rotation);
score += 1;
countDown = timeToselect;
hitEffectEmission.enabled = false;
}
}
}
void SetRandomPosition()
{
float x = Random.Range(-5.0f, 5.0f);
float z = Random.Range(-5.0f, 5.0f);
target.transform.position = new Vector3(x, 0.0f, z);
}
}