unity3D射线检测持续检测是否发生碰撞2D,3D

1:3D场景中

 首先新建场景,创建Plan并设置层级为Ground ,再胶囊体并设置层级为Player。并在Player内创建一个空物体子集Feet并加上碰撞体 并为其添加Feet脚本。

layer选要检测的层级本例为ground切不可勾选自己的层级,因为Raycast是返回检测到的第一个物体,射线是从物体内发射出的,返回第一个物体会只检测到Player。

 Player拖拽胶囊体

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Feet : MonoBehaviour
{

    public static bool isGround;
    public LayerMask layer;
    public Transform Player;


    private void FixedUpdate()
    {
        Ray ray = new Ray(transform.position, Vector3.down);
        RaycastHit hit;
        isGround = Physics.Raycast(ray, out hit, 0.65f, layer);
        Debug.DrawRay(ray.origin, ray.direction * 20, Color.red);
        print(isGround);
        if (hit.collider != null)
        {
            print(hit.collider.name);
        }
    }

   

}

 创建Move脚本控制胶囊体的移动,并设置物体的移动速度跳跃速度

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Move : MonoBehaviour
{
    private bool isGroundM;
    public float JumpSpeed;
    public float Speed;
    private Rigidbody rg;

    // Start is called before the first frame update
    void Start()
    {
        rg = GetComponent<Rigidbody>();
       
        
    }

    // Update is called once per frame
    void Update()
    {
        float H = Input.GetAxis("Horizontal")*Speed*Time.deltaTime;
        float V = Input.GetAxis("Vertical") * Speed * Time.deltaTime;
        transform.Translate(-V, 0, H);

        //print(isGroundM);
        //print(Feet.isGround);
        if (Input.GetKeyDown(KeyCode.Space)&& Feet.isGround)
        {
            //print(1);
            rg.velocity  += new Vector3(0,  5 ,0);
            rg.AddForce(Vector3.up * JumpSpeed);
        }
       

    }

   public bool Isray(bool isground)
    {

        return isground;
    }
}

   运行后

结果:若在地面上持续返回True ,跳跃后返回false。


2:2D场景中

较3D简单用一段代码概括

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Feet : MonoBehaviour
{
    //设置负责记录是否接触层级的BOOl值
    public static bool isGrand;
    public static bool isGround;
    public static bool isMoveGround;
    private bool isGround2;

    //设置要检测的层级
    private LayerMask LOneWayPlatform;
    private LayerMask LGround;
    private LayerMask LMovePlant;
    private Rigidbody2D myFeet;

    private void Start()
    {
        LOneWayPlatform = 1 << 12;//表示只检测12层
        LGround= 1 << 8;
        LMovePlant = 1 << 14;
        myFeet = GetComponent<Rigidbody2D>();
    }
    private void FixedUpdate()
    {
        
        isGround = Physics2D.Raycast(transform.position, Vector2.down, 1f, LOneWayPlatform);
        isGround2 = Physics2D.Raycast(transform.position, Vector2.down, 1f, LGround);
        isMoveGround= Physics2D.Raycast(transform.position, Vector2.down, 1f, LMovePlant);
        
        //print(isGround);
        //print(isGround2);
        CheckGrounded();
    }

    void CheckGrounded()
    {
        //是否接触地面
        isGrand = isGround|| isGround2|| isMoveGround;
        //print(isGrand);
    }

}

3:结束语

这个主要用于检验一些角色的2段跳,和动画切换等

  • 1
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:游动-白 设计师:我叫白小胖 返回首页

打赏作者

生命不止学习不息

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值