using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TestRayCasAndCollider : MonoBehaviour
{
public Text txt;
public Text txtHitPoint;
public GameObject heroGo;
private Animator heroAnim;
private float speed = 2;
//조이스틱
public TestMyJoyStick joystick;
private Coroutine joystickRoutine;
void Awake()
{
Debug.Log("어우에읶끄");
this.heroAnim = this.heroGo.GetComponent<Animator>();
}
void Start()
{
Debug.Log("레이케스트 콜라이더 테스트체잿시뱆ㄷ가사");
joystick.OnPointerDownHandler = () =>
{
Debug.Log("따따운핸들럴");
StartCoroutine(UpdateJoyStickImple());
};
joystick.OnPointerUpHandler = () =>
{
StopAllCoroutines();
Debug.Log("어법핸들럴럴럴");
this.heroAnim.Play("idle@loop");
};
}
private IEnumerator UpdateJoyStickImple()
{
while (true)
{
var dir = (Vector3.forward * joystick.Vertical + Vector3.right * joystick.Horizontal).normalized;
Debug.LogFormat("{0}", dir);
if (dir != Vector3.zero)
{
//보는방향
//var rad = Mathf.Atan2(dir.x, dir.z);
//var dig = Mathf.Rad2Deg * rad;
//this.heroGo.transform.rotation = Quaternion.Euler(new Vector3(0, dig, 0));
this.heroGo.transform.rotation = Quaternion.LookRotation(dir);
//움직임
this.heroAnim.Play("run@loop");
this.heroGo.transform.position += dir * this.speed * Time.deltaTime;
yield return null;
}
else
{
this.heroAnim.Play("idle@loop");
}
}
}
private void Move(Vector3 pos)
{
StopAllCoroutines();
StartCoroutine(this.MoveImpl(pos));
}
private IEnumerator MoveImpl(Vector3 pos)
{
this.heroAnim.Play("run@loop");
var dir = (pos - this.heroGo.transform.position).normalized;
var dis = Vector3.Distance(pos, this.heroGo.transform.position);
this.heroGo.transform.LookAt(pos);
while (true)
{
if (dis > 0)
{
this.heroGo.transform.position += dir * this.speed * Time.deltaTime;
dis -= this.speed * Time.deltaTime;
yield return null;
}
else
{
this.heroAnim.Play("idle@loop");
this.heroGo.transform.position = pos;
break;
}
}
}
//화면을 누른 좌표로 이동
private void UpdateMouseInput()
{
if (Input.GetMouseButtonUp(0))
{
//마우스 스크린 좌표를 화면에 표시
Debug.LogFormat("마우스 포지션 : {0}", Input.mousePosition);
this.txt.text = Input.mousePosition.ToString();
//레이 객체를 얻어옴
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//화면에 레이를 표시
Debug.DrawRay(ray.origin, ray.direction * 1000, Color.red, 5f);
RaycastHit hitInfo;
if (Physics.Raycast(ray.origin, ray.direction, out hitInfo, 1000))
{
Debug.LogFormat("히트 포인트 : {0}", hitInfo.point);
this.txtHitPoint.text = hitInfo.point.ToString();
this.Move(hitInfo.point);
}
else
{
this.txtHitPoint.text = "빈 공간";
}
}
}
//WASD or 방향키 입력 이동
private void UpdateKeyInput()
{
var keyX = Input.GetAxis("Horizontal");
var keyY = Input.GetAxis("Vertical");
Vector3 dir = new Vector3(keyX, 0, keyY).normalized;
Debug.LogFormat("{0}", dir);
if (dir != Vector3.zero)
{
//보는방향
//var rad = Mathf.Atan2(keyX, keyY);
//var dig = Mathf.Rad2Deg * rad;
//this.heroGo.transform.rotation = Quaternion.Euler(new Vector3(0, dig, 0));
this.heroGo.transform.rotation = Quaternion.LookRotation(dir);
//움직임
this.heroAnim.Play("run@loop");
this.heroGo.transform.position += dir * this.speed * Time.deltaTime;
}
else
{
this.heroAnim.Play("idle@loop");
}
}
private void UpdateJoyStickInput()
{
var dir = (Vector3.forward * joystick.Vertical + Vector3.right * joystick.Horizontal).normalized;
Debug.LogFormat("{0}", dir);
if (dir != Vector3.zero)
{
//보는방향
//var rad = Mathf.Atan2(dir.x, dir.z);
//var dig = Mathf.Rad2Deg * rad;
//this.heroGo.transform.rotation = Quaternion.Euler(new Vector3(0, dig, 0));
this.heroGo.transform.rotation = Quaternion.LookRotation(dir);
//움직임
this.heroAnim.Play("run@loop");
this.heroGo.transform.position += dir * this.speed * Time.deltaTime;
}
else
{
this.heroAnim.Play("idle@loop");
}
}
}