0
\$\begingroup\$

There are 2 objects made in a blender and imported into Unity.

Eye of the Cyclops.blend

Flask.blend:

The problem is that after using the code, which should create an object in the hand-body area (I'm still working on it), one object is created far from the character.

Example:

Everything is OK. введите сюда описание изображения

It's not ok. введите сюда описание изображения

Code:
PlayerInteraction:

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

public class PlayerInteraction : MonoBehaviour
{

    public GameObject target = null;

    public KeyCode interactKey;

    public GameObject itemHolder;

    private void Update()
    {
        if (Input.GetKeyDown(interactKey))
        {
            if (target == null)
            {
                return;
            }

            FoodBox food = target.GetComponent<FoodBox>();
            if (food != null && itemHolder == null)
            {
                food.Interact(this);
            }

            TableBox table = target.GetComponent<TableBox>();
            if (table != null)
            {
                table.Interact(itemHolder, this);
            }

        }
    }

    public void SetItem(GameObject c)
    {
        if (c != null)
        {
            Debug.Log("Взяли предмет!");
            itemHolder = Instantiate(c, transform.position + new Vector3(0f, 2.15f, -0.65f), Quaternion.Euler(-90f, 0f, 180f), transform);
        }
        else
        {
            Destroy(itemHolder);
        }
    }

    private void OnTriggerEnter(Collider col)
    {
        if (target != col.gameObject && target != null)
        {
            return;
        }
        else {
            target = col.gameObject;
            Debug.Log("Мы дошли сюда до " + target.name);
        }

    }

    private void OnTriggerExit(Collider col)
    {
        if (col.gameObject == target)
        {
            target = null;
        }
    }
}

TableBox:

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

public class TableBox : MonoBehaviour
{

    public GameObject itemHolder;

    private void Start()
    {
        if (itemHolder != null)
        {
            itemHolder = Instantiate(itemHolder, transform.position + new Vector3(0f, 1.9f, 0f), Quaternion.Euler(-90f, 45f, 0f),transform);
            Debug.Log("Мы создались!!");
        }
    }

    public void Interact(GameObject i, PlayerInteraction player)
    {
        if ((i == null || itemHolder == null))
        {
            player.SetItem(this.itemHolder);
            Destroy(this.itemHolder);
            //Debug.Log(itemHolder);
            this.itemHolder = i;

            if (this.itemHolder != null)
            {
                this.itemHolder = Instantiate(this.itemHolder, transform.position + new Vector3(0f, 1.9f, 0f), Quaternion.Euler(-90f, 0f, -135f),transform);
                Debug.Log("Мы ингридиенты из ТейблБокса!");
            }
        }
        else
        {
            return;
        }
    }
}

FoodBox:

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

public class FoodBox : MonoBehaviour
{

    public GameObject ingredient;
    private Animator anim;

    // Use this for initialization
    void Start()
    {
        anim = GetComponent<Animator>();
        anim.SetBool("openFoodBox",false);
    }
    public void Interact(PlayerInteraction player)
    {
        anim.SetBool("openFoodBox", true);

        if (anim.GetBool("openFoodBox")) {
            anim.Play("Opening");
            player.SetItem(ingredient);
            Debug.Log("Мы ингридиенты из ФудБокса!");
        }

        anim.SetBool("openFoodBox",false);
    }
}

There is also an assumption that this is due to the initial position of the models. Because for me, some models are stored normally vertically, and some lie horizontally.

введите сюда описание изображения

I also add the prefab inspector:

Eye of the Cyclops:
введите сюда описание изображения

Flask:
введите сюда описание изображения

\$\endgroup\$
5
\$\begingroup\$

As usual, the answer to "Why is Unity doing this bizarre thing" is "Unity is doing an entirely normal thing with bizarre data you gave it"

In this case, the origin of your flasks in Blender sits far outside the object itself, where the blue & red arrows cross in the image below:

Origin of flasks object sitting way above the object

This is the point that becomes the local (0, 0, 0) point when you import the mesh into Unity.

To make the object sit where you wanted it, you had to add a substantial vertical offset to your object position (+ new Vector3(0f, 1.9f, 0f) in your code), to place this origin high above your character, so that the visible mesh way below it would line up where you wanted it.

But when you spawn an object with the origin in a more expected position - the cyclops eye's origin is at its center - the result is the visible mesh sitting higher than you want it.

So, the best solution is to model your objects with the origin in a standardized place relative to the visible mesh (say, around its center of mass or at its base). See how to modify the origin of an object in Blender here.

If for some reason correcting the source mesh is not an option, you can also wrap it in an empty parent game object, and move the child mesh until it sits where you want relative to the parent's origin. Then, spawn and manipulate the parent, rather than the mesh directly.

\$\endgroup\$
  • \$\begingroup\$ Best solution is working) \$\endgroup\$ – Artik Slayer Jul 6 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.