当前位置: 首页 > article >正文

日志2025.1.30

日志2025.1.30
1.简略地做了一下交互系统
public class Interactable : MonoBehaviour
{
    private MeshRenderer renderer;
    private Material defaultMaterial;
    public Material highlightMaterial;

    private void Awake()
    {
        renderer = GetComponentInChildren<MeshRenderer>();
        defaultMaterial = renderer.material;
    }

    public virtual void Interaction()
    {
        Debug.Log("www");
    }

    public void HightLightActive(bool active)
    {
        if (active)
        {
            renderer.material = highlightMaterial;
        }
        else
        {
            renderer.material = defaultMaterial;
        }
    }

    protected virtual void OnTriggerEnter(Collider other)
    {
        PlayerInteraction playerInteraction = other.GetComponent<PlayerInteraction>();

        if (playerInteraction == null) return;

        playerInteraction.interactables.Add(this);

        playerInteraction.ChangeClosestInteractable();
    }

    protected virtual void OnTriggerExit(Collider other)
    {
        PlayerInteraction playerInteraction = other.GetComponent<PlayerInteraction>();

        if (playerInteraction == null) return;

        playerInteraction.interactables.Remove(this);

        playerInteraction.ChangeClosestInteractable();
    }
}


public class PlayerInteraction : MonoBehaviour
{
    public List<Interactable> interactables;

    private Interactable closestInteractable;

    private Player player;

    private void Awake()
    {
        player = GetComponent<Player>();
        PlayerControls controls = player.controls;
        controls.Character.Interaction.performed += context => InteractWithClosest();
    }

    private void InteractWithClosest()
    {
        closestInteractable?.Interaction();
    }

    //找到最近的可互动物,并将其高光
    public void ChangeClosestInteractable()
    {
        closestInteractable?.HightLightActive(false);
        closestInteractable = null;

        float closestDistance = float.MaxValue;

        foreach (Interactable interactable in interactables)
        {
            float distance = Vector3.Distance(transform.position, interactable.transform.position);

            if (distance < closestDistance) { 
                closestDistance = distance;
                closestInteractable = interactable;
            }
        }

        closestInteractable?.HightLightActive(true);

    }

}


2.将该交互系统用于拾取武器
public class Pickup_Weapon : Interactable
{
    private PlayerWeaponController playerWeaponController;
    [SerializeField] private Weapon_Data weaponData;


    public override void Interaction()
    {
        base.Interaction();

        playerWeaponController.PickUpWeapon(weaponData);

    }

    protected override void OnTriggerEnter(Collider other)
    {
        base.OnTriggerEnter(other);

        if (playerWeaponController == null)
        {
            playerWeaponController = other.GetComponent<PlayerWeaponController>();
        }

    }

    protected override void OnTriggerExit(Collider other)
    {
        base.OnTriggerExit(other);

    }

}


http://www.kler.cn/a/526485.html

相关文章:

  • android 圆形弹窗摄像头开发踩坑——源码————未来之窗跨平台操作
  • 论文阅读(七):贝叶斯因果表型网络解释遗传变异和生物学知识
  • 如何将DeepSeek部署到本地电脑
  • C++ 3
  • 2501,20个窗口常用操作
  • YOLOv10 介绍
  • 【深度分析】DeepSeek 遭暴力破解,攻击 IP 均来自美国,造成影响有多大?有哪些好的防御措施?
  • Spring AI 与企业级应用架构的结合
  • 举例说明python单利模式的必要性
  • 数论问题80
  • floodfill算法(6题)
  • Node.js——模块化(模块的基本概念、模块化的规范、包与NPM)
  • 傅里叶分析之掐死教程
  • Zookeeper入门部署(单点与集群)
  • 《Chart.js 饼图:深度解析与最佳实践指南》
  • 【新春特辑】2025年1月科技浪潮中的AI最新时事与科技趋势
  • autosar bsw 的关键模块
  • Nuitka打包python脚本
  • C++中常用的十大排序方法之1——冒泡排序
  • CF 761A.Dasha and Stairs(Java实现)
  • deb安装失败后,无法再安装别的包的解决方案
  • MyBatis 入门
  • 深度学习 Pytorch 神经网络的损失函数
  • AIGC(生成式AI)试用 20 -- deepseek 初识
  • 2024-10-26 进程间通信
  • Python 梯度下降法(三):Adagrad Optimize