There are 2 objects made in a blender and imported into Unity.
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:
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:







