i wanna spawn object with speed from different axis like x -x y and -y
Currently this script is attached to my enemy prefab.
public class meteor : MonoBehaviour
{
public float speed = 10.0f;
private Rigidbody2D rb;
public float rotationSpeed = 100.0f;
public float randomNumber;
// Use this for initialization
void Start()
{
rb = this.GetComponent<Rigidbody2D>();
randomNumber = Random.Range(0, 4);
rb.velocity = new Vector2(0, -speed);
// if (randomNumber == 0)
// rb.velocity = new Vector2(0, speed);
/* if (randomNumber == 2)
rb.velocity = new Vector2(0, -speed);
if (randomNumber == 3)
rb.velocity = new Vector2(speed, 0);
if (randomNumber == 4)
rb.velocity = new Vector2(-speed, 0);*/
}
void Update()
{
transform.Rotate(0, 0, rotationSpeed * Time.deltaTime);
}
};
and this script is attached to my gameobject that instantiate the prefab is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class spawnScript : MonoBehaviour
{
public GameObject asteroidPrefab;
public float respawnTime = 1.0f;
private Vector2 screenBounds;
// Use this for initialization
void Start()
{
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
StartCoroutine(asteroidWave());
}
private void spawnEnemy()
{
GameObject a = Instantiate(asteroidPrefab) as GameObject;
a.transform.position = new Vector2(Random.Range(-screenBounds.x, screenBounds.x), screenBounds.y*1 );
float randomnumber= GetComponent<meteor>().randomNumber;
/* if (randomnumber == 0)
a.transform.position = new Vector2( Random.Range(-screenBounds.x, screenBounds.x), screenBounds.y * -2);
if (randomnumber == 1)
a.transform.position = new Vector2(Random.Range(-screenBounds.x, screenBounds.x), screenBounds.y * 2);
if (randomnumber == 2)
a.transform.position = new Vector2( screenBounds.x * -2,Random.Range(-screenBounds.y, screenBounds.y));
if (randomnumber == 3)
a.transform.position = new Vector2(screenBounds.x * 2, Random.Range(-screenBounds.y, screenBounds.y));*/
}
IEnumerator asteroidWave()
{
while (true)
{
yield return new WaitForSeconds(respawnTime);
spawnEnemy();
}
}
}
Now i wanna get a random number ranging number from 1to 4 and than spawn that object from axis
depending on my if condition. i am not able to access random number using get component. Help me?