using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TestRaycastAndCollider : MonoBehaviour
{
public Text txtMousePoint;
public Text txtHitPoint;
public GameObject heroGo;
private Animator heroAnim;
private Coroutine heroRoutine;
private Coroutine joystickRoutine;
public TestMyJoystick joystick;
private void Awake()
{
Debug.Log("TestRaycastAndCollider Awake!");
}
// Start is called before the first frame update
void Start()
{
this.heroAnim = heroGo.GetComponent<Animator>();
Debug.Log("TestRaycastAndCollider Start!");
this.joystick.OnPointerDownHandler = () =>
{
Debug.Log("다운핸들러");
if (this.joystickRoutine != null)
{
StopCoroutine(this.joystickRoutine);
}
StartCoroutine(this.UpdateJoystickImpl());
};
this.joystick.OnPointerUpHandler = () =>
{
Debug.Log("업핸들러");
StopAllCoroutines();
this.heroAnim.Play("idle");
};
}
private void UpdateJoystick()
{
Vector3 direction = Vector3.forward * this.joystick.Vertical + Vector3.right * joystick.Horizontal;
direction = new Vector3(direction.x, 0, direction.z).normalized;
if (direction != Vector3.zero)
{
this.heroAnim.Play("run");
this.heroGo.transform.position += direction * 1.0f * Time.deltaTime;
var rad = Mathf.Atan2(direction.x, direction.z);
var dig = Mathf.Rad2Deg * rad;
this.heroGo.transform.rotation = Quaternion.LookRotation(direction);
//this.heroGo.transform.rotation = Quaternion.Euler(new Vector3(0, dig, 0));
}
else
{
this.heroAnim.Play("idle");
}
}
private IEnumerator UpdateJoystickImpl()
{
while (true)
{
Vector3 direction = (Vector3.forward * this.joystick.Vertical + Vector3.right * joystick.Horizontal).normalized;
if (direction != Vector3.zero)
{
this.heroGo.transform.rotation = Quaternion.LookRotation(direction);
this.heroAnim.Play("run");
this.heroGo.transform.position += direction * 2.0f * Time.deltaTime;
yield return null;
}
else
{
this.heroAnim.Play("idle");
}
}
}
// Update is called once per frame
private void UpdateKeyInput()
{
var keyY = Input.GetAxis("Vertical");
var keyX = Input.GetAxis("Horizontal");
Debug.LogFormat("keyY : {0}, keyX : {1}", keyY, keyX);
var moveKey = new Vector3(keyX, 0, keyY).normalized;
if (moveKey != Vector3.zero)
{
this.heroAnim.Play("run");
this.heroGo.transform.position += moveKey * 1.0f * Time.deltaTime;
var rad = Mathf.Atan2(keyX, keyY);
var dig = Mathf.Rad2Deg * rad;
this.heroGo.transform.rotation = Quaternion.Euler(new Vector3(0, dig, 0));
}
else
{
this.heroAnim.Play("idle");
}
}
private void UpdateMouseInput()
{
if(Input.GetMouseButton(0))
{
//찍은 곳 스크린 좌표 표시
this.txtMousePoint.text = Input.mousePosition.ToString();
//찍은 곳 Ray생성
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//화면 Ray표시
Debug.DrawRay(ray.origin, ray.direction * 1000, Color.red, 5f);
RaycastHit hitInfo;
if(Physics.Raycast(ray.origin, ray.direction, out hitInfo, 1000))
{
Debug.LogFormat("hitInfo.point : {0}", hitInfo.point.ToString());
this.txtHitPoint.text = hitInfo.point.ToString();
this.Move(hitInfo.point);
}
else
{
this.txtHitPoint.text = "None";
}
}
}
public void Move(Vector3 targetCoordinate)
{
if(this.heroRoutine == null)
{
this.heroAnim.Play("run");
}
if(this.heroRoutine != null)
{
StopAllCoroutines();
}
this.heroRoutine = StartCoroutine(this.MoveHero(targetCoordinate, () =>
{
this.heroAnim.Play("idle");
}));
}
private IEnumerator MoveHero(Vector3 targetCoordinate, System.Action OnCompleteMove)
{
this.heroGo.transform.LookAt(targetCoordinate);
var dir = (targetCoordinate - this.heroGo.transform.position).normalized;
var speed = dir * 1.0f * Time.deltaTime;
while(true)
{
var distance = Vector3.Distance(targetCoordinate, this.heroGo.transform.position);
this.heroGo.transform.position += speed;
if(distance <= 0.01f)
{
this.heroGo.transform.position = targetCoordinate;
break;
}
yield return null;
}
OnCompleteMove();
}
}