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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

scale enemy bullet based on scale of enemy

Discussion in 'Scripting' started by tedthebug, Aug 11, 2015.

  1. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Hi, I'm making a 2d game with:
    Enemy prefab
    Enemy bullet prefab

    I want the bullets to be instantiated at a scale in proportion to the scale of the enemy that fired it & am having trouble passing the scale of the enemy across to the bullet.

    The code on the bullet that relates to it is:
    Code (CSharp):
    1.  
    2.     public float speed;
    3.     float desiredScaleRate = 1.018f;
    4.  
    5.     // scale variables
    6.     public E_Rotate_Ship shipScale;
    7.     public GameObject Self1;
    8.  
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.  
    13.         shipScale = Self1.gameObject.GetComponent<E_Rotate_Ship> ();
    14.         //set size of bullet
    15.  
    16.  
    17.     transform.localScale = new Vector3 (transform.localScale.x / shipScale.enemyScale.x, transform.localScale.y * shipScale.enemyScale.y, 1);
    18.         // destroy the bullet after 2 seconds
    19.         Destroy (gameObject, 2f);
    20.  
    21.     }

    The enemy is changing it's size each update & the code on the enemy tracking its scale for use by the bullet is:
    Code (CSharp):
    1. // Set variable for use by bullet
    2.     public Vector3 enemyScale;
    3.  
    4. // Update is called once per frame
    5.     void Update () {
    6. // set value of variable for use by bullet script
    7.         enemyScale = transform.localScale;
    8. }

    In the bullet prefab inspector I have used the enemy prefab as Self1 & referenced the script on the enemy that is updating its scale & triggering the bullet.

    The problem I'm having is that in debug the enemy is updating the enemyScale correctly but on the bullet prefab it is using the scale of (0,0,0). Any suggestions on how I can get the correct scale to be passed across?

    Thanks
     
  2. Lorinthio

    Lorinthio

    Joined:
    Aug 7, 2015
    Posts:
    39
    Couple of things here!

    In line 13, shipScale = Self1.gameObject, is redundant specifically at "Self1.gameObject", you already have Self1 as a gameObject, so you're saying gameObject.gameObject

    I'm not sure how you have your scaling working, but this line seems a little strange.

    Code (CSharp):
    1. new Vector3(transform.localScale.x/ shipScale.enemyScale.x, transform.localScale.y* shipScale.enemyScale.y, 1);
    In terms of keeping the scale in respect of the enemy ship, wouldn't you want something such as...
    Code (CSharp):
    1. new Vector3(enemyScale.x * desiredScaleRate, enemyScale.y * desiredScaleRate, enemyScale.z * desiredScaleRate)

    In terms of performance, for the enemyScale function in the second snippet. You have enemyScale = transform.localScale. If you are planning on changing the scale of the enemy all the time this is appropriate. However I would recommend moving that to the Start() function. So you're not wasting that processing for each enemy.
     
  3. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Thanks for responding. Yes I was scaling that enemy every turn.

    I eventually worked out that the bullet must've been taking the scale from the prefab itself & not the enemy(clone) that fired the bullet. I got it to work by casting the instantiate to an GameObject obj, setting that obj scale to the scale of the enemy, then scaling the bullet during its start function.
     
  4. Lorinthio

    Lorinthio

    Joined:
    Aug 7, 2015
    Posts:
    39
    Good to hear you got it working!