_xiaofang/xiaofang/Assets/Script/Character/Skills/Skill_Pick.cs

105 lines
3.1 KiB
C#
Raw Normal View History

2024-10-21 10:08:49 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Skill_Pick : MonoBehaviour
{
//private CharacterAin CharacterAin;
//<2F><>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڻ<EFBFBD>ȡ<EFBFBD><C8A1>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Transform<72><6D><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private Animator CharacterAnimator;
private Transform rightHand, leftHand;
//<2F><>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public Vector3 relativePosition;
//<2F><>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת
public Vector3 relativeRotation;
// <20><><EFBFBD>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҹ<EFBFBD><D2B8><EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD>ķ<EFBFBD>Χ
public float pickUpRange = 2.0f;
// Start is called before the first frame update
void Start()
{
CharacterAnimator = transform.GetComponent<Animator>();
if (CharacterAnimator != null)
{
rightHand = CharacterAnimator.GetBoneTransform(HumanBodyBones.RightHand);
leftHand = CharacterAnimator.GetBoneTransform(HumanBodyBones.LeftHand);
}
else
{
Debug.LogError("<22>Ҳ<EFBFBD><D2B2><EFBFBD> Animator ");
}
}
// <20><><EFBFBD><EFBFBD>
public GameObject Pick()
{
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>ҵ<EFBFBD>ǰλ<C7B0><CEBB>
Vector3 playerPosition = transform.position;
// ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߻<EFBFBD><DFBB>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҹ<EFBFBD><D2B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ
Collider[] itemsInRange = Physics.OverlapSphere(playerPosition, pickUpRange, LayerMask.GetMask("CanPick"));
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E2B5BD><EFBFBD><EFBFBD>Ʒ
foreach (Collider itemCollider in itemsInRange)
{
// ȷ<><C8B7><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD>𣨿<EFBFBD><F0A3A8BF><EFBFBD>ͨ<EFBFBD><CDA8>ij<EFBFBD><C4B3>״̬<D7B4><CCAC><EFBFBD><EFBFBD>ǩ<EFBFBD><C7A9>
if (itemCollider.CompareTag("CanPick") && !itemCollider.isTrigger)
{
// ִ<>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD>Ǽ򵥵ؽ<F2B5A5B5><D8BD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD>ҵ<EFBFBD><D2B5>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD><EFBFBD>
GameObject item = itemCollider.gameObject;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD>ײ<EFBFBD><EFBFBD><E5A3AC>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ
itemCollider.enabled = false;
// <20><><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6>󣬲<EFBFBD><F3A3ACB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD>ú<EFBFBD><C3BA><EFBFBD>ת
item.transform.SetParent(rightHand, false); // false<73><65>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>λ<EFBFBD><CEBB>
item.transform.localPosition = relativePosition; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>Ĭ<EFBFBD>ϵij<CFB5><C4B3><EFBFBD>λ<EFBFBD><CEBB>
item.transform.localRotation = Quaternion.Euler(relativeRotation); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת
// <20><><EFBFBD>µ<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>е<EFBFBD><D0B5><EFBFBD>Ʒ
return item;
}
}
return null;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ
public void Drop(GameObject currentItem)
{
// <20><><EFBFBD>鵱ǰ<E9B5B1>Ƿ<EFBFBD><C7B7>г<EFBFBD><D0B3>е<EFBFBD><D0B5><EFBFBD>Ʒ
if (currentItem == null)
{
return;
}
// <20><><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƴ<EFBFBD>
Transform itemTransform = currentItem.transform;
itemTransform.SetParent(null); // <20><><EFBFBD><EFBFBD>Ϊnull<6C><6C>ʾû<CABE>и<EFBFBD><D0B8><EFBFBD><EFBFBD><EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2>
Collider itemCollider = currentItem.GetComponent<Collider>();
if (itemCollider != null)
{
itemCollider.enabled = true;
}
// <20><><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD>ǰλ<C7B0>õĵ<C3B5><C4B5><EFBFBD><EFBFBD><EFBFBD>
Vector3 dropPosition = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
itemTransform.position = dropPosition;
itemTransform.rotation = Quaternion.identity; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>е<EFBFBD><D0B5><EFBFBD>Ʒ
currentItem = null;
}
}