敵ロボットをターンさせる2
![A6541b45 88b4 469f b2ce 3256127d2f96](https://codegenius.org/uploads/slide/image/3803/a6541b45-88b4-469f-b2ce-3256127d2f96.jpeg)
敵を右にターンさせる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyTurn : MonoBehaviour
{
private float maxDistance = 0.5f;
private string tagName;
void Update()
{
RaycastHit hit;
Ray ray = new Ray(transform.position, transform.forward);
Debug.DrawRay(transform.position, transform.forward * 0.5f, Color.red);
if(Physics.Raycast(ray, out hit, maxDistance))
{
GameObject target = hit.collider.gameObject;
tagName = target.tag;
switch(tagName)
{
case "RedPillar_L":
transform.root.localRotation = Quaternion.Euler(0, transform.root.eulerAngles.y + 270, 0);
break;
// ★追加(右にターンさせる)
case "RedPillar_R":
transform.root.localRotation = Quaternion.Euler(0, transform.root.eulerAngles.y + 90, 0);
break;
}
}
}
}
![3ac1567b c499 4c37 bf1e a89473d6c446](https://codegenius.org/uploads/slide/image/3804/3ac1567b-c499-4c37-bf1e-a89473d6c446.jpeg)
![B35fb011 e2d7 4525 bdb4 ae4b9d8729d0](https://codegenius.org/uploads/slide/image/3805/b35fb011-e2d7-4525-bdb4-ae4b9d8729d0.jpeg)
![Fc2ea2d7 ffe2 434f b313 fd0da985dfcc](https://codegenius.org/uploads/slide/image/3806/fc2ea2d7-ffe2-434f-b313-fd0da985dfcc.jpeg)
![7fe619bb 9c7b 40aa 961e 746d6c5a845c](https://codegenius.org/uploads/slide/image/3807/7fe619bb-9c7b-40aa-961e-746d6c5a845c.jpeg)
![351fab5d 49dc 4548 b4d4 4c9b9c91cce7](https://codegenius.org/uploads/slide/image/3808/351fab5d-49dc-4548-b4d4-4c9b9c91cce7.jpeg)
【2019版】TowerD I(基礎/全10回)
1 | 敵ロボットの作成&前進 |
2 | 敵ロボットをターンさせる1 |
3 | 敵ロボットをターンさせる2 |
4 | 敵ロボットにHPをつける |
5 | ロボットを生み出す装置を作る |
6 | マイロボットの作成1 |
7 | マイロボットにライフタイムを設定する |
8 | マイロボットの作成2 |
9 | ★チェックポイント |
10 | ★チャレンジ |
![A6541b45 88b4 469f b2ce 3256127d2f96](https://codegenius.org/uploads/slide/image/3803/a6541b45-88b4-469f-b2ce-3256127d2f96.jpeg)
敵を右にターンさせる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyTurn : MonoBehaviour
{
private float maxDistance = 0.5f;
private string tagName;
void Update()
{
RaycastHit hit;
Ray ray = new Ray(transform.position, transform.forward);
Debug.DrawRay(transform.position, transform.forward * 0.5f, Color.red);
if(Physics.Raycast(ray, out hit, maxDistance))
{
GameObject target = hit.collider.gameObject;
tagName = target.tag;
switch(tagName)
{
case "RedPillar_L":
transform.root.localRotation = Quaternion.Euler(0, transform.root.eulerAngles.y + 270, 0);
break;
// ★追加(右にターンさせる)
case "RedPillar_R":
transform.root.localRotation = Quaternion.Euler(0, transform.root.eulerAngles.y + 90, 0);
break;
}
}
}
}
![3ac1567b c499 4c37 bf1e a89473d6c446](https://codegenius.org/uploads/slide/image/3804/3ac1567b-c499-4c37-bf1e-a89473d6c446.jpeg)
![B35fb011 e2d7 4525 bdb4 ae4b9d8729d0](https://codegenius.org/uploads/slide/image/3805/b35fb011-e2d7-4525-bdb4-ae4b9d8729d0.jpeg)
![Fc2ea2d7 ffe2 434f b313 fd0da985dfcc](https://codegenius.org/uploads/slide/image/3806/fc2ea2d7-ffe2-434f-b313-fd0da985dfcc.jpeg)
![7fe619bb 9c7b 40aa 961e 746d6c5a845c](https://codegenius.org/uploads/slide/image/3807/7fe619bb-9c7b-40aa-961e-746d6c5a845c.jpeg)
![351fab5d 49dc 4548 b4d4 4c9b9c91cce7](https://codegenius.org/uploads/slide/image/3808/351fab5d-49dc-4548-b4d4-4c9b9c91cce7.jpeg)
敵ロボットをターンさせる2