Animator.SetTrigger没有在多人游戏中调用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Animator.SetTrigger没有在多人游戏中调用相关的知识,希望对你有一定的参考价值。
我和一个朋友正在使用镜子制作多人统一游戏。当我调用播放器动画时,除了行animator.SetTrigger(“ Melee”);之外,所有动画均有效。当其他玩家单击鼠标左键时,它会显示在他们的屏幕上,但不会出现在我的屏幕上,反之亦然。
我们几乎已经尝试了所有解决方法,但是没有运气。我们正在使用NetworkAnimator,该协同例程已成功调用,但仍无法正常工作。
我们在做什么错?
private float X;
private float Y;
[Header("Settings")]
public float Sensitivity;
public float Speed;
public float JumpForce;
public float health;
[Header("Components")]
public GameObject head;
private Rigidbody rb;
private Vector3 JumpForceVector;
public bool canJump = true;
public Camera cam;
public Animator animator;
private GameObject item;
public Transform itemPosition;
public TextMeshProUGUI hintText;
public GameObject rightArm;
private bool lookingAtHoldableObject = false;
public bool canAttack = true;
public float punchDamage = 1f;
private float meleeDamage;
public NetworkIdentity Identity;
void Start()
{
rb = GetComponent<Rigidbody>();
JumpForceVector = new Vector3(0f, JumpForce, 0f);
meleeDamage = punchDamage;
}
void Awake()
{
Vector3 euler = transform.rotation.eulerAngles;
X = euler.x;
Y = euler.y;
}
void shootLookRay()
{
RaycastHit hit;
Debug.DrawRay(cam.transform.position, cam.transform.forward*100, Color.red);
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit))
{
if (hit.collider.gameObject != item && hit.collider.gameObject.GetComponent<Holdable>() != null && Vector3.Distance(hit.collider.gameObject.transform.position, transform.position) < 5f)
{
lookingAtHoldableObject = true;
hintText.text = $"Pick Up {hit.collider.gameObject.name} (E)";
if (Input.GetKeyUp(KeyCode.E))
{
if (item != null)
{
item.transform.SetParent(null);
meleeDamage = punchDamage;
}
animator.SetBool("HasItem", true);
item = hit.collider.gameObject;
item.transform.SetParent(itemPosition);
item.transform.position = itemPosition.position + hit.collider.gameObject.GetComponent<Holdable>().offset;
item.transform.localEulerAngles = item.GetComponent<Holdable>().rotation;
if (item.GetComponent<Holdable>().itemClass == Holdable.itemClasses.Melee)
{
meleeDamage = item.GetComponent<Holdable>().damage;
}
}
}
else
{
lookingAtHoldableObject = false;
hintText.text = string.Empty;
}
}
}
void look()
{
const float MIN_X = 0.0f;
const float MAX_X = 360.0f;
const float MIN_Y = -42;
const float MAX_Y = 43.809f;
Cursor.lockState = CursorLockMode.Confined;
Cursor.visible = false;
X += Input.GetAxis("Mouse X") * (Sensitivity * Time.deltaTime);
if (X < MIN_X) X += MAX_X;
else if (X > MAX_X) X -= MAX_X;
Y -= Input.GetAxis("Mouse Y") * (Sensitivity * Time.deltaTime);
if (Y < MIN_Y) Y = MIN_Y;
else if (Y > MAX_Y) Y = MAX_Y;
transform.rotation = Quaternion.Euler(0.0f, X, 0.0f);
head.transform.rotation = Quaternion.Euler(Y, X, 0.0f);
}
public IEnumerator Attack()
{
canAttack = false;
animator.SetTrigger("Melee");
yield return new WaitForSeconds(0.15f);
canAttack = true;
}
void Update()
{
if (!Identity.isLocalPlayer)
{
cam.enabled = false;
return;
}
else
{
cam.enabled = true;
}
if (Input.GetMouseButton(0) && canAttack)
{
StartCoroutine(Attack());
}
if (Input.GetKey(KeyCode.W))
{
rb.AddForce(transform.forward * Speed, ForceMode.Impulse);
}
if (Input.GetKey(KeyCode.A))
{
rb.AddForce(-transform.right * Speed, ForceMode.Impulse);
}
if (Input.GetKey(KeyCode.S))
{
rb.AddForce(-transform.forward * Speed, ForceMode.Impulse);
}
if (Input.GetKey(KeyCode.D))
{
rb.AddForce(transform.right * Speed, ForceMode.Impulse);
}
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))
{
animator.SetTrigger("Walk");
}
else
{
if (canAttack)
animator.SetTrigger("Idle");
}
if (Input.GetKey(KeyCode.E) && !lookingAtHoldableObject && item != null)
{
animator.SetBool("HasItem", false);
item.transform.SetParent(null);
item = null;
}
if (canJump && Input.GetKey(KeyCode.Space))
{
rb.AddForce(0f, JumpForce, 0f, ForceMode.Impulse);
}
if (item != null)
{
item.transform.position = itemPosition.position + item.GetComponent<Holdable>().offset;
}
shootLookRay();
look();
}
答案
您需要做的就是包装一个if语句,该语句表示identity.isLocalPlayer
以上是关于Animator.SetTrigger没有在多人游戏中调用的主要内容,如果未能解决你的问题,请参考以下文章