Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Projectile spawning top right of screen rather than middle

Discussion in 'Scripting' started by charu_, Jan 24, 2020.

  1. charu_

    charu_

    Joined:
    Jan 16, 2020
    Posts:
    9
    So I'm aiming to have a marshmallow shoot out a projectile on click, and currently my target is to make it spawn on click. Though it spawns at the top right which I cannot seem to fix. Any help greatly appreciated!

    The error =




    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpawnProjectile : MonoBehaviour
    6. {
    7.  
    8.     public GameObject firePoint;
    9.     public List<GameObject> vfx = new List<GameObject>();
    10.  
    11.     private GameObject effectToSpawn;
    12.  
    13.     void Start()
    14.     {
    15.         effectToSpawn = vfx[0];
    16.     }
    17.  
    18.  
    19.     void Update()
    20.     {
    21.         if(Input.GetMouseButton(0))
    22.         {
    23.             SpawnVFX();
    24.         }
    25.     }
    26.  
    27.     void SpawnVFX()
    28.     {
    29.         GameObject vfx;
    30.  
    31.         if(firePoint != null)
    32.         {
    33.             vfx = Instantiate(effectToSpawn, firePoint.transform.position, Quaternion.identity);
    34.         }
    35.         else
    36.         {
    37.             Debug.Log("No Fire Point");
    38.         }
    39.     }
    40. }
    41.  
     
  2. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    What is firePoint? Is this the spawn location?
     
  3. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    This should get you started, either transform.position it to vfx or firePoint.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class SpawnProjectile : MonoBehaviour
    5. {
    6.     public GameObject firePoint;
    7.     public List<GameObject> vfx = new List<GameObject>();
    8.     private GameObject effectToSpawn;
    9.     void Start()
    10.     {
    11.         effectToSpawn = vfx[0];
    12.     }
    13.     void Update()
    14.     {
    15.         if(Input.GetMouseButton(0))
    16.         {
    17.             SpawnVFX();
    18.         }
    19.     }
    20.     void SpawnVFX()
    21.     {
    22.         GameObject vfx;
    23.         if(firePoint != null)
    24.         {
    25.             vfx = Instantiate(effectToSpawn, firePoint.transform.position, Quaternion.identity);
    26.             vfx.transform.position = this.transform.position;
    27.         }
    28.         else
    29.         {
    30.             Debug.Log("No Fire Point");
    31.         }
    32.     }
    33. }
     

    Attached Files:

    Last edited: Jan 25, 2020
  4. charu_

    charu_

    Joined:
    Jan 16, 2020
    Posts:
    9
    FirePoint is where the projectile should spawn.
     
  5. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    Where is the pivot on the object that the script is assigned to? (select it and press Z).
     
  6. charu_

    charu_

    Joined:
    Jan 16, 2020
    Posts:
    9

    https://prnt.sc/qsjj6c

    Sorry if I don't understand some stuff, am a beginner.
     
  7. Snipe76

    Snipe76

    Joined:
    May 23, 2017
    Posts:
    12
    Why did you add the code at line 26?
    This will change the position of the VFX to the position of the game object that the scripts sit on.
    I think you can remove that line.
     
  8. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    It changes the position of the the spawn blob in relation to VFX or firePoint., it's not parented to it (see image).

    The centre of firePoint might be off, it seems to be generating top right constantly.