Unity 获取点的法线方向实现吸附效果

通过HTCVive手柄发射射线控制物体的吸附效果,类似于帖在墙壁上的画框。

1.首先编写手柄上的控制类:SteamVRControllerBase。通过手柄射线获得碰撞点的位置和法线方向

using UnityEngine;
using System.Collections;

public class SteamVRControllerBase : MonoBehaviour {

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

    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))
        {
            sphere.position = hitInfo.point;         //复制位置
            sphereNormal = hitInfo.normal;           //获得法线方向
        }
    }
    //获取位置来控制画框的位置
    public Transform GetSphereTrans()
    {
        return sphere.transform;
    }
    //获取法线方向
    public Vector3 GetSphereNormal()
    {
        return sphereNormal;
    }
}

2.给画框添加吸附的控制类:AdsorbController。这样画框就能根据碰撞点的位置及时的吸附在墙壁上

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

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


  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YanisWu

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

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值