_xiaofang/xiaofang/Assets/Script/Character/Skills/Skill_Pick.cs
2024-12-04 10:58:48 +08:00

117 lines
3.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Skill_Pick : MonoBehaviour
{
//private CharacterAin CharacterAin;
//角色动画控制器用于获取角色具体身体Transform捡东西
public Animator CharacterAnimator;
public Transform player;
private Transform rightHand, leftHand;
//物品的相对坐标
public Vector3 relativePosition;
//物品的相对旋转
public Vector3 relativeRotation;
// 用于检测玩家附近物品的范围
public float pickUpRange = 2.0f;
void Start()
{
//CharacterAnimator = transform.GetComponent<Animator>();
if (CharacterAnimator != null)
{
rightHand = CharacterAnimator.GetBoneTransform(HumanBodyBones.RightHand);
leftHand = CharacterAnimator.GetBoneTransform(HumanBodyBones.LeftHand);
}
else
{
Debug.LogError(transform.name+"找不到 Animator ");
}
}
// 捡起
public GameObject Pick()
{
// 获取玩家当前位置
Vector3 playerPosition = player.transform.position;
// 使用物理射线或重叠球检测来查找玩家附近的物品
Collider[] itemsInRange = Physics.OverlapSphere(playerPosition, pickUpRange, LayerMask.GetMask("Default"));
// 遍历检测到的物品
foreach (Collider itemCollider in itemsInRange)
{
// 确保物品可以被捡起(可能通过某种状态或标签)
if (itemCollider.CompareTag("CanPick") && !itemCollider.isTrigger)
{
// 执行捡起动作(这里只是简单地将物品设置为玩家的子对象)
GameObject item = itemCollider.gameObject;
// 禁用物品的碰撞体,防止它与玩家或其他物体碰撞
itemCollider.enabled = false;
// 将物品设置为玩家手部的子对象,并调整其位置和旋转
item.transform.SetParent(rightHand, true); // false表示保持物品的世界旋转和位置
item.transform.localPosition = relativePosition; // 假设玩家手中有一个默认的持物位置
item.transform.localRotation = Quaternion.Euler(relativeRotation); // 重置旋转
Debug.Log("^^^^^^^^^^^^^^^^^^^^^^^^^");
// 更新当前持有的物品
return item;
}
}
Debug.Log("############################");
return null;
}
// 丢弃物品
public GameObject Drop(GameObject currentItem)
{
// 检查当前是否有持有的物品
if (currentItem == null)
{
return null;
}
// 将物品从玩家的子对象中移除
Transform itemTransform = currentItem.transform;
itemTransform.SetParent(null); // 设置为null表示没有父对象
// 重新启用物品的碰撞器
Collider itemCollider = currentItem.GetComponent<Collider>();
if (itemCollider != null)
{
itemCollider.enabled = true;
}
// 将物品放置在玩家当前位置的地面上
Vector3 dropPosition = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
itemTransform.position = dropPosition;
itemTransform.rotation = Quaternion.identity; // 重置旋转
// 清除当前持有的物品
currentItem = null;
return currentItem;
}
}