Unity物体表面吸附

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一、实现效果

请添加图片描述

二、代码

1.模拟VR

代码如下:

using UnityEngine;
using System.Collections;

public class SteamVRControllerBase : MonoBehaviour
{

    public static SteamVRControllerBase getInstance;
    private Transform sphere;          //点的位置,使用一个小球
    private Vector3 sphereNormal = Vector3.zero;      //点的法线方向
    public LayerMask _groundLayerMask;

    private void Awake()
    {
        getInstance = this;
        sphere = transform.Find("Sphere");
    }

    RaycastHit hitInfo;

    private void FixedUpdate()
    {
        Ray ray = new Ray(transform.position, transform.forward);
        if (Physics.Raycast(ray, out hitInfo, 1000f, _groundLayerMask))
        {
            sphere.position = hitInfo.point;         //复制位置
            sphereNormal = hitInfo.normal;           //获得法线方向
        }
    }
    //获取位置来控制画框的位置
    public Transform GetSphereTrans()
    {
        return sphere.transform;
    }
    //获取法线方向
    public Vector3 GetSphereNormal()
    {
        return sphereNormal;
    }
}

2.计算目标物体大小

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


public class AdsorbController : MonoBehaviour
{

    private Collider col;                 //通过碰撞体获得大小
    private Vector3 Velocity = Vector3.zero;
    private float smoothTime = 0.1f;

    private Vector3 length;                 //物体长宽高

    private void Start()
    {
        col = GetComponent<Collider>();
        length = col.bounds.size;
    }
    private void Update()
    {
        Vector3 targetPos = SteamVRControllerBase.getInstance.GetSphereTrans().position;
        //法线的起点需要向前移动半个物体宽度的距离,因为画框中心点在中间位置,否则会嵌入一半墙体
        Debug.Log(targetPos.x - (length.x * transform.lossyScale.x) / 2);
        targetPos = new Vector3(targetPos.x - (length.x * transform.lossyScale.x) / 2, targetPos.y - (length.y * transform.lossyScale.y) / 2, targetPos.z - (length.z * transform.lossyScale.z) / 2);
        //使用阻尼函数平滑移动效果
        transform.position = Vector3.SmoothDamp(transform.position, targetPos, ref Velocity, smoothTime);
        Vector3 direction = SteamVRControllerBase.getInstance.GetSphereNormal();         //获取法线方向
        Debug.DrawRay(transform.position, direction);       //画出法线,用来测试
        transform.forward = direction;               //让画框前方与法线方向一致
    }
}

源码

链接:https://pan.baidu.com/s/1wHPHzcVTo0gPHQuGhaq6xg
提取码:5xj7

  • 18
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Unity黑洞吸附是指在Unity游戏引擎中,利用编程技术实现的一种物理效果,即当游戏角色或物体进入黑洞范围时,会被黑洞吸附并被拉进黑洞的中心位置。 在实现这一效果的过程中,首先需要在Unity中创建一个黑洞物体,通常是一个具有重力属性的空心球体。接着,为黑洞物体添加一个吸附组件,这个组件将负责计算吸附物体的位置和速度变化。 当一个物体进入黑洞的感应范围时,通常会通过碰撞检测或者物体触发器来实现,黑洞吸附组件会开始计算被吸附物体的位置和速度。这个计算过程可以根据黑洞的吸附能力和被吸附物体的质量来确定。 一般情况下,黑洞吸附效果应该包括以下几个要点:首先,被吸附物体会逐渐接近黑洞的中心,并且速度会逐渐加快。其次,黑洞的吸附范围应该是有限的,否则物体不能离开黑洞。最后,被吸附物体应该能够被黑洞完全吸进去,不再出现。 在实现黑洞吸附效果时,可以根据需求进行调整。例如,可以借助粒子效果来模拟黑洞的旋转和光影效果,增强黑洞的视觉效果和真实感。此外,还可以结合音效,使得黑洞吸附的过程更加生动有趣。 总而言之,Unity黑洞吸附是一种可以在游戏中实现的物理效果,它可以通过编程技术来实现,通过定义吸附范围和物体属性等参数来控制被吸附物体的位置和速度变化,从而实现黑洞吸附的效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值