Make projectile move in certain direction depending on position for base object (C#)
Hello once again my fellow Unites! I'm developing a 2D mobile survival game and I have a little problem, I've created an exploding enemy, that is supposed to shoot out 4 projectiles in a certain direction depending on which position that then enemy has chosen.
I have managed to instantiate the 4 projectiles once the exploding enemy has "exploded", but here's where the problem begins, the projectiles just flys off into a random direction, how can I fix this? I would like to set the direction for the projectiles, depending on which position the "exploding enemy" went to. So that the projectiles does not just fly off in a random.range direction.
Here's what it looks like now (The "big ball" is the exploding enemy and it can go to certain position on the screen, the "small balls" are the projectiles):
Here's an image for how I would like it to be:
and here's the code for the "exploding enemy": (Note that the Transform moveToPoints is where the exploding enemy is supposed to go"
using UnityEngine;
using System.Collections;
public class EnemyExplodeScript : MonoBehaviour {
public Transform[] moveToPoints;
public Transform explodingEnemyTransform;
public GameObject instantiatedEnemy1;
public GameObject instantiatedEnemy2;
public GameObject instantiatedEnemy3;
public GameObject instantiatedEnemy4;
int movePoint = 0;
public float speed;
public float explodeTimer = 3.0f;
public Rigidbody2D enemyMove;
public ParticleSystem preExplosion;
public ParticleSystem explosion;
void Start ()
{
//Find the which points the enemy should follow in the array
moveToPoints[0] = GameObject.Find("MoveRightUp").transform;
moveToPoints[1] = GameObject.Find("MoveLeftDown").transform;
moveToPoints[2] = GameObject.Find("MoveRightDown").transform;
moveToPoints[3] = GameObject.Find("MoveRightUp").transform;
moveToPoints[4] = GameObject.Find("MoveMiddle").transform;
//Set where the enemy should go with the array
movePoint = Random.Range(0, moveToPoints.Length);
}
void Update ()
{
// Speed and time
float step = speed * Time.deltaTime;
//Get the enemy moving
transform.Translate (Vector3.MoveTowards (transform.position, moveToPoints[movePoint].position, step) - transform.position);
}
void ExplosionSpawnEnemy()
{
// Get the transform ahead of time for readability
//Transform explodingEnemyTransform = GameObject.Find("Enemy_explode").transform;
// Instantiate the four projectiles
GameObject explodingEnemyBall1 = Instantiate(instantiatedEnemy1, explodingEnemyTransform.position, explodingEnemyTransform.rotation) as GameObject;
GameObject explodingEnemyBall2 = Instantiate(instantiatedEnemy1, explodingEnemyTransform.position, explodingEnemyTransform.rotation) as GameObject;
GameObject explodingEnemyBall3 = Instantiate(instantiatedEnemy1, explodingEnemyTransform.position, explodingEnemyTransform.rotation) as GameObject;
GameObject explodingEnemyBall4 = Instantiate(instantiatedEnemy1, explodingEnemyTransform.position, explodingEnemyTransform.rotation) as GameObject;
}
and last but not least here's the code for the instantiated projectiles, note that they right now just go off in a random range direction. I want to set the direction that they fly off to depending on the position on which the exploding enemy is in:
using UnityEngine;
using System.Collections;
public class EnemyExplodeBallScript : MonoBehaviour
{
Rigidbody2D bouncing;
public float speed = 30;
public float timer = 3.0f;
Vector3 velocity;
void Start()
{
//transform.position = Vector3.zero;
transform.rotation = Quaternion.identity;
**// Random range where the ball is supposed to fly
float angle = Random.Range (0, 180);**
**// Calculate velocity for ball
velocity = new Vector2 (Mathf.Cos (angle), Mathf.Sin(angle));**
// Get the ball moving!
this.GetComponent<Rigidbody2D> ().velocity = velocity * speed;
}
You guys are just great, I'm grateful for any help that I can get! Thank you!
Answer by Brocccoli · Apr 04, 2016 at 08:48 PM
Wouldn't you want 4 different angles for each ball?
0, 90, 180, 270? This would be for a cross, so I suppose your angles would be 45, 135, 225, 315.
I would encapsulate the idea of the "explosion with 4 balls" into one Component Script, so that on death of the gameObject, the balls are instantiated and fly in the proper directions
Not sure if this is a big help or not.