用手柄操控切割物体(未完待续)

识别手柄按键

根据steamvr_input窗口进行动作名称设置
根据steamvr_input窗口进行动作名称设置
GrabPinch:扳机键
InteractUI:扳机键(带震动)
Teleport:圆形面板
GrabGrip:手柄两侧键

	public SteamVR_Input_Sources handtype;//定义信号源
    public SteamVR_Action_Boolean grabAction;//定义抓取功能
    
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        
        

        if (grabAction.GetStateDown(handtype))//grabAction是在控制面板中设置的动作类型
        {
            Debug.Log("testHand");
        }
	}

利用Ezyslicer插件进行切割

//碰撞检测 对source(待切割物体)进行赋值
private void OnTriggerEnter(Collider other)
    {
        Debug.Log("collision!");
        source = other.gameObject;//碰撞后赋值
    }

//将Souce切割,传入的第一个参数为切割的位置(即刀片的位置),传入的第二个参数为切割面的法向量(即刀片表面的法向量)
            SlicedHull hull = source.Slice(transform.position, transform.up);
 
            //创建把source切割以后的上半部分物体
            hull.CreateUpperHull(source);
            //创建把source切割以后的下半部分物体
            hull.CreateLowerHull(source);
 
            //因为是新建切出来两个物体,因此要把原来的物体关闭

问题1:

当脚本中添加切割脚本时 OnTriggerEnter()正常进行赋值 Slicer不能被手柄抓取(还未解决该问题)

猜想:

可能是由于碰撞器的缘故

解决:

问题在于碰撞检测采用的OnTriggerEnter 并勾选了IsTrigger 所以不存在collision碰撞检测 所以当手柄碰撞到Slicer时并没有使其边框变黄

所以采用 OnHandHover()进行拾取

    public bool isChosen;

    private Hand.AttachmentFlags attachmentFlags = Hand.defaultAttachmentFlags & (~Hand.AttachmentFlags.SnapOnAttach) & (~Hand.AttachmentFlags.DetachOthers) & (~Hand.AttachmentFlags.VelocityMovement);//检测是否有被抓取物体
    private Interactable interactable;

    // Use this for initialization
    void Start () {
        isChosen = false;

    }
    private void Awake()
    {
        interactable = this.GetComponent<Interactable>();//获取交互组件   
    }
    // 在这个物体上悬停时被调用
  
    private void OnHandHoverBegin(Hand hand)
    {   
    }
    // Called when a Hand stops hovering over this object
   	 private void OnHandHoverEnd(Hand hand)//结束悬停时操作
    {
       Debug.Log("HandHovering is over");
    }
    //悬停时均调用
    private void HandHoverUpdate(Hand hand)
    {
        //trackedObj = hand.GetComponent<SteamVR_Behaviour_Pose>();
        GrabTypes startingGrabType = hand.GetGrabStarting();
        bool isGrabEnding = hand.IsGrabEnding(this.gameObject);

        if (SteamVR_Actions.default_InteractUI.GetStateDown(SteamVR_Input_Sources.Any))
        {
            isChosen = true;//被选中状态   
        }
        if (interactable.attachedToHand == null && startingGrabType != GrabTypes.None)   
        //抓取开始,被抓取物体绑定在手柄上
        {
            

            // Call this to continue receiving HandHoverUpdate messages,
            // and prevent the hand from hovering over anything else
            hand.HoverLock(interactable);

            // Attach this object to the hand
            hand.AttachObject(gameObject, startingGrabType, attachmentFlags);
        }
        else if (isGrabEnding)
        {
            // Detach this object from the hand
            hand.DetachObject(gameObject);

            // Call this to undo HoverLock
            hand.HoverUnlock(interactable);

        }
    private void OpenChosenObjectMenu()
    {

    }
    private void OpenChosenToolMenu()
    {

    }
 // 物体靠近Hand时被调用
    private void OnAttachedToHand(Hand hand)                                //被手柄抓取
    {
        isChosen = true;

        
    private void OnDetachedFromHand(Hand hand)                              //被手柄释放
    {
        isChosen = false;
    }

问题2

  • 加入Player预制体,正常设置,但是手柄距离视野太远
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值