Instantiated Prefab Spawns With Unwanted Offset

I am working on a 3D axe-throwing game, when an axe hits the target, a fractured prefab of the target with multiple children with rigidbody components is spawned and the old model is destroyed. Although when the fractured prefab is spawned it has a noticeable Y offset. From what I have read I don’t know if it is to do with the rigidbodies of the fractured prefab or to do with floating point numbers.

Target Destruction Code:

   void Hit()
    {
       
        targetManager.TargetRemoved();
        score.IncreaseScore(targetManager.defaultScore);
        if(settingsManager.canVibrate)
        {
            Handheld.Vibrate();
        }
       
        Despawn();

    }

    void Despawn()
    {
      
  
       GameObject brokenObject = Instantiate(brokenTarget, transform.position, Quaternion.Euler(0, 90, 0), null);
        Destroy(gameObject);


    }

Full Script:

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

public class Target : MonoBehaviour
{
    bool hasBeenHit;
    TargetManager targetManager;

    [SerializeField]  float offsetAmount;

    [SerializeField] GameObject brokenTarget;

    Score score;

    GameManager gameManager;
    SettingsManager settingsManager;


    [SerializeField] float distanceMoved;
    [SerializeField] bool isMovingTargetHorizontal;
    [SerializeField] bool isMovingTargetVertical;

    [SerializeField] List<AudioClip> hitClips;
    [SerializeField] AudioSource audioSource;

   



    private void Start()
    {
       
        gameManager = GameObject.FindGameObjectWithTag("Game Manager").GetComponent<GameManager>();
        targetManager = GameObject.FindGameObjectWithTag("Target Manager").GetComponent<TargetManager>();
        settingsManager = GameObject.FindGameObjectWithTag("SettingsManager").GetComponent<SettingsManager>();


        distanceMoved = 0;


        int moveNumX = Random.Range(0, 2);
        int moveNumY = Random.Range(0, 2);

        print(moveNumX);
        print(moveNumY);
        if (moveNumX == 1)
        {
            isMovingTargetHorizontal = true;
        }
        if (moveNumY == 1)
        {
            isMovingTargetVertical = true;
        }

        if (isMovingTargetHorizontal)
        {
            MoveTargetHorizontal();
        }
        if (isMovingTargetVertical)
        {
            MoveTargetVertical();
        }
    }


    private void OnCollisionEnter(Collision collision)
    {
        score = GameObject.FindGameObjectWithTag("Score Text").GetComponent<Score>();

        if (collision.transform.tag == "Axe" && !hasBeenHit)
        {
           int randNum = Random.Range(0, hitClips.Count);
            audioSource.clip = hitClips[randNum];
            audioSource.Play();
            Invoke("Hit", audioSource.clip.length);
            hasBeenHit = true;
            gameManager.AddAxes(1);
        }
    }


   void Hit()
    {
       
        targetManager.TargetRemoved();
        score.IncreaseScore(targetManager.defaultScore);
        if(settingsManager.canVibrate)
        {
            Handheld.Vibrate();
        }
       
        Despawn();

    }

    void Despawn()
    {
      
  
       GameObject brokenObject = Instantiate(brokenTarget, transform.position, Quaternion.Euler(0, 90, 0), null);
        Destroy(gameObject);


    }

    void MoveTargetHorizontal()
    {
       
       LeanTween.moveX(gameObject, Random.Range(-2, targetManager.maxDistance), Random.Range(1, targetManager.maxTargetSpeed)).setLoopPingPong();
       

    }
    void MoveTargetVertical()
    {

        LeanTween.moveY(gameObject, Random.Range(-2, targetManager.maxDistance), Random.Range(1, targetManager.maxTargetSpeed)).setLoopPingPong();


    }

}

Any help would be greatly appreciated!

If you open your prefab, you can see what offset your objects will be at. Anything at the origin will be where you spawn your object.

Thanks for the response, I have the broken target prefab sitting at a position of 0,0,0. I have also just tried setting the Y to a negative value to see if that would fix it, but that did not fix it.

Perhaps your geometry is not centered where you expect it.

Open the prefab again and toggle the Center / Pivot and Global / Local buttons to see what’s going on.

If this is the case, here is how to fix:

  • edit the geometry with a 3D program, OR

  • remake the prefab with a blank GameObject to help you offset the geometry back where it belongs.

1 Like

This fixed it! The pivot was a lot lower than the geometry, and moving the children to the pivot fixed the problem. Thank you!

1 Like