简体   繁体   中英

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

I'm making a game in Unity and it's a 3d project. After I drag an object next to another object and the distance is not too big, I want them to merge. 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.

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. One should use OnTriggerEnter(Collider other) or 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) . Right now, the parent object is at the center of the object that has the script on it. 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:

...
   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. (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.)

You may just want to see if they are a certain distance and then execute the rest of the code. 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. 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()

Vector3.Distance

Interpolation:

What is interpolation

Vector3

Float

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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