- Home /
2D bullet spray problems
Hello all,
I am making a 2D action game in which one of the enemies is an annoying ninja that throws ninja stars at you. I have figured out how to make him throw 1 at you and it works fine. However, I want him to have an attack variation where he throws 3 stars at you. This effect should be similar to the spreadgun effect in old games like contra.
Example here: http://gamerlimit.com/wp-content/uploads/2009/03/contraspread-391x306-custom.gif
I have taken a look at this topic: http://answers.unity3d.com/questions/199736/2D-Bullet-Hell-Game--Bullet-Patterns.html and determined that I would not be able to use this method in my game.
The main difference is that my projectiles are not being programmed to go in a straight line. I still want the middle shuriken to target my player's location, while the other 2 are simply offset away from the middle one by a certain amount. Here is the code I have currently:
This is in my ninja's script and it is how the projectiles are instantiated:
void shootone()
{
Instantiate(shuriken, new Vector3(transform.position.x, transform.position.y, transform.position.z), transform.rotation);
}
void shootmult()
{
Instantiate(shuriken, new Vector3(transform.position.x, transform.position.y, transform.position.z), transform.rotation);
Instantiate(shuleft, new Vector3(transform.position.x, transform.position.y, transform.position.z), transform.rotation);
Instantiate(shuright, new Vector3(transform.position.x, transform.position.y, transform.position.z), transform.rotation);
}
From when it is created, the projectile receives instructions for its behavior. Below is the code for the main shuriken (the one that targets that player):
void Start () {
Player = GameObject.FindGameObjectWithTag("Player");
rigidbody.velocity = Getdiff() * speed;
}
public Vector3 Getdiff()
{
Vector3 diff = Player.transform.position - transform.position;
return diff;
}
As I said earlier, I have no problems with the above code, it works as I intended. I have included it because my attempt to make the 2 offset shurikens are based on it. Here is my attempt:
I have duplicated the original shuriken object and made shuright and shuleft. As you can see above, the Shootmult() function instantiates 3 different shurikens instead of 3 clones of the same one. This is because I gave the other 2 a different GetDiff() function. Here it is:
public Vector3 Getdiffleft()
{
Vector3 diff = new Vector3(Player.transform.position.x, Player.transform.position.y - 500, Player.transform.position.z)
- transform.position;
return diff;
}
public Vector3 Getdiffright()
{
Vector3 diff = new Vector3(Player.transform.position.x, Player.transform.position.y + 500, Player.transform.position.z)
- transform.position;
return diff;
}
So, what I have tried to do is offset the target destination so that the left one will target slightly below the player's y position while the right one will target slightly above. Obviously, this did not work, but I do not understand why. The regular Shuriken works by homing in on the player's position on startup. Why can't this location be offset by a certain amount?
Some have suggested that I take the regular approach of making the projectile simply fly straight with different origin points. If i take this approach, the ninja's location will have to be less dynamic. I am ready to do that if I cannot find a solution that lets me keep the homing aspect. Can anyone help me achieve this effect?
EDIT: As I'm running close to my deadline, I am just going to use shurikens that go straight for my spreadshot and homing for the single shot. Thanks everyone for the suggestions, if anyone comes up with a way to do what I originally asked please feel free to post as it may help others in the future.
Thanks for reading!
Answer by ScroodgeM · Jul 27, 2012 at 11:41 AM
replace shuriken's method with this:
void Start() { Player = GameObject.FindGameObjectWithTag("Player"); //i'd remove distance-from-player-dependent speed, normally speed should not depends on distance to target. correct me if i'm wrong //this line makes speed forward independent of target position //i'd also remove homing-missile effect (i'm sure it's correct cause in your source two paricles except central one will never hit player) rigidbody.velocity = transform.forward * speed; }
replace shurikens starter's method with this:
void shootmult() { Instantiate(shuriken, transform.position, Quaternion.LookRotation(target.position - transform.position, Vector3.up) * Quaternion.Euler(0f, 0f, -10f)); Instantiate(shuriken, transform.position, Quaternion.LookRotation(target.position - transform.position, Vector3.up) * Quaternion.Euler(0f, 0f, 0f)); Instantiate(shuriken, transform.position, Quaternion.LookRotation(target.position - transform.position, Vector3.up) * Quaternion.Euler(0f, 0f, 10f)); }
Thanks for the input, Scroodge$$anonymous$$. However, as I stated, my game is a 2D game and this enemy unit can appear anywhere on the screen. This is why I need the ho$$anonymous$$g function so that it will always target the player. Furthermore, the Getdiff() function only calls once at startup so it only "homes" in on the player's position during that frame. The player is free to move after that, which is why it is false to say that the other 2 shurikens would never hit him. If the player moves slighly upwards or downwards he should encounter the side shurikens.
If I am unable to make it work with the ho$$anonymous$$g function, I will probably consider making the ninja's location more static so that he can just throw in a specific direction like you've described. But if anyone can think of a way I can keep the ho$$anonymous$$g and also have spread shot effect, I would greatly appreciate it!
Your answer
Follow this Question
Related Questions
How to make 3 bullets fire at different angles 1 Answer
Distribute terrain in zones 3 Answers
Bullet fires before direction change 0 Answers
2D bullet trajectory prediction (line renderer) 0 Answers
Creating a spread of bullets 2 Answers