首页 > 其他分享 >3d的单向平台检测

3d的单向平台检测

时间:2023-02-27 11:24:34浏览次数:40  
标签:direction collider 检测 Vector3 单向 transform private other 3d

https://www.youtube.com/watch?v=qwwjwb7XlUc

using UnityEngine;

[RequireComponent(typeof(BoxCollider))]
public class OneWayBoxCollider : MonoBehaviour
{
    /// <summary> The direction that the other object should be coming from for entry. </summary>
    [SerializeField] private Vector3 entryDirection = Vector3.up;
    /// <summary> Should the entry direction be used as a local direction? </summary>
    [SerializeField] private bool localDirection = false;
    /// <summary> How large should the trigger be in comparison to the original collider? </summary>
    [SerializeField] private Vector3 triggerScale = Vector3.one * 1.25f;
    /// <summary> The original collider. Will always be present thanks to the RequireComponent attribute. </summary>
    private new BoxCollider collider = null;
    /// <summary> The trigger that we add ourselves once the game starts up. </summary>
    private BoxCollider collisionCheckTrigger = null;

    /// <summary> The entry direction, calculated accordingly based on whether it is a local direction or not. 
    /// This is pretty much what I've done in the video when copy-pasting the local direction check, but written as a property. </summary>
    public Vector3 Direction => localDirection ? transform.TransformDirection(entryDirection.normalized) : entryDirection.normalized;

    private void Awake()
    {
        collider = GetComponent<BoxCollider>();

        // Adding the BoxCollider and making sure that its sizes match the ones
        // of the OG collider.
        collisionCheckTrigger = gameObject.AddComponent<BoxCollider>();
        collisionCheckTrigger.size = new Vector3(
            collider.size.x * triggerScale.x,
            collider.size.y * triggerScale.y,
            collider.size.z * triggerScale.z
        );
        collisionCheckTrigger.center = collider.center;
        collisionCheckTrigger.isTrigger = true;
    }

    private void OnValidate()
    {
        // This bit of code exists only to prevent OnDrawGizmos from throwing
        // errors in the editor mode when it does not have reference to the
        // collider, if used.
        collider = GetComponent<BoxCollider>();
        collider.isTrigger = false;
    }

    private void OnTriggerStay(Collider other)
    {
        // Simulate a collision between our trigger and the intruder to check
        // the direction that the latter is coming from. The method returns true
        // if any collision has been detected.
        if (Physics.ComputePenetration(
            collisionCheckTrigger, transform.position, transform.rotation,
            other, other.transform.position, other.transform.rotation,
            out Vector3 collisionDirection, out float _))
        {
            float dot = Vector3.Dot(Direction, collisionDirection);

            // Opposite direction; passing not allowed.
            if (dot < 0)
            {
                // Making sure that the two object are NOT ignoring each other.
                Physics.IgnoreCollision(collider, other, false);
            }
            else
            {
                // Making the colliders ignore each other, and thus allow passage
                // from one way.
                Physics.IgnoreCollision(collider, other, true);
            }
        }
    }

    private void OnDrawGizmosSelected()
    {
        // Instead of directly using the transform.position, we're using the
        // collider center, converted into global position. The way I did it
        // in the video made it easier to write, but the on-screen drawing would
        // not take in consideration the actual offset of the collider.
        Gizmos.color = Color.red;
        Gizmos.DrawRay(transform.TransformPoint(collider.center), Direction * 2);

        Gizmos.color = Color.green;
        Gizmos.DrawRay(transform.TransformPoint(collider.center), -Direction * 2);
    }
}

 

标签:direction,collider,检测,Vector3,单向,transform,private,other,3d
From: https://www.cnblogs.com/sanyejun/p/17159003.html

相关文章

  • 3DMAX安装失败怎么办?安装3DMAX失败提示错误怎么解决?
    3DMAX安装失败怎么办?安装3DMAX失败提示错误怎么解决?有很多同学想把3DMAX卸载后重新安装,但是发现3DMAX安装到一半就失败了或者显示3DMAX已安装或者安装未完成,大多数情况下......
  • 「解题报告」ARC133D Range XOR
    不会做。考虑拆分成前缀和的形式。设\(w_i=1\oplus2\oplus\cdots\oplusi\),打表容易发现:\[w_i=\begin{cases}i&i\equiv0\pmod4\\1&i\equiv1\pmod......
  • STATA: ssc describe d 检测所有能通过ssc安装的以d开头的命令列表
    //检测所有能通过ssc安装的以d开头的命令列表sscdescribed//-------------------------------------------------------------------------------------------------......
  • 解决d3dcompiler_33.dll找不到的问题
    其实很多用户玩单机游戏或者安装软件的时候就出现过这种问题,如果是新手第一时间会认为是软件或游戏出错了,其实并不是这样,其主要原因就是你电脑系统的该dll文件丢失了或者损......
  • 触觉仿真系统:Force Dimension+CHAI 3D
    推荐:将 NSDT场景编辑器 加入你的3D开发工具链ForceDimension成立于2001年,总部在瑞士,比sensable晚了8年,开发的理念也不一样,他们开发是连杆式力反馈触觉系统,仿真系统是......
  • STATA检测指定观测值是否连续
    检测变量名filter观测值不连续,而filter2观测值是连续的listfilter2iffilter2!=(filter2[_n-1]+1)in2/L//检测变量名filter2的值是否连续://如果所有的取值是连续的......
  • 【牛客】4 序列检测&时序逻辑
    VL25 输入序列连续的序列检测这种题用移位寄存器是最方便的,用状态机会麻烦很多。`timescale1ns/1nsmodulesequence_detect(inputclk,inputrst_n,i......
  • iOS卡顿检测方案
    方案一:基于RunLoop主线程绝大部分计算或者绘制任务都是以Runloop为单位发生。单次Runloop如果时长超过16ms,就会导致UI体验的卡顿。那如何检测单次Runloop的耗时呢?Runloop的......
  • 【目标检测】重读经典之 SSD: Single Shot MultiBox Detector
    原始题目SSD:SingleShotMultiBoxDetector中文名称SSD:一阶段多框检测器发表时间2015年12月8日平台ECCV2016来源北卡罗来纳大学教堂山分校......
  • Direct3D Compute Shader基础
    从DirectX11.0版本(即SM5.0,需win7及以上)开始,引入ComputeShader(计算着色器)来进行GPU编程。ComputeShader不属于图形渲染管线的一个步骤,使得开发者可以脱离图形渲染管线的......