動く敵の作り方1(Vライン)
![2719e472 8f21 4b09 9a68 036e3dee2dda](https://codegenius.org/uploads/slide/image/3096/2719e472-8f21-4b09-9a68-036e3dee2dda.jpeg)
![A43a3fac 90e3 414f b584 9218c2f4030b](https://codegenius.org/uploads/slide/image/3097/a43a3fac-90e3-414f-b584-9218c2f4030b.jpeg)
![768d810e 12a2 4524 843e 15960a6b7837](https://codegenius.org/uploads/slide/image/3098/768d810e-12a2-4524-843e-15960a6b7837.jpeg)
敵を動かす(Vライン)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyXMove : MonoBehaviour
{
[Range(0, 50)]
public float moveDistance;
private Vector3 pos;
private bool isReturn = false;
private void Update()
{
pos = transform.position;
if (pos.z > 0 && isReturn == false)
{
// Translate(x軸, y軸, z軸)
// x軸がプラス →方向のベクトル
// z軸がマイナス ↓方向のベクトル
// この2つのベクトルを合成すると「↘️」方向のベクトルになる。(→ + ↓ = ↘️)
transform.Translate(moveDistance * Time.deltaTime, 0, -moveDistance * Time.deltaTime, Space.World);
}
else // pos.zが1以下になった時、進行方向を変化させる。
{
isReturn = true;
// x軸がプラス →方向のベクトル
// z軸がマイナス ↑方向のベクトル
// この2つのベクトルを合成すると「↗️」方向のベクトルになる。(→ + ↑ = ↗️)
transform.Translate(moveDistance * Time.deltaTime, 0, moveDistance * Time.deltaTime, Space.World);
}
}
}
![E14cd37d 9e05 40c0 9c65 ecf1b4a05f5c](https://codegenius.org/uploads/slide/image/3099/e14cd37d-9e05-40c0-9c65-ecf1b4a05f5c.jpeg)
![3922b86b c0ae 425e 90b2 15a0f7d654e0](https://codegenius.org/uploads/slide/image/3100/3922b86b-c0ae-425e-90b2-15a0f7d654e0.jpeg)
![Fff2ad27 37d3 48d6 9d15 ce01c40a6d7d](https://codegenius.org/uploads/slide/image/3101/fff2ad27-37d3-48d6-9d15-ce01c40a6d7d.jpeg)
![B295eed8 31f8 4bcf 908f 3ff97699d59e](https://codegenius.org/uploads/slide/image/3102/b295eed8-31f8-4bcf-908f-3ff97699d59e.jpeg)
![95cd5baf a2fa 4f8d a089 f160fb1d699e](https://codegenius.org/uploads/slide/image/3103/95cd5baf-a2fa-4f8d-a089-f160fb1d699e.jpeg)
敵を動かす(逆Vライン)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyX2Move : MonoBehaviour
{
[Range(0, 50)]
public float moveDistance;
private Vector3 pos;
private bool isReturn = false;
void Update()
{
pos = transform.position;
if (pos.z < 0 && isReturn == false)
{
transform.Translate(-moveDistance * Time.deltaTime, 0, moveDistance * Time.deltaTime, Space.World);
}
else
{
isReturn = true;
transform.Translate(-moveDistance * Time.deltaTime, 0, -moveDistance * Time.deltaTime, Space.World);
}
}
}
![57b1331c a416 4dd2 9936 073a647a5388](https://codegenius.org/uploads/slide/image/3104/57b1331c-a416-4dd2-9936-073a647a5388.jpeg)
![A9f6e8da 056c 42a8 9129 705cd32c095a](https://codegenius.org/uploads/slide/image/3105/a9f6e8da-056c-42a8-9129-705cd32c095a.jpeg)
![3357d334 56d8 464a a787 d32741fdafae](https://codegenius.org/uploads/slide/image/3106/3357d334-56d8-464a-a787-d32741fdafae.jpeg)
![6dd72747 f6e5 44ec 8695 fa9865337903](https://codegenius.org/uploads/slide/image/3107/6dd72747-f6e5-44ec-8695-fa9865337903.jpeg)
敵の発生装置
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyGene2 : MonoBehaviour
{
public GameObject enemyPrefab;
public int geneAmount;
[Range(0, 1.5f)]
public float waitTime;
private void Start()
{
StartCoroutine(GeneEnemy());
}
IEnumerator GeneEnemy()
{
for (int i = 0; i < geneAmount; i++)
{
GameObject enemy = Instantiate(enemyPrefab, transform.position, Quaternion.identity);
Destroy(enemy, 15.0f);
yield return new WaitForSeconds(waitTime);
}
}
}
![Ed7e3a9f 8a7d 4579 a3d2 c78266f75b87](https://codegenius.org/uploads/slide/image/3108/ed7e3a9f-8a7d-4579-a3d2-c78266f75b87.jpeg)
![5281d218 41a7 4550 8d4c 0805b873bd85](https://codegenius.org/uploads/slide/image/3109/5281d218-41a7-4550-8d4c-0805b873bd85.jpeg)
![75de6fe0 dcd8 4ade 896c 498d5740c829](https://codegenius.org/uploads/slide/image/3110/75de6fe0-dcd8-4ade-896c-498d5740c829.jpeg)
![8b780150 3e08 412c 9047 60013e646ee5](https://codegenius.org/uploads/slide/image/3111/8b780150-3e08-412c-9047-60013e646ee5.jpeg)
![2719e472 8f21 4b09 9a68 036e3dee2dda](https://codegenius.org/uploads/slide/image/3096/2719e472-8f21-4b09-9a68-036e3dee2dda.jpeg)
![A43a3fac 90e3 414f b584 9218c2f4030b](https://codegenius.org/uploads/slide/image/3097/a43a3fac-90e3-414f-b584-9218c2f4030b.jpeg)
![768d810e 12a2 4524 843e 15960a6b7837](https://codegenius.org/uploads/slide/image/3098/768d810e-12a2-4524-843e-15960a6b7837.jpeg)
敵を動かす(Vライン)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyXMove : MonoBehaviour
{
[Range(0, 50)]
public float moveDistance;
private Vector3 pos;
private bool isReturn = false;
private void Update()
{
pos = transform.position;
if (pos.z > 0 && isReturn == false)
{
// Translate(x軸, y軸, z軸)
// x軸がプラス →方向のベクトル
// z軸がマイナス ↓方向のベクトル
// この2つのベクトルを合成すると「↘️」方向のベクトルになる。(→ + ↓ = ↘️)
transform.Translate(moveDistance * Time.deltaTime, 0, -moveDistance * Time.deltaTime, Space.World);
}
else // pos.zが1以下になった時、進行方向を変化させる。
{
isReturn = true;
// x軸がプラス →方向のベクトル
// z軸がマイナス ↑方向のベクトル
// この2つのベクトルを合成すると「↗️」方向のベクトルになる。(→ + ↑ = ↗️)
transform.Translate(moveDistance * Time.deltaTime, 0, moveDistance * Time.deltaTime, Space.World);
}
}
}
![E14cd37d 9e05 40c0 9c65 ecf1b4a05f5c](https://codegenius.org/uploads/slide/image/3099/e14cd37d-9e05-40c0-9c65-ecf1b4a05f5c.jpeg)
![3922b86b c0ae 425e 90b2 15a0f7d654e0](https://codegenius.org/uploads/slide/image/3100/3922b86b-c0ae-425e-90b2-15a0f7d654e0.jpeg)
![Fff2ad27 37d3 48d6 9d15 ce01c40a6d7d](https://codegenius.org/uploads/slide/image/3101/fff2ad27-37d3-48d6-9d15-ce01c40a6d7d.jpeg)
![B295eed8 31f8 4bcf 908f 3ff97699d59e](https://codegenius.org/uploads/slide/image/3102/b295eed8-31f8-4bcf-908f-3ff97699d59e.jpeg)
![95cd5baf a2fa 4f8d a089 f160fb1d699e](https://codegenius.org/uploads/slide/image/3103/95cd5baf-a2fa-4f8d-a089-f160fb1d699e.jpeg)
敵を動かす(逆Vライン)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyX2Move : MonoBehaviour
{
[Range(0, 50)]
public float moveDistance;
private Vector3 pos;
private bool isReturn = false;
void Update()
{
pos = transform.position;
if (pos.z < 0 && isReturn == false)
{
transform.Translate(-moveDistance * Time.deltaTime, 0, moveDistance * Time.deltaTime, Space.World);
}
else
{
isReturn = true;
transform.Translate(-moveDistance * Time.deltaTime, 0, -moveDistance * Time.deltaTime, Space.World);
}
}
}
![57b1331c a416 4dd2 9936 073a647a5388](https://codegenius.org/uploads/slide/image/3104/57b1331c-a416-4dd2-9936-073a647a5388.jpeg)
![A9f6e8da 056c 42a8 9129 705cd32c095a](https://codegenius.org/uploads/slide/image/3105/a9f6e8da-056c-42a8-9129-705cd32c095a.jpeg)
![3357d334 56d8 464a a787 d32741fdafae](https://codegenius.org/uploads/slide/image/3106/3357d334-56d8-464a-a787-d32741fdafae.jpeg)
![6dd72747 f6e5 44ec 8695 fa9865337903](https://codegenius.org/uploads/slide/image/3107/6dd72747-f6e5-44ec-8695-fa9865337903.jpeg)
敵の発生装置
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyGene2 : MonoBehaviour
{
public GameObject enemyPrefab;
public int geneAmount;
[Range(0, 1.5f)]
public float waitTime;
private void Start()
{
StartCoroutine(GeneEnemy());
}
IEnumerator GeneEnemy()
{
for (int i = 0; i < geneAmount; i++)
{
GameObject enemy = Instantiate(enemyPrefab, transform.position, Quaternion.identity);
Destroy(enemy, 15.0f);
yield return new WaitForSeconds(waitTime);
}
}
}
![Ed7e3a9f 8a7d 4579 a3d2 c78266f75b87](https://codegenius.org/uploads/slide/image/3108/ed7e3a9f-8a7d-4579-a3d2-c78266f75b87.jpeg)
![5281d218 41a7 4550 8d4c 0805b873bd85](https://codegenius.org/uploads/slide/image/3109/5281d218-41a7-4550-8d4c-0805b873bd85.jpeg)
![75de6fe0 dcd8 4ade 896c 498d5740c829](https://codegenius.org/uploads/slide/image/3110/75de6fe0-dcd8-4ade-896c-498d5740c829.jpeg)
![8b780150 3e08 412c 9047 60013e646ee5](https://codegenius.org/uploads/slide/image/3111/8b780150-3e08-412c-9047-60013e646ee5.jpeg)
動く敵の作り方1(Vライン)