マイロボットの作成2




自動プロパティの設定
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMove : MonoBehaviour
{
// ★改良
// 「自動プロパティ」をネットで調べてみよう!
public float MoveSpeed { get; set; } // 自動プロパティの設定(ポイント)
private float maxDis = 0.75f;
private string tagName;
// ★改良
private void Start()
{
MoveSpeed = 5;
}
void Update()
{
// ★改良
transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime);
RaycastHit hit;
Ray ray = new Ray(transform.position, transform.forward);
Debug.DrawRay(transform.position, transform.forward * maxDis, Color.red);
if(Physics.Raycast(ray, out hit, maxDis))
{
GameObject target = hit.collider.gameObject;
tagName = target.tag;
switch (tagName)
{
case "Right":
transform.localRotation = Quaternion.Euler(0, transform.eulerAngles.y + 90, 0);
break;
case "Left":
transform.localRotation = Quaternion.Euler(0, transform.eulerAngles.y + 270, 0);
break;
}
}
}
}

敵の移動速度を低下させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveSlow : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if(other.TryGetComponent(out EnemyMove eMove))
{
eMove.MoveSpeed /= 3; // 敵の移動速度を「3分の1」にする。
}
}
private void OnTriggerExit(Collider other)
{
if (other.TryGetComponent(out EnemyMove eMove))
{
eMove.MoveSpeed *= 3; // 敵の移動速度を「3倍」にする。(元に戻す)
}
}
}













自動プロパティの設定
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMove : MonoBehaviour
{
// ★改良
// 「自動プロパティ」をネットで調べてみよう!
public float MoveSpeed { get; set; } // 自動プロパティの設定(ポイント)
private float maxDis = 0.75f;
private string tagName;
// ★改良
private void Start()
{
MoveSpeed = 5;
}
void Update()
{
// ★改良
transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime);
RaycastHit hit;
Ray ray = new Ray(transform.position, transform.forward);
Debug.DrawRay(transform.position, transform.forward * maxDis, Color.red);
if(Physics.Raycast(ray, out hit, maxDis))
{
GameObject target = hit.collider.gameObject;
tagName = target.tag;
switch (tagName)
{
case "Right":
transform.localRotation = Quaternion.Euler(0, transform.eulerAngles.y + 90, 0);
break;
case "Left":
transform.localRotation = Quaternion.Euler(0, transform.eulerAngles.y + 270, 0);
break;
}
}
}
}

敵の移動速度を低下させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveSlow : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if(other.TryGetComponent(out EnemyMove eMove))
{
eMove.MoveSpeed /= 3; // 敵の移動速度を「3分の1」にする。
}
}
private void OnTriggerExit(Collider other)
{
if (other.TryGetComponent(out EnemyMove eMove))
{
eMove.MoveSpeed *= 3; // 敵の移動速度を「3倍」にする。(元に戻す)
}
}
}









マイロボットの作成2