using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private Item netItem;
public bool ForceLocal;
public bool IsLocal
{
get
{
return ForceLocal || (netItem != null && netItem.IsLocal);
}
}
private UIHookup uiHookup;
private CharacterController charControl;
// Start is called before the first frame update
void Awake()
{
netItem = GetComponentInChildren<Item>();
castLight = transform.Find("CastLight").gameObject;
uiHookup = FindObjectOfType<UIHookup>();
if (ForceLocal)
{
StartLocal();
}
}
private GameObject castLight;
private float bottomOffset;
void StartLocal()
{
castLight.SetActive(true);
charControl = GetComponentInChildren<CharacterController>();
charControl.enabled = true;
bottomOffset = -charControl.center.y + charControl.height * 0.5f;
}
public float Speed;
private List<Touch> touches = new List<Touch>();
public float MoveTouchFullZone = 0.7f;
public float MoveTouchRadiusSize = 1.45f;
// Update is called once per frame
void Update()
{
if (IsLocal && charControl != null)
{
RaycastHit hit;
if (Physics.Raycast(new Ray(transform.position, Vector3.down), out hit, 10f))
{
Vector3 pos = transform.position;
pos.y = hit.point.y + (bottomOffset + 0.01f);
transform.position = pos;
}
Vector3 wantMove = new Vector3();
if (Input.GetKey(KeyCode.UpArrow))
{
wantMove.z += 1;
}
if (Input.GetKey(KeyCode.DownArrow))
{
wantMove.z -= 1;
}
if (Input.GetKey(KeyCode.RightArrow))
{
wantMove.x += 1;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
wantMove.x -= 1;
}
wantMove.Normalize();
if (uiHookup != null && uiHookup.MoveTouch != null && uiHookup.MainCanvas != null)
{
float radius = uiHookup.MoveTouch.rect.width * 0.5f * MoveTouchRadiusSize;
touches.Clear();
touches.AddRange(Input.touches);
if (Input.GetMouseButton(0))
{
touches.Add(new Touch { position = Input.mousePosition });
}
foreach (Touch touch in touches)
{
Vector2 diff = new Vector2();
RectTransformUtility.ScreenPointToLocalPointInRectangle(uiHookup.MoveTouch, touch.position, null, out diff);
if (diff.magnitude < radius)
{
wantMove = new Vector3(diff.x, 0, diff.y) / (radius * MoveTouchFullZone);
if (wantMove.magnitude > 1) wantMove.Normalize();
}
}
}
if (wantMove.sqrMagnitude > 0)
{
wantMove *= Speed * Time.deltaTime;
wantMove.y = -0.1f * Time.deltaTime;
CollisionFlags flags = charControl.Move(wantMove);
}
}
}
}