using UnityEngine;
|
using UnityEngine.AI;
|
|
public class TestPlayerController : MonoBehaviour
|
{
|
public float speed = 5;
|
private NavMeshPath m_Path;
|
private Animator m_Animator;
|
|
private int m_CurrentIndex;
|
private bool m_IsStartFinding = false;
|
|
private bool m_StartedRotateCamera;
|
private bool m_MousePressed = false;
|
private Vector3 m_PreMousePos;
|
|
private float m_RecordDist;
|
private float m_RecordRotX;
|
private float m_RecordRotY;
|
|
private void Awake()
|
{
|
if (CameraController.Instance == null)
|
{
|
var prefab = Instantiate(BuiltInLoader.LoadPrefab("GameCamera"));
|
CameraController.Instance.SetcamerePrefab(prefab);
|
}
|
CameraController.Instance.SetLookTarget(transform);
|
|
m_Path = new NavMeshPath();
|
|
m_Animator = gameObject.AddMissingComponent<Animator>();
|
SystemSetting.Instance.SetQualityLevel(GameQuality.High);
|
|
var shadowPrefab = BuiltInLoader.LoadPrefab("ActorShadowCaster");
|
var shadowCaster = Instantiate(shadowPrefab).GetComponent<ActorShadowCaster>();
|
|
if (shadowCaster != null)
|
{
|
shadowCaster.Follow(this.transform.Find("A_Name"));
|
shadowCaster.Cast(this.transform);
|
shadowCaster.SetActive(true);
|
}
|
}
|
|
// 9 -45 40
|
private void Update()
|
{
|
if (Input.GetKeyDown(KeyCode.F1))
|
{
|
m_StartedRotateCamera = !m_StartedRotateCamera;
|
|
if (m_StartedRotateCamera)
|
{
|
CameraController.Instance.Distance = 11;
|
CameraController.Instance.rotationX = -110;
|
CameraController.Instance.rotationY = 50;
|
}
|
else
|
{
|
CameraController.Instance.Distance = 9;
|
CameraController.Instance.rotationX = -45;
|
CameraController.Instance.rotationY = 40;
|
}
|
}
|
|
if (Input.GetMouseButtonDown(0))
|
{
|
if (CameraController.Instance == null)
|
{
|
return;
|
}
|
|
Camera _mainCamera = CameraController.Instance.CameraObject;
|
|
RaycastHit _hitInfo;
|
Ray _ray = _mainCamera.ScreenPointToRay(Input.mousePosition);
|
|
if (Physics.Raycast(_ray, out _hitInfo, 100, LayerUtility.WalkbleMask))
|
{
|
NavMeshHit _hit;
|
|
Vector3 _c = transform.position;
|
_c.y = 0;
|
Vector3 _p = _hitInfo.point;
|
_p.y = 0;
|
|
if (NavMesh.SamplePosition(_p, out _hit, 10, NavMesh.AllAreas))
|
{
|
NavMesh.CalculatePath(_c, _hit.position, NavMesh.AllAreas, m_Path);
|
m_IsStartFinding = true;
|
m_CurrentIndex = 0;
|
m_Animator.SetInteger(GAStaticDefine.Param_Action, GAStaticDefine.Act_Run);
|
}
|
else
|
{
|
transform.position = _hitInfo.point;
|
}
|
}
|
}
|
|
if (!m_IsStartFinding)
|
{
|
return;
|
}
|
|
if (m_Path.corners != null)
|
{
|
Vector3 _current;
|
if (m_CurrentIndex > m_Path.corners.Length - 1 || m_CurrentIndex < 0)
|
{
|
m_IsStartFinding = false;
|
m_Animator.SetInteger(GAStaticDefine.Param_Action, GAStaticDefine.Act_Idle);
|
return;
|
}
|
if (m_Path.corners.Length == 1)
|
{
|
_current = m_Path.corners[0];
|
}
|
else
|
{
|
_current = m_Path.corners[1];
|
}
|
|
Vector3 _currentPosition = transform.position;
|
_current.y = 0;
|
_currentPosition.y = 0;
|
|
float _chkDistSqrt = MathUtility.DistanceSqrtXZ(_current, transform.position);
|
|
Vector3 _dir = (_current - _currentPosition).normalized;
|
_dir.y = 0;
|
if (_dir != Vector3.zero)
|
{
|
transform.forward = _dir;
|
}
|
Vector3 _detla = speed * Time.deltaTime * _dir;
|
_detla.y = 0;
|
_currentPosition += _detla;
|
|
RaycastHit _hitInfo;
|
|
if (Physics.Raycast(_currentPosition + new Vector3(0, 50, 0), Vector3.down, out _hitInfo, 100, 1 << LayerMask.NameToLayer("WalkbleLayer")))
|
{
|
transform.position = _hitInfo.point;
|
}
|
|
if (_chkDistSqrt < Mathf.Pow(speed * Time.deltaTime, 2))
|
{
|
transform.position = new Vector3(_current.x, transform.position.y, _current.z);
|
m_CurrentIndex += 1;
|
|
if (m_CurrentIndex > m_Path.corners.Length - 1)
|
{
|
m_IsStartFinding = false;
|
m_Animator.SetInteger(GAStaticDefine.Param_Action, GAStaticDefine.Act_Idle);
|
}
|
}
|
}
|
}
|
}
|