Search Unity

About Instantiating missile from enemy from random positions.

Discussion in 'Scripting' started by BrokenhearT, Jul 4, 2015.

  1. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    Here's the script I don't see anything wrong with it, maybe you guys can find out.

    Code (CSharp):
    1.     public GameObject enemyMissile;
    2.     public Transform player;
    3.  
    4.     public int maxpos = 1.5f;
    5.  
    6.     public float missileDelay = 0.5f;
    7.     public float missilecooldownTimer = 0;
    8.  
    9.  
    10.  
    11.     void Start ()
    12.     {
    13.         if (GameObject.Find ("Player"))
    14.             player = GameObject.Find ("Player").transform;
    15.     }
    16.    
    17.     void Update ()
    18.     {
    19.         missilecooldownTimer -= Time.deltaTime;
    20.  
    21.         if (missilecooldownTimer <=0 && player)
    22.  
    23.        
    24.         {
    25.             Vector3 maxpos = new Vector3(Random.Range(1.5f, -1.5f), transform.position.y, transform.position.z);
    26.  
    27.             missilecooldownTimer = missileDelay;
    28.            
    29.             Instantiate(enemyMissile, maxpos, transform.rotation);
    30.        
    31.         }
    32.  
    33.     }
    34.  
    35. }
    thanks in advance.
     
  2. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    What exactly is the problem?
     
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You have your Random.Range values backwards, you have maximum before minimum.

    Change it to this:
    Code (csharp):
    1.  
    2. Random.Range(-1.5f, 1.5f)
    3.  
     
  4. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    The script has no errors and the problem is the missile is lunched only from the center of the enemy, not in random position
     
  5. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69

    Vector3 maxpos = new Vector3(Random.Range(-1.5f, 1.5f), transform.position.y, transform.position.z);

    I did and still didn't change anything.
     
  6. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    I notice you have
    "public int maxpos = 1.5f;"

    Are you showing us you doing this
    "Vector3 maxpos = new Vector3(Random.Range(-1.5f, 1.5f), transform.position.y, transform.position.z);"
    but in reality you are actually doing this?
    "Vector3 someName = new Vector3(Random.Range(-maxpos, maxpos), transform.position.y, transform.position.z);"

    Maybe your int maxpos, although is set to 1.5f in the script, is actually still 0 in the inspector?
    Though, I dont think it would even let you do that since your vector3 is also named maxpos, so if you plugged maxpos, it would think you are putting in a vector3. This may not be the issue, but it might be a possibility that the code you are showing us isnt completely what you have.
     
  7. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    Well I removed the int in max pos, and this is the whole script I attched, and problem still going on.
     
  8. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    When I run this code
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TestRandomRange : MonoBehaviour
    4. {
    5.     public GameObject prefab;
    6.  
    7.     void Update()
    8.     {
    9.         Vector3 maxpos = new Vector3(Random.Range(1.5f, -1.5f), transform.position.y, transform.position.z);
    10.         Instantiate(prefab, maxpos, transform.rotation);
    11.     }
    12. }
    Everything works as expected.

    Make a new scene with just your script and do a test just to make sure you don't have anything causing problems.

    Post your project if you want me to look at it.
     
  9. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Try making the numbers larger. If your scene is big you wont notice a difference.
     
  10. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    I tried it didn't do anything, but I have the spawning script for enemies, and it works fine with the same set up it's really so weird!
     
  11. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    post an example project of the problem so we can look at it ourselves.

    Maybe you didnt put the script on the gameobject and instead still have an old script there. Or maybe some other reasons.