티스토리 뷰
#player의 애니매이션 구현, 내각 만들어주기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DeerStandard : MonoBehaviour
{
public enum eState
{
None = -1, Idle, Run, Attack
}
public float radius = 30;
public Transform target;
private Animator anim;
private Vector3 dir;
private eState state;
private Coroutine routine;
public float speed = 1f;
public GameObject modelGo; //player
public bool isPlayer;
void Start()
{
this.anim = this.modelGo.GetComponent<Animator>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space)) //Attack
{
if (this.routine != null) StopCoroutine(this.routine); //실행중인 코루틴이있다면
this.routine = this.StartCoroutine(this.CorAttack());
}
if (this.state != eState.Attack)
{
var h = Input.GetAxisRaw("Horizontal");
var v = Input.GetAxisRaw("Vertical");
dir = Vector3.Normalize(new Vector3(h, 0, v));
if (dir != Vector3.zero) //h,v가 0이 아니면
{
var angle = Mathf.Atan2(dir.x, dir.z) * Mathf.Rad2Deg; //player의 내각
this.modelGo.transform.rotation = Quaternion.AngleAxis(angle, Vector3.up);
this.anim.SetInteger("State", 1);
this.transform.Translate(this.modelGo.transform.forward * this.speed * Time.deltaTime, Space.World);
this.state = eState.Run;
}
else //dir=Vector3.zero
{
this.state = eState.Idle;
this.anim.SetInteger("State", 0);
}
}
this.OnDrawGizmos();
}
private IEnumerator CorAttack()
{
this.anim.SetInteger("State", 2);
var prevState = this.state;
this.state = eState.Attack;
Debug.LogFormat("state: <color=yellow>{0}</color>", this.state);
yield return new WaitForSeconds(0.833f);
if (prevState == eState.Run)
{
this.anim.SetInteger("State", 1);
this.state = eState.Run;
}
else if (prevState == eState.Idle)
{
this.anim.SetInteger("State", 0);
this.state = eState.Idle;
}
}
private void OnDrawGizmos()
{
if (!this.isPlayer) return;
DrawExtension.DrawArrow.DrawWireArc(this.transform.position, this.transform.forward, 90, 1);
}
}
#Dot를 이용한 player의 내각 구현
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DeerStandard : MonoBehaviour
{
public enum eState
{
None = -1, Idle, Run, Attack
}
public float radius = 30;
public Transform target;
private Animator anim;
private Vector3 dir;
private eState state;
private Coroutine routine;
public float speed = 1f;
public GameObject modelGo; //player
public bool isPlayer;
void Start()
{
this.anim = this.modelGo.GetComponent<Animator>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space)) //Attack
{
if (this.routine != null) StopCoroutine(this.routine); //실행중인 코루틴이있다면
this.routine = this.StartCoroutine(this.CorAttack());
}
if (this.state != eState.Attack)
{
var h = Input.GetAxisRaw("Horizontal");
var v = Input.GetAxisRaw("Vertical");
dir = Vector3.Normalize(new Vector3(h, 0, v));
if (dir != Vector3.zero) //h,v가 0이 아니면
{
var angle = Mathf.Atan2(dir.x, dir.z) * Mathf.Rad2Deg; //player의 내각
this.modelGo.transform.rotation = Quaternion.AngleAxis(angle, Vector3.up);
this.anim.SetInteger("State", 1);
this.transform.Translate(this.modelGo.transform.forward * this.speed * Time.deltaTime, Space.World);
this.state = eState.Run;
}
else //dir=Vector3.zero
{
this.state = eState.Idle;
this.anim.SetInteger("State", 0);
}
}
this.OnDrawGizmos();
}
private IEnumerator CorAttack()
{
this.anim.SetInteger("State", 2);
var prevState = this.state;
this.state = eState.Attack;
Debug.LogFormat("state: <color=yellow>{0}</color>", this.state);
yield return new WaitForSeconds(0.833f);
if (prevState == eState.Run)
{
this.anim.SetInteger("State", 1);
this.state = eState.Run;
}
else if (prevState == eState.Idle)
{
this.anim.SetInteger("State", 0);
this.state = eState.Idle;
}
}
private void OnDrawGizmos()
{
if (!this.isPlayer) return;
DrawExtension.DrawArrow.DrawWireArc(this.transform.position, this.transform.forward, 90, 1);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestDot2 : MonoBehaviour
{
//hero의 내각 구하기
public Transform hero; //hero
public Transform monster; //monster
private float cos;
void Start()
{
this.cos = Mathf.Cos(45 * Mathf.Deg2Rad);
}
void Update()
{
this.monster.transform.RotateAround(this.hero.transform.position, Vector3.up, 50 * Time.deltaTime);
var toOther = Vector3.Normalize(this.monster.position - this.hero.transform.position);
DrawExtension.DrawArrow.ForDebug(this.hero.position, toOther, 0f, Color.red, DrawExtension.ArrowType.Solid);
var dot = Vector3.Dot(this.hero.transform.forward, toOther);
//Debug.LogFormat("Dot:{0}", dot);
if (dot > this.cos)
{
Debug.Log("<color=red>in sight!</color>");
}
else
{
Debug.Log("<color=blue>out sight!</color>");
}
}
}
'게임클라이언트 프로그래밍 > Unity' 카테고리의 다른 글
Shader(UV 텍스쳐 합치기, VertexColor,Metallic, Smoothness_Time,_BumpMap,_Metallic,_Smoothness) (0) | 2023.03.16 |
---|---|
Shader (색상 설정, 텍스쳐 제어, lerp함수, UV) (0) | 2023.03.15 |
VR City복습(waypoint순회하며 이동,Quaternion.Lerp회전,Raycast) (1) | 2023.03.06 |
3D 캐릭터 이동,시야, 공격 애니매이션 구현 (0) | 2023.03.03 |
3D Dot, 캐릭터의 시야 (0) | 2023.03.02 |