Y
Y
Yugg02019-02-12 23:04:46
C++ / C#
Yugg0, 2019-02-12 23:04:46

Why in Unity do objects "spawn" above another object?

In my project, when I chop a tree, the object that should "spawn" in it (the tree disappears) only it "spawns" much higher than the object!
Here is the tree script

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

public class HPwood : MonoBehaviour
{
    public float hp;
    public GameObject[] Spawn;

    private void Update()
    {
        if(hp < 0)
        {
            for(int i = 0; i < Spawn.Length; i++)
            {
                Vector3 v3 = new Vector3(transform.position.x, transform.position.y + (Spawn[i].transform.localScale.y + i), transform.position.z);
                Instantiate(Spawn[i], v3, Quaternion.identity);
            }
            Destroy(gameObject);
        }
    }
}

5c6325c2525d2933554604.png5c632665a4e94829948220.png
just in case the ax script!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Axe : MonoBehaviour
{
    RaycastHit hit;
    public AnimationClip anim;

    private void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            if(Physics.Raycast(transform.parent.transform.position, transform.parent.transform.transform.forward, out hit, 10))
            {
                if(hit.collider.tag == "Tree")
                {
                    hit.collider.gameObject.GetComponent<HPwood>().hp -= 5;
                }
            }

        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2019-02-13
@YugGO

Spawn[i].transform.localScale.y + i
at least this moment was not in vain allocated to you ..
you should MULTIPLICATE on the scale (scale), at least. not to add to it.
you get it in global world coordinates, take the scale as the distance.
so you get that you spawn objects N + i higher)) in world coordinates.
and then the second question. and why skeil local??? coordinates then global will be used. and then the global one would be used. including all parents. (although for the prefab the same is most likely, but still, to understand what is not entirely true there)
and finally. in advance.
you have a collider on the tree. and I also feel there is a collider on the resources being created))
you have one collider from another will be pushed out .. if you do not turn it off on the tree.
Destroy does not work instantly and will have time to calculate the physics))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question