167 lines
5.4 KiB
C#
167 lines
5.4 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public class Player : MonoBehaviour
|
||
{
|
||
float times;
|
||
|
||
public bool isOtherPlayer; //其他玩家
|
||
public string sn; //设备SN
|
||
public TextMeshProUGUI user; //体验者昵称
|
||
public uint human; //人类 表示用animator数组里面的第几个模模型 注意:不显示人就是查这个参数,本地只有2种人的类型,扫码那里要注意这个参数
|
||
public uint group; //分组名
|
||
public int x, y, z, q, w, e, r; //位置和旋转
|
||
public float LastTime ;
|
||
public GameObject[] meshRendererObj;//人物形态(大人,大人黄色,小孩,小孩黄色)
|
||
public GameObject[] manModelList; //人模型(大小,小孩)
|
||
public Animator[] animator; //人物动画(大人,大人黄色,小孩,小孩黄色)
|
||
int indexPlay = 0; //具体是哪个人物形态和动画
|
||
float manFirstHight = -1; //玩家初始的高度
|
||
float state = 1; //玩家状态(走,站,蹲)
|
||
float height = 0f; //文字高度
|
||
|
||
float distanceToTarget;
|
||
void Start()
|
||
{
|
||
times = Config.times;
|
||
//collider = this.GetComponent<CapsuleCollider>();
|
||
}
|
||
|
||
private uint lastHuman = 100; // 缓存上一次的索引
|
||
private void Update()
|
||
{
|
||
if (Time.time - LastTime > 5)
|
||
{
|
||
if (gameObject.activeSelf)
|
||
{
|
||
gameObject.SetActive(false);
|
||
}
|
||
}
|
||
|
||
if (human != lastHuman)
|
||
{
|
||
for (int i = 0; i < manModelList.Length; i++)
|
||
{
|
||
manModelList[i].gameObject.SetActive(i == human);
|
||
}
|
||
|
||
if (human == 0)
|
||
{
|
||
height = 0f;
|
||
}
|
||
else
|
||
{
|
||
height = -0.3f;
|
||
}
|
||
|
||
//设置文字高度
|
||
//user.gameObject.transform.localPosition = new Vector3(user.gameObject.transform.localPosition.x, user.gameObject.transform.localPosition.y + height, user.gameObject.transform.localPosition.z);
|
||
lastHuman = human; // 更新缓存
|
||
}
|
||
|
||
|
||
if (isOtherPlayer)
|
||
{
|
||
if(manFirstHight == -1)//设置玩家初始高度
|
||
manFirstHight = y / times;
|
||
|
||
Vector3 tarpos = new Vector3(x / times, 0, z / times);
|
||
Quaternion tarqua = new Quaternion(q / times, w / times, e / times, r / times);
|
||
Vector3 tareu = tarqua.eulerAngles;
|
||
|
||
Vector3 currv = new Vector3(0, transform.eulerAngles.y, 0);
|
||
Quaternion currq = Quaternion.Euler(currv);
|
||
Vector3 targetv = new Vector3(0, tareu.y, 0);
|
||
Quaternion targetq = Quaternion.Euler(targetv);
|
||
|
||
|
||
//Debug.Log(sn+"收到的位置是:" + tarpos+"||收到的旋转是:"+ targetq);
|
||
|
||
//当初始值跟目标角度小于2,就将目标角度赋值给初始角度,让旋转角度是需要的角度
|
||
if (Quaternion.Angle(currq, targetq) < 2)
|
||
{
|
||
transform.rotation = targetq;
|
||
}
|
||
else
|
||
{
|
||
transform.rotation = Quaternion.Slerp(currq, targetq, 0.1f);
|
||
}
|
||
|
||
distanceToTarget = Vector3.Distance(transform.position, tarpos);
|
||
if (distanceToTarget > 0.1f)
|
||
{
|
||
// 位置变化幅度超过 10cm 的处理逻辑
|
||
//animator[human].SetBool("iswalk", true);
|
||
if (transform.position == Vector3.zero)
|
||
{
|
||
transform.position = tarpos;
|
||
}
|
||
else
|
||
{
|
||
transform.position = Vector3.Lerp(transform.position, tarpos, 0.02f);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
// 距离小于等于 10cm 时停止移动
|
||
transform.position = tarpos;
|
||
}
|
||
|
||
if (Config.Group == group) //同组
|
||
{
|
||
if (human == 0)//大人
|
||
{
|
||
indexPlay = 0;
|
||
meshRendererObj[human].gameObject.SetActive(true);
|
||
meshRendererObj[human + 1].gameObject.SetActive(false);
|
||
}
|
||
else //小孩
|
||
{
|
||
indexPlay = 2;
|
||
meshRendererObj[2].gameObject.SetActive(true);
|
||
meshRendererObj[3].gameObject.SetActive(false);
|
||
}
|
||
}
|
||
else //不同组
|
||
{
|
||
if (human == 0)//大人
|
||
{
|
||
indexPlay = 1;
|
||
meshRendererObj[human].gameObject.SetActive(false);
|
||
meshRendererObj[human + 1].gameObject.SetActive(true);
|
||
}
|
||
else //小孩
|
||
{
|
||
indexPlay = 3;
|
||
// Debug.Log("数组长度:"+ meshRendererObj.Length+"||"+ human);
|
||
meshRendererObj[2].gameObject.SetActive(false);
|
||
meshRendererObj[3].gameObject.SetActive(true);
|
||
}
|
||
}
|
||
|
||
state = y / times; //玩家在传位置的时候就用这个作为状态传的
|
||
|
||
//Debug.Log("最终状态是:"+ state);
|
||
|
||
if (state == 1)//人物站
|
||
{
|
||
animator[indexPlay].Play("01_Man_Idle");
|
||
}
|
||
else if (state == 2) //走
|
||
{
|
||
animator[indexPlay].Play("01_Man_Walk");
|
||
}
|
||
else //蹲
|
||
{
|
||
animator[indexPlay].Play("01_Man_Down");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
|
||
}
|
||
}
|
||
} |