简体   繁体   English

旋转的 object 的 x 轴和 z 轴倒转为单位 3d

[英]Rotated object has x and z axis inversed in unity 3d

I'm making a game in Unity and it's a 3d project.我正在 Unity 中制作游戏,它是一个 3d 项目。 After I drag an object next to another object and the distance is not too big, I want them to merge.在我将一个 object 拖到另一个 object 旁边并且距离不太大之后,我希望它们合并。 If I put the object on the right side of the other I want it to merge on the right side and inverse if I put it on the left side.如果我将 object 放在另一个的右侧,我希望它在右侧合并,如果我放在左侧则反转。

If the objects are rotated, Z axis position will change with the X axis position and I won't know what position should I add to the dragged object so they will be merged together. If the objects are rotated, Z axis position will change with the X axis position and I won't know what position should I add to the dragged object so they will be merged together.

Can someone help me with a code for this?有人可以帮我写一个代码吗?

I have an idea of what you want.我知道你想要什么。 I think you mean like two balls hitting each other, and you want them to move together.我认为您的意思是两个球互相撞击,并且您希望它们一起移动。 My first idea is that you could set the object that hit it and the object that got hit to children of an empty game object that you could move around.我的第一个想法是,您可以设置击中它的 object 和击中它的 object 的孩子可以移动的空游戏 object。 One should use OnTriggerEnter(Collider other) or OnCollisionEnter(Collision other) .应该使用OnTriggerEnter(Collider other)OnCollisionEnter(Collision other) I will use one in the example below, and I will show you how to change them later.我将在下面的示例中使用一个,稍后我将向您展示如何更改它们。

using UnityEngine;

//Important: only attach script to one of the objects you want to group.
public class exampleClass : MonoBehaviour
{
   public GameObject empty;
   void OnTriggerEnter(Collider other)
   {
      if (other.gameObject.tag == “exampleTag”)
    //change tag to the tag of the object(s) you want to join.
      {
         Join(other.gameObject);
      }
   }
   void Join(GameObject obj)
   {
      var parent = Instantiate(empty, transform.position);
      transform.parent = parent.transform;
      obj.transform.parent = parent.transform;
   }
}

Here is a basic script that joins the objects into a group where the parent (object which controls the other two objects) can move/rotate both at the same time.这是一个基本脚本,它将对象加入一个组,其中父对象(控制其他两个对象的对象)可以同时移动/旋转两者。 If you want to make them use OnCollisionEnter() , (link at the bottom of answer for when to use trigger or collision) then you change OnTriggerEnter(Collider other) to OnCollisionEnter(Collision other) .如果您想让他们使用OnCollisionEnter() (答案底部的链接了解何时使用触发器或碰撞),那么您将OnTriggerEnter(Collider other)更改为OnCollisionEnter(Collision other) Right now, the parent object is at the center of the object that has the script on it.现在,父 object 位于上面有脚本的 object 的中心。 We can change that to the middle of both colliding objects by using interpolation.我们可以使用插值将其更改为两个碰撞对象的中间。 Interpolation gets a point between two vectors or floats based on a value from 0 - 1. Change part of the Join(GameObject obj) method, to this:插值基于 0 - 1 的值在两个向量或浮点数之间获取一个点。将Join(GameObject obj)方法的一部分更改为:

...
   void Join(GameObject obj)
   {
      Vector3 middle = Vector3.Lerp(transform.position, obj.transform.position, 0.5f);
      var parent = Instantiate(empty, middle);
      transform.parent = parent.transform;
      obj.transform.parent = parent.transform;
   }

It finds the middle of the the object's position and the hit object's position.它找到对象的 position 和命中对象的 position 的中间。 (You can change 0.5f to 0.1f to be one tenth of the way to the object's position to the hit object's position.) (您可以将 0.5f 更改为 0.1f 为对象的 position 到被击对象的 position 的十分之一。)

You may just want to see if they are a certain distance and then execute the rest of the code.您可能只是想看看它们是否有一定的距离,然后执行代码的 rest。 If you do, then you would want to change the following code:如果这样做,那么您将需要更改以下代码:


using UnityEngine;

//Important: only attach script to one of the objects you want to group.
public class exampleClass : MonoBehaviour
{
   public float maxDist = 1f;
   public GameObject empty;
   void Update()
   {
      GameObject[] objects = FindGameObjectsWithTag(“exampleTag”);
    //change tag to the tag of the object(s) you want to join.
      for (int i = 0; i < objects.Length; i++)
      {
         float distance = Vector3.Distance(objects[i].transform.position, transform.position);
         if (distance <= maxDist)
         {
            Join(other.gameObject);
         }
      }
   }
...

This code finds the distance with Vector3.Distance() between any object marked with a certain tag and the object with the script.此代码使用Vector3.Distance()查找任何带有某个标签的 object 与带有脚本的 object 之间的距离。 If that distance is less than or equal to the max distance to join, then it joins the objects.如果该距离小于或等于要加入的最大距离,则它会加入对象。 If you get any errors or this is not as you intended, leave a comment on this post.如果您遇到任何错误或这与您的预期不符,请在此帖子上发表评论。

Some links for extra help:一些额外帮助的链接:

Instantiate 实例化

OnCollisionEnter() or OnTriggerEnter() OnCollisionEnter()OnTriggerEnter()

Vector3.Distance 矢量3.距离

Interpolation:插值:

What is interpolation 什么是插值

Vector3 矢量3

Float 浮动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM