Unity3D
Unity3D 관련
by 히아레인
Tag
Media Log
Location Log
Guest Book
Admin
Write
Article Category
분류 전체보기
(495)
유니티
(227)
Note
(104)
스크립트
(123)
유니티로 게임을 만드는 10가..
(0)
챕터 1 도깨비
(0)
절대강좌
(0)
보고싶은책
(32)
cocos2d-x
(6)
웹관련
(13)
자바스크립트
(3)
DB
(5)
PHP & MySql 정리
(0)
잡다한것들전부
(207)
클레이 사격
(0)
팁
(61)
세팅
(1)
프로젝트
(0)
런게임
(0)
따라하기
(0)
Tutorial
(7)
기타
(15)
c++ STL
(12)
C, C++, C#
(15)
안드로이드
(15)
언어
(0)
수학
(0)
cookbook관련
(2)
물리구현
(0)
심화
(1)
프로젝트
(0)
팁
(38)
포스팅예정
(0)
Basic Concepts
(7)
세팅
(4)
소식
(1)
2D 관련 자료
(2)
에셋
(2)
C#
(6)
Card
(0)
계란받기게임
(0)
Flying Owl
(0)
Dungeon의 Alien
(0)
Temple Run
(0)
Memory Test
(0)
운석피하기
(0)
공식예제
(13)
예제따라하기
(0)
퍼즐
(0)
참고사이트
(4)
유니티 코리아
(1)
Notice
Recent Article
UGUI Text 글자 간격..
다른 확률을 가진 아이..
유니티 서버 관련.
유니티 5.x 애니메이션..
git 언어별 ignore 리..
Recent Comment
정리가 너무 잘 되어..
[승인대기]
[승인대기]
대단하군요
감사합니다. ^
메트리얼 쉐이더를 tra..
안녕하세요. 이런 움직..
Sprites/default 메트..
머터리얼을 Sprites/de..
프로그램 실행하자마자..
Recent Trackback
水泳 rx
unity3d stealth 적의 시야 관련 스크립트
|
카테고리 없음
2015. 1. 4. 20:35
Posted by 히아레인
using UnityEngine; using System.Collections; public class EnemySight : MonoBehaviour { public float fieldOfViewAngle = 110f; // Number of degrees, centred on forward, for the enemy see. public bool playerInSight; // Whether or not the player is currently sighted. public Vector3 personalLastSighting; // Last place this enemy spotted the player. private NavMeshAgent nav; // Reference to the NavMeshAgent component. private SphereCollider col; // Reference to the sphere collider trigger component. private Animator anim; // Reference to the Animator. private LastPlayerSighting lastPlayerSighting; // Reference to last global sighting of the player. private GameObject player; // Reference to the player. private Animator playerAnim; // Reference to the player's animator component. private PlayerHealth playerHealth; // Reference to the player's health script. private HashIDs hash; // Reference to the HashIDs. private Vector3 previousSighting; // Where the player was sighted last frame. void Awake () { // Setting up the references. nav = GetComponent<NavMeshAgent>(); col = GetComponent<SphereCollider>(); anim = GetComponent<Animator>(); lastPlayerSighting = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<LastPlayerSighting>(); player = GameObject.FindGameObjectWithTag(Tags.player); playerAnim = player.GetComponent<Animator>(); playerHealth = player.GetComponent<PlayerHealth>(); hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>(); // Set the personal sighting and the previous sighting to the reset position. personalLastSighting = lastPlayerSighting.resetPosition; previousSighting = lastPlayerSighting.resetPosition; } void Update () { // If the last global sighting of the player has changed... if(lastPlayerSighting.position != previousSighting) // ... then update the personal sighting to be the same as the global sighting. personalLastSighting = lastPlayerSighting.position; // Set the previous sighting to the be the sighting from this frame. previousSighting = lastPlayerSighting.position; // If the player is alive... if(playerHealth.health > 0f) // ... set the animator parameter to whether the player is in sight or not. anim.SetBool(hash.playerInSightBool, playerInSight); else // ... set the animator parameter to false. anim.SetBool(hash.playerInSightBool, false); } void OnTriggerStay (Collider other) { // If the player has entered the trigger sphere... if(other.gameObject == player) { // By default the player is not in sight. playerInSight = false; // Create a vector from the enemy to the player and store the angle between it and forward. Vector3 direction = other.transform.position - transform.position; float angle = Vector3.Angle(direction, transform.forward); // If the angle between forward and where the player is, is less than half the angle of view... if(angle < fieldOfViewAngle * 0.5f) { RaycastHit hit; // ... and if a raycast towards the player hits something... if(Physics.Raycast(transform.position + transform.up, direction.normalized, out hit, col.radius)) { // ... and if the raycast hits the player... if(hit.collider.gameObject == player) { // ... the player is in sight. playerInSight = true; // Set the last global sighting is the players current position. lastPlayerSighting.position = player.transform.position; } } } // Store the name hashes of the current states. int playerLayerZeroStateHash = playerAnim.GetCurrentAnimatorStateInfo(0).nameHash; int playerLayerOneStateHash = playerAnim.GetCurrentAnimatorStateInfo(1).nameHash; // If the player is running or is attracting attention... if(playerLayerZeroStateHash == hash.locomotionState || playerLayerOneStateHash == hash.shoutState) { // ... and if the player is within hearing range... if(CalculatePathLength(player.transform.position) <= col.radius) // ... set the last personal sighting of the player to the player's current position. personalLastSighting = player.transform.position; } } } void OnTriggerExit (Collider other) { // If the player leaves the trigger zone... if(other.gameObject == player) // ... the player is not in sight. playerInSight = false; } float CalculatePathLength (Vector3 targetPosition) { // Create a path and set it based on a target position. NavMeshPath path = new NavMeshPath(); if(nav.enabled) nav.CalculatePath(targetPosition, path); // Create an array of points which is the length of the number of corners in the path + 2. Vector3[] allWayPoints = new Vector3[path.corners.Length + 2]; // The first point is the enemy's position. allWayPoints[0] = transform.position; // The last point is the target position. allWayPoints[allWayPoints.Length - 1] = targetPosition; // The points inbetween are the corners of the path. for(int i = 0; i < path.corners.Length; i++) { allWayPoints[i + 1] = path.corners[i]; } // Create a float to store the path length that is by default 0. float pathLength = 0; // Increment the path length by an amount equal to the distance between each waypoint and the next. for(int i = 0; i < allWayPoints.Length - 1; i++) { pathLength += Vector3.Distance(allWayPoints[i], allWayPoints[i + 1]); } return pathLength; } }
공유하기
글 요소
구독하기
Unity3D
저작자표시
Trackback
0
And
Comment
0
Trackback
|
1
|
···
|
26
|
27
|
28
|
29
|
30
|
31
|
32
|
33
|
34
|
···
|
495
|
Tag Cloud
소스
itween
하이라이트
transform
안드로이드
cocos2d
러너게임
COCOS2D-X
유니티
JNI
It
action
c++
Flip
상수
C#
Roguelike
게임
구글
블로그
디자인패턴
Unity3d
NGUI
코드
계란받기
인벤토리
파티클
튜토리얼
서적
이클립스
php
Unity2D
Calendar
«
2020/06
»
일
월
화
수
목
금
토
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Archive
2015/07
(3)
2015/06
(4)
2015/05
(1)
2015/04
(3)
2015/03
(2)
My Link
유니티 강좌
유니티 코리아
ElMaHe
유니티 정보를 나누어..
데브코리아 - ★게임개..
순순디자인
The Anarchists
2D Toolkit - Document..
cocos2d-x 공식 사이트
안드로이드 개발자
JSON 오류 검사
Stack Overflow
금지된 엑시노아의 비..