如何在运行时统一捕捉两个对象? [英] how to snap two objects in runtime in unity?

查看:59
本文介绍了如何在运行时统一捕捉两个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是3d模型,我想将另一个类似的模型连接到顶部的银色连接器上侧面以及右侧的另一个模型(所以一定要帮助我捕捉它)我想知道如何在运行时将两个3D对象捕捉到一起.即,在播放"期间,用户必须能够向上,向下,向左,向右拖动,以将一个对象与另一个对象对齐,例如"lego".一个3D对象应该捕捉到另一个3D对象.我将如何实现呢?

this is the 3d model i wanted to connect another model like this to its silver connectors on top side and also another model to right side(so do help me to snap it)I want to know how to snap two 3D objects together in runtime. i.e during "play" the user must be able to dragup, down, left, right to snap one object with the other object .for example like "lego", ie. one 3D object should snap to another 3D object. How would I achieve this?

这是我用于拖动的代码:

This is the code I use for dragging:

using System.Collections;

using UnityEngine;

public class drag : MonoBehaviour {

    Vector3 dist;
    float posX;
    float PosY;
    void OnMouseDown()
    {
        dist = Camera.main.WorldToScreenPoint(transform.position);
        posX = Input.mousePosition.x - dist.x;
        PosY = Input.mousePosition.y - dist.y;
    }
    void OnMouseDrag()
    {
        Vector3 curPos = new Vector3(Input.mousePosition.x - posX, Input.mousePosition.y - PosY, dist.z);
        Vector3 worldPos = Camera.main.ScreenToWorldPoint(curPos);
        transform.position = worldPos;
    }
}

推荐答案

有很多方法可以实现此目的.这是我想出的第一种方法.如果我们分解了可能包含快照的内容,则可以汇总一个脚本以附加到每个可快照的子代:

There are many many ways to accomplish this. Here is the first method I came up with. If we break down what might comprise a snap, we can muster up a script to attach to each snappable child:

1)将标签"parentblock"分配给要拖动的对象.
2)将触发器对撞机附加到父对象和可捕捉子对象上.
3)当拖动的对象进入碰撞区域时,将其捕捉到父对象.
4)存储与父级的偏移量,以在对齐后保持其位置.

1) Assign tag "parentblock" to the object you are dragging around.
2) Attach a trigger collider to both the parent object and the snappable child object.
3) When the the dragged object enters the collision area, snap it to the parent.
4) Store the offset from the parent to maintain its position once snapped.

bool snapped = false;
GameObject snapparent; // the gameobject this transform will be snapped to
Vector3 offset; // the offset of this object's position from the parent

Update()
{
    if (snapped == true)
    {
        //retain this objects position in relation to the parent
        transform.position = parent.transform.position + offset;
    }
}

void OnTriggerEnter(Collider col)
{
    if (col.tag == "parentblock")
    {
        snapped = true;
        snapparent = col.gameObject;
        offset = transform.position - snapparent.transform.position; //store relation to parent
    }
}

请记住,这只会将子对象与要拖动的1个主父对象对齐.不用说,您可能需要对其进行调整,以使其能够根据您的需要专门针对您的项目执行,但是希望这可以使您朝着正确的方向发展. (仅供参考,我尚未对此进行测试,因此不能保证它会立即可用)

Keep in mind this will only snap child objects to 1 master parent object that you are dragging around. It should go without saying you may need to tweak this for it to perform specifically to your project like you want, but hopefully this gets you in the right direction. (FYI I have not tested this so am not guaranteeing it will work right out of the gate)

这篇关于如何在运行时统一捕捉两个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆