敵の攻撃を作る

敵の攻撃力の作成
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyShotBeam : MonoBehaviour
{
public GameObject beamPrefab;
public AudioClip sound;
public float area;
public int shotInterval;
private int count;
private GameObject target;
void Start()
{
target = GameObject.Find("Player");
}
void Update()
{
// (ポイント)Distanceは2点間の距離を求めるもの
// プレーヤーがarea内に侵入したら攻撃を開始する。
if (Vector3.Distance(transform.position, target.transform.position) < area)
{
count += 1;
if (count % shotInterval == 0)
{
GameObject beam = Instantiate(beamPrefab, transform.position, transform.rotation);
Destroy(beam, 5.0f);
AudioSource.PlayClipAtPoint(sound, transform.position);
}
}
}
}




敵の攻撃力の改良
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyShotBeam : MonoBehaviour
{
public GameObject beamPrefab;
public AudioClip sound;
public float area;
public int shotInterval;
private int count;
private GameObject target;
void Start()
{
target = GameObject.Find("Player");
}
void Update()
{
if (Vector3.Distance(transform.position, target.transform.position) < area)
{
// ★追加(向きを変える)
transform.LookAt(target.transform);
// ★追加(色を赤に変える)
GetComponent<MeshRenderer>().material.color = Color.red;
count += 1;
if (count % shotInterval == 0)
{
GameObject beam = Instantiate(beamPrefab, transform.position, transform.rotation);
Destroy(beam, 5.0f);
AudioSource.PlayClipAtPoint(sound, transform.position);
}
}
// ★追加(距離が離れたら色を白に戻す)
else
{
GetComponent<MeshRenderer>().material.color = Color.white;
}
}
}


【2019版】X_Mission(基礎/全51回)
他のコースを見る
敵の攻撃力の作成
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyShotBeam : MonoBehaviour
{
public GameObject beamPrefab;
public AudioClip sound;
public float area;
public int shotInterval;
private int count;
private GameObject target;
void Start()
{
target = GameObject.Find("Player");
}
void Update()
{
// (ポイント)Distanceは2点間の距離を求めるもの
// プレーヤーがarea内に侵入したら攻撃を開始する。
if (Vector3.Distance(transform.position, target.transform.position) < area)
{
count += 1;
if (count % shotInterval == 0)
{
GameObject beam = Instantiate(beamPrefab, transform.position, transform.rotation);
Destroy(beam, 5.0f);
AudioSource.PlayClipAtPoint(sound, transform.position);
}
}
}
}




敵の攻撃力の改良
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyShotBeam : MonoBehaviour
{
public GameObject beamPrefab;
public AudioClip sound;
public float area;
public int shotInterval;
private int count;
private GameObject target;
void Start()
{
target = GameObject.Find("Player");
}
void Update()
{
if (Vector3.Distance(transform.position, target.transform.position) < area)
{
// ★追加(向きを変える)
transform.LookAt(target.transform);
// ★追加(色を赤に変える)
GetComponent<MeshRenderer>().material.color = Color.red;
count += 1;
if (count % shotInterval == 0)
{
GameObject beam = Instantiate(beamPrefab, transform.position, transform.rotation);
Destroy(beam, 5.0f);
AudioSource.PlayClipAtPoint(sound, transform.position);
}
}
// ★追加(距離が離れたら色を白に戻す)
else
{
GetComponent<MeshRenderer>().material.color = Color.white;
}
}
}


敵の攻撃を作る