Grenade spawns in the wrong location

Viewed 56

In my game I want to have a floating monster that's attack throws a grenade at the player. My problem is that the grenade only spawns in 0, 0, 0. In my script I make it so that the zombies spawns in on its own location but for some reason that doesn't work. I tried making it spawn by having the spawn location equal new Vector3(100, 100, 100) but it still spawned at 0, 0, 0. I know that the co-routine runs because I put a Debug.Log. Thanks for the help!

Edit #2: I can't have a rigidbody on the script. I have edited the movement script and I have found that no mater what if a rigidbody is added then it will go to 0, 0, 0.

Edit #3: I updated the scripts

Here is my script: (Sorry if the code is bad)

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

public class ZKAttack_lvl3 : MonoBehaviour
{
    public Transform Player;
    public float MoveSpeed = 2.0f;
    public float InRadius = 10.0f;
    public float AttackRange = 15.0f;

    private Coroutine hasCourutineRunYet;

    public GameObject grenade;
    public GameObject FloatingMonster;
    private Vector3 FloatingMon;
    private Animator anim;
    private Rigidbody rigid;

    private void Start()
    {
        anim = GetComponent<Animator>();
        Player = GameObject.FindGameObjectsWithTag("Player")[0].transform;
        rigid = GetComponent<Rigidbody>();
        
    }

    void Update()
    {
        Player = GameObject.FindGameObjectsWithTag("Player")[0].transform;
        transform.LookAt(Player);

        float dstSqr = (Player.position - transform.position).sqrMagnitude;
        bool inRadius = (dstSqr <= InRadius * InRadius);
        bool inAttackRange = (dstSqr <= AttackRange * AttackRange);        
        anim.SetBool("AttackingPlayer", inAttackRange);
        if (inRadius)
        {
            transform.position += transform.forward * MoveSpeed * Time.deltaTime;
        }

        rigid.AddForce(1, 10, 1);

        if (inAttackRange)
        {
            if (hasCourutineRunYet == null)
            {
                hasCourutineRunYet = StartCoroutine(GrenadeAttack());
            }
        }

        
    }

    IEnumerator GrenadeAttack()
    {        
        FloatingMon = FloatingMonster.transform.position;        
        GameObject bulletObject = Instantiate(grenade, FloatingMonster.transform.position, Quaternion.identity);       

        yield return new WaitForSeconds(2.0f);
    }
}

Edit: This is the code for the movement:

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

public class GrenadeMovement : MonoBehaviour
{
    public float speed = 10f;

    public float lifeDuration = 4.0f;

    private float lifeTimer;

    private Coroutine hasCourutineRunYet;
    
    private Transform Player;

    public SphereCollider sphereCollider;
    

    // Use this for initialization
    void Start()
    {
        lifeTimer = lifeDuration;

        sphereCollider.enabled = false;     

        Player = GameObject.FindGameObjectsWithTag("Player")[0].transform;
    }

    // Update is called once per frame
    void Update()
    {   
        lifeTimer -= Time.deltaTime;

        if (lifeTimer >= 3f)
        {
            Player = GameObject.FindGameObjectsWithTag("Player")[0].transform;
            transform.LookAt(Player);

            transform.position += transform.forward * speed * Time.deltaTime;
        }

        if (lifeTimer >= 0f)
        {
            transform.position = speed * transform.up * Time.deltaTime;         
            transform.position = speed * transform.forward * Time.deltaTime;            
        }


        if (lifeTimer <= 0f)
        {
            if (hasCourutineRunYet == null)
            {
                hasCourutineRunYet = StartCoroutine(GrenadeExplosion());
            }

        }
    }

    private void OnTriggerEnter(Collider coll)
    {
        if (coll.gameObject.tag == "Player")
        {
            if (hasCourutineRunYet == null)
            {
                hasCourutineRunYet = StartCoroutine(GrenadeExplosion());
            }
        }


    }

    IEnumerator GrenadeExplosion()
    {
        sphereCollider.enabled = true;

        yield return new WaitForSeconds(1.0f);
        Destroy(gameObject);
    }
}
2 Answers

With the help of @ken I figured out that I couldn't use a rigidbody so I changed the insantiation to this: GameObject bulletObject = Instantiate(grenade, FloatingMonster.transform.position, Quaternion.identity);. I then changed the movement script for it to:

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

public class GrenadeMovement : MonoBehaviour
{
    public float speed = 10f;

    public float lifeDuration = 4.0f;

    private float lifeTimer;

    private Coroutine hasCourutineRunYet;
    
    private Transform Player;

    public SphereCollider sphereCollider;

    public Vector3 velocity;

    // Use this for initialization
    void Start()
    {
        lifeTimer = lifeDuration;

        sphereCollider.enabled = false;     

        Player = GameObject.FindGameObjectsWithTag("Player")[0].transform;
    }

    // Update is called once per frame
    void Update()
    {   
        lifeTimer -= Time.deltaTime;

        if (lifeTimer >= 2f)
        {
            Player = GameObject.FindGameObjectsWithTag("Player")[0].transform;
            transform.LookAt(Player);

            transform.position += transform.forward * 5 * Time.deltaTime;
        }

        if (lifeTimer >= 0f && lifeTimer <= 2f)
        {           
            transform.position = 9.18f * transform.up * Time.deltaTime;         
            transform.position = 9.18f * transform.forward * Time.deltaTime;            
        }


        if (lifeTimer <= 0f)
        {
            if (hasCourutineRunYet == null)
            {
                hasCourutineRunYet = StartCoroutine(GrenadeExplosion());
            }

        }
    }

    private void OnTriggerEnter(Collider coll)
    {
        if (coll.gameObject.tag == "Player" || coll.gameObject.tag == "Terrain")
        {
            if (hasCourutineRunYet == null)
            {
                hasCourutineRunYet = StartCoroutine(GrenadeExplosion());
            }
        }


    }

    IEnumerator GrenadeExplosion()
    {
        sphereCollider.enabled = true;

        yield return new WaitForSeconds(1.0f);
        Destroy(gameObject);
    }
}

Thank you for all your help, I have been trying to fix this all week.

You could set its position in the Instantiate line. Instantiate has several arguments. You can set its position in Instantiate, as well as its rotation and parent.

Set it to this:

    IEnumerator GrenadeAttack()
    {        
        GameObject bulletObject = Instantiate(grenade, FloatingMonster.transform.position, Quaternion.identity);
        yield return new WaitForSeconds(2.0f);
    }
Related