• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

  • 共151篇

    Unity - 游戏引擎

关闭

返回栏目

关闭

返回Unity - 游戏引擎栏目

127 - Player - PlayerAttack.cs - 玩家_攻击

作者:

贺及楼

成为作者

更新日期:2023-09-17 11:00:19

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PlayerAttack : MonoBehaviour {
  5. public GameObject arrowPrefab;
  6. private Animator anim;
  7. private Transform leftHandTrans;
  8. private Vector3 shootDir;
  9. private PlayerManager playerMng;
  10. // Use this for initialization
  11. void Start () {
  12. anim = GetComponent<Animator>();
  13. leftHandTrans = transform.Find("Bip001/Bip001 Pelvis/Bip001 Spine/Bip001 Neck/Bip001 L Clavicle/Bip001 L UpperArm/Bip001 L Forearm/Bip001 L Hand");
  14. }
  15. // Update is called once per frame
  16. void Update () {
  17. if (anim.GetCurrentAnimatorStateInfo(0).IsName("Grounded"))
  18. {
  19. if (Input.GetMouseButtonDown(0))
  20. {
  21. Ray ray= Camera.main.ScreenPointToRay(Input.mousePosition);
  22. RaycastHit hit;
  23. bool isCollider = Physics.Raycast(ray, out hit);
  24. if (isCollider)
  25. {
  26. Vector3 targetPoint = hit.point;
  27. targetPoint.y = transform.position.y;
  28. shootDir = targetPoint - transform.position;
  29. transform.rotation = Quaternion.LookRotation(shootDir);
  30. anim.SetTrigger("Attack");
  31. Invoke("Shoot", 0.1f);
  32. }
  33. }
  34. }
  35. }
  36. public void SetPlayerMng(PlayerManager playerMng)
  37. {
  38. this.playerMng = playerMng;
  39. }
  40. private void Shoot()
  41. {
  42. playerMng.Shoot(arrowPrefab, leftHandTrans.position, Quaternion.LookRotation(shootDir));
  43. }
  44. }