using eulerangles to create a spread attack

I’m struggling with this atm. I’m trying to figure out how I can use transform.eulerangles using JS to create a spread attack, like a wave of shots.

19134-u_shot.jpg

this is what I mean. what would be the easiest way to do this? I have a script that spawns a projectile when you push fire (space) and when you have a certain power up, the shot should become this wave effect but I’m not sure how to do it. I saw it done in a tutorial I watched a while ago but I can’t find it now…

Here is what I’ve tried but it doesn’t work how I want it, it spawns the projectiles, all 5 of them (when power up is at level 5) butttt it spawns them all in the same spot soooo its kind of useless.

When you push space bar:

            shots = PowLvl;
			if (Time.time >= nextShot){
			if(wep1 == true && PowLvl == 1){
				for(var x : int = 0; x <= shots; x++)
				{
					Instantiate(wave, Vector3(transform.position.x,transform.position.y + 1,2), Quaternion.identity);
				}
				nextShot = Time.time + manager.fireRate - manager.fRate;
			}
			if(wep1 == true && PowLvl == 2){
				for(var y : int = 0; y <= shots; y++)
				{
					Instantiate(wave, Vector3(transform.position.x,transform.position.y + 1,2), Quaternion.identity);
				}
				nextShot = Time.time + manager.fireRate  - manager.fRate;
			}
			if(wep1 == true && PowLvl == 3){
				for(var z : int = 0; z <= shots; z++)
				{
					Instantiate(wave, Vector3(transform.position.x,transform.position.y + 1,2), Quaternion.identity);
				}
				nextShot = Time.time + manager.fireRate  - manager.fRate;
			}
			if(wep1 == true && PowLvl == 4){
				for(var v : int = 0; v <= shots; v++)
				{
					Instantiate(wave, Vector3(transform.position.x,transform.position.y + 1,2), Quaternion.identity);
				}
				nextShot = Time.time + manager.fireRate  - manager.fRate;
			}
			if(wep1 == true && PowLvl == 5){
				for(var w : int = 0; w <= shots; w++)
				{
					Instantiate(wave, Vector3(transform.position.x,transform.position.y + 1,2), Quaternion.identity);
				}
				nextShot = Time.time + manager.fireRate  - manager.fRate;
			}

and the script attached to the instantiated shot, wave:

function Update () {
	transform.Translate(Vector3(0,speed*Time.deltaTime,0));
	if (player == null){
		player = GameObject.FindGameObjectWithTag("Player").GetComponent(Player_Script);
	}
	
	if(player.wep1 == true){
	
		if (player.PowLvl == 2){
		Quaternion.Euler(0,0,30);
			dmg = 5;
			speed = 6;
		}
		if (player.PowLvl == 3){
		Quaternion.Euler(0,0,30);
			speed = 7;
			
		}
		if (player.PowLvl == 4){
		Quaternion.Euler(0,0,30);
			speed = 8;
		}
		if (player.PowLvl == 5){
		Quaternion.Euler(0,0,30);
			speed = 9;
		}
	}

Any tips on any of this mess of code would be appreciated, I’m trying to improve my abysmal scripting skills.

2 Answers

2

I would do something like this (player script):

  public var halfAng: float = 15; // angle between shots / 2
  private var speed: float;
  private var nextShot: float;

  function Update(){
    if (Input.GetKeyDown("space")){
      if (Time.time >= nextShot && wep1){
        nextShot = Time.time + manager.fireRate - manager.fRate;
        switch (PowLvl){
          case 1:
            speed = 5;
            Shoot(0); // one shot straight ahead
            break;
          case 2:
            speed = 6;
            Shoot(-halfAng); // left shot at -halfAng
            Shoot(halfAng); // right shot at +halfAng
            break;
          case 3:
            speed = 7;
            Shoot(-2*halfAng); //left shot at -2*halfAng
            Shoot(0); // center shot straight ahead
            Shoot(2*halfAng); // right shot at 2*halfAng
            break;
          case 4:
            speed = 8;
            Shoot(-3*halfAng); // leftmost shot at -3*halfAng
            Shoot(-halfAng); // left shot at -halfAng
            Shoot(halfAng); // right shot at +halfAng
            Shoot(3*halfAng); // rightmost shot at +3*halfAng
            break;
          case 5:
            speed = 9;
            Shoot(-4*halfAng); // leftmost shot at -4*halfAng
            Shoot(-2*halfAng); // left shot at -2*halfAng
            Shoot(0); // center shot
            Shoot(2*halfAng); // right shot at +2*halfAng
            Shoot(4*halfAng); // rightmost shot at +4*halfAng
            break;
        }
      }
    }
  }

  function Shoot(angle: float){
    var localPos = Vector3(0,1,2); // relative position
    var rot = Quaternion.Euler(0,0,angle); // calculate rotation
    // rotate relative position and convert to world coordinates:
    var pos = transform.TransformPoint(rot * localPos);
    var shot = Instantiate(wave, pos, rot); // create the shot
    shot.GetComponent(ShotScript).speed = speed; // set its speed
  }

The shot script (save as ShotScript.js) is just this:

  public var speed: float;

  function Update(){
    transform.Translate(0, speed * Time.deltaTime, 0);
  }

NOTE: This code assumes that Z is the up direction and Y is forward.

Wow, I like that script! That is exactly what I was looking for. I really need to learn how to do some of these things, switch/case would clean up so many of my scripts in this project...

The lines that say

Quaternion.Euler (0,0,30);

don’t do anything.

All that is doing is defining a rotation. It is not assigning that rotation to any variable or a property.
What you probably meant to do is:

transform.Rotate (0, 0, 30).

Another solution might be to create a “BulletBurst” prefab with all the bullets already positioned and rotated, and fire them all off on their respective local forward directions at once.

Yeah, that line was originally transform.eulerAngle.z = 30; but that didn't work, so I was trying new stuff. Lemme try that rotate and check it out. The prefab idea sounds nice but not sure if I want to go that route EDIT: Woah, yea rotate definitely isn't what I want, then the projectile just spins in place once you fire it. haha!