Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by user5200 · Jul 27, 2012 at 05:27 AM · c#2dbullettrajectory

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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));
}
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image user5200 · Jul 27, 2012 at 10:40 PM 0
Share

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!

avatar image ScroodgeM · Jul 28, 2012 at 04:54 AM 0
Share

i'd edit the answer

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges