prefab not spawning in right spot

Hi I want my prefab projectile to spawn at player2 position but it spawns a little ways away to the right. if facing my objects. I tried
projectile.transform.position = new Vector3(spawnX, spawnY, spawnz)

and

projectile.transform.position = Player2.transform.position

and

projectile.tansform.position = this.transform.position.rotation

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

public class APIJumpWirhGrav : MonoBehaviour
{
    public bool attacking = false;
    public int playerHeight = 21;
    public bool onTopOfPlayer = false;
    private GameObject Player;
    public Animator anim;
    public Transform target;
    public UnityEngine.AI.NavMeshAgent agent;
    public GameObject playerRefrefrince;
    private bool inJumpZone = false;
    private Vector3 jumpHeight = Vector3.up * 4;
    private GameObject Player2;
    private CharacterController control;
    private SphereCollider InJumpZone;
    public GameObject tarrget;
    private CharacterController controller;
    private Rigidbody rb;
    public Transform objectToFollow;
    private float speed = 15f;
    public GameObject TarrgetPos;
    public float gravity = 20.0f;
    public float targetTime = 100f;
    BoxCollider collider2;
    GameObject prefab;
    public Vector3 forceMag = new Vector3(0, -900f, 50f);




    void Start()
    {

        rb = GetComponent<Rigidbody>();
        Player = GameObject.FindWithTag("Player");
        Player2 = GameObject.FindWithTag("Player2");

        prefab = Resources.Load("projectile") as GameObject;
       

        collider2 = GetComponent<BoxCollider>();


    }



    void Update()
    {







        if (attacking == true)
        {



            targetTime -= 10 * Time.deltaTime;
            if (targetTime <= 0.0f)
            {
                timerEnded();
                agent.isStopped = false;

            }
        }




        if (Input.GetButtonDown("Fire1") && onTopOfPlayer == true)
        {

            float spawnX = transform.position.x;
            float spawnY = transform.position.y;
            float spawnz = transform.position.z;

           

            GameObject projectile = Instantiate(prefab) as GameObject;

            projectile.transform.position = new Vector3(spawnX, spawnY, spawnz);



            agent.isStopped = true;///////////////
                                   //     gameObject.transform.parent = gameObject.transform.parent = null;

            inJumpZone = false;
            attacking = true;
        //    GetComponent<BoxCollider>().enabled = true;
            anim.SetBool("Jumping", true);
            anim.SetBool("IsMoving", false);
            Destroy(Player2);

        }






        if (onTopOfPlayer == true && attacking == false)
        {

            agent.isStopped = true;///////////
          //  GetComponent<BoxCollider>().enabled = true;
            transform.position = Vector3.Lerp(transform.position, target.transform.position, 1f);
            transform.rotation = Player.transform.rotation;
            anim.SetBool("Jumping", false);
            anim.SetBool("IsMoving", false);


        }

        if (inJumpZone && attacking == false)
        {
         //   GetComponent<BoxCollider>().enabled = true;
            transform.position = Vector3.Lerp(transform.position, target.transform.position + jumpHeight, .50f);
            transform.rotation = Player.transform.rotation;

            anim.SetBool("Jumping", true);
            anim.SetBool("IsMoving", false);
            onTopOfPlayer = true;

        }
        else
       if (inJumpZone == false && attacking == false)
        {

            {

                agent.SetDestination(target.position);
                anim.SetBool("IsMoving", true);
                anim.SetBool("Jumping", false);


            }
        }

    }

    void OnTriggerEnter(Collider other)


    {

        if (other.tag == "Player" && attacking == false)
        {
            inJumpZone = true;
            anim.SetBool("IsMoving", false);
            anim.SetBool("Jumping", true);

        }

    }

    void timerEnded()
    {

        attacking = false;
        onTopOfPlayer = false;
        targetTime = 100f;

    }
    void DestroyGameObject()
    {



        Destroy(gameObject);
    }

}

I must have some kind if registration point off how would I go about checking that?

Are you sure that the projectile prefab’s model is correctly centered on 0,0,0? If it were off center it would appear in game that the projectile was being placed on the wrong location.

Is the projectile a child of another gameobject, that would be messing witht the position, if it is a child of the player, set at (0,0,0) and it will be in the center of it, another posibility is that the transform on the prefab isnt (0,0,0) and is being moved according to that

I think Joe-Censored is right. Try dragging the prefab into an empty scene. Click its top-level container and see if the gizmo appears in the center of the projectile. The gizmo’s position will indicate the projectile’s origin point.

1 Like

How would you go about fixing this? i’m new to unity and my model is centered at 0,0,0 in blender, how can i change the center of the model in unity?

You can change the Origin in Blender OR create an Empty GameObject, set the object you want to change as a child of the empty GameObject and you can change the origin off that.