아카이브
[주말과제] Hero Shooter 적 감지 본문
1. 레이를 쏴서 감지
Tutorial Main 스크립트에 코드 추가
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TutorialMain : MonoBehaviour
{
[SerializeField]
private VariableJoystick joystick;
[SerializeField]
private Player player;
[SerializeField]
private Portal portal;
[SerializeField]
private Transform rightDoor;
[SerializeField]
private Transform leftDoor;
private void Start()
{
this.portal.onReachPortal = () => {
Debug.Log("You've reached the portal");
//문열기
Debug.Log("Move to the door");
this.rightDoor.localRotation = Quaternion.Euler(0, -90, 0);
this.leftDoor.localRotation = Quaternion.Euler(0, 90, 0);
};
//오른쪽 문 회전값 나오게하기
//Debug.LogFormat("RightDoor World Rotation : {0}", this.rightDoor.rotation.eulerAngles);
//Debug.LogFormat("RightDoor Local Rotation : {0}", this.rightDoor.localRotation.eulerAngles);
//this.rightDoor.localRotation = Quaternion.Euler(0, -90, 0);
}
void Update()
{
var h = this.joystick.Horizontal;
var v = this.joystick.Vertical;
var dir = new Vector3(h, 0, v);
if (dir != Vector3.zero)
this.player.Move(dir);
else
this.player.Idle();
//레이를 쏴서 적 감지하기
if (Input.GetMouseButtonDown(0))
{
//클릭한 화면의 픽셀좌표로 Ray를 만든다
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//ray를 출력
Debug.DrawRay(ray.origin, ray.direction * 1000f, Color.red, 5f);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1000f))
{
//충돌한 콜라이더의 게임오브젝트의 태그가 Monster라면
if (hit.collider.gameObject.tag == "Monster")
{
//출력
Debug.Log("Monster");
}
}
}
}
}
2. 시야에 들어오면 적 감지