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 Cl0ud · Jul 21, 2013 at 10:54 AM · 2dbulletshootspawn points

Problem creating a 2D scroller shooting game

Hello everyone , I have a problem with my 2D scroller shooting game (like metal slug), basically I'm not able to create a script to shoot in front of the player . I've created one , but the bullet doesn't move in front of the player , and it keep staying where it was spawned (the actual player position). Here's my script :

 var Sparo_pref : Transform;
 
 function Update () 
 {
 if(Input.GetAxis("Spara")){
     var bullet = Instantiate(Sparo_pref, Vector3(transform.position.x,transform.position.y,0), Quaternion.identity);
     bullet.rigidbody.AddForce(bullet.transform.forward * 5);
 }



Can someone help me, please? I have been searching a solution for couple of days , but nothing happens...

Edit : code sample ;)

Comment
Add comment · Show 2
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 clunk47 · Jul 25, 2013 at 01:34 AM 0
Share

Try adding constantForce to the bullet. I'd also define the prefab as a Rigidbody. I'd also spawn 1 unit in front of player, ins$$anonymous$$d of 0 on z axis (your choice). You also need to have the bullet ignore collision with the object it is spawning from, if that object has a collider.

 var Sparo_pref : Transform;
 var force : float = 100f;
  
 function Update () 
 {
     if(Input.Get$$anonymous$$ouseButtonDown(0))
     {
         var bullet = Instantiate(Sparo_pref, transform.position + transform.forward, Quaternion.identity);
         if(!bullet.gameObject.GetComponent(ConstantForce))
         {
             bullet.gameObject.AddComponent(ConstantForce);
             bullet.constantForce.force = bullet.transform.forward * force;
         }
         else
             bullet.constantForce.force = bullet.transform.forward * force;
         if(bullet != null)
             Physics.IgnoreCollision(this.collider, bullet.collider, true);
     }
 }

avatar image clunk47 · Aug 09, 2013 at 05:20 PM 0
Share

Please either accept an appropriate answer, let us know it has or has not been resolved, or close your question!

2 Replies

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

Answer by AlucardJay · Jul 21, 2013 at 02:55 PM

The problem is you want to add an instantaneous force, where AddForce is applied over time in physics. For an instantaneous force in your case of shooting a bullet and applying force only once, you need to use ForceMode.Impulse :

 rigidbody.AddForce( transform.up * jumpForce, ForceMode.Impulse );

or using your script :

 bullet.rigidbody.AddForce( bullet.transform.forward * 5, ForceMode.Impulse );

Links :

  • http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html

  • http://docs.unity3d.com/Documentation/ScriptReference/ForceMode.Impulse.html

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 Cl0ud · Jul 21, 2013 at 09:41 PM 0
Share

I've already tried that ... It doesn't work :[

avatar image AlucardJay · Jul 22, 2013 at 10:28 AM 0
Share

Have you tried increasing the force? 5 is a very small force! Also check you arn't instantiating where it is hitting the player, and the player is absorbing all the force

avatar image
0

Answer by Dimling · Jul 21, 2013 at 11:30 AM

First of all it would be nice if you formated your code by using the "Code Sample" button so its easier for us to read.

First of all what I do when I create a 2d shooter game is to create a sphere in front of the player, right before the gun, that represent the spawn point for the bullet. Lets just call it "FireSpawnPoint". This one must of course not hit the collider of the player. After that I make a prefab that represent the bullet. For intsance just a spehere for this as well. Lets just call this one "BulletPrefab". After this its time to write some code.

 public void Fire(){
      GameObject clone = Instantiate(BulletPrefab, GameObject.Find("FireSpawnPoint").transform.position, Quaternion.identity) as GameObject;
      clone.rigidbody.AddForce(Vector3.right * 5000); 
 }

This script spawns a bullet in front of the player, at the spawn point, and then fires its away.

Comment
Add comment · Show 3 · 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 Cl0ud · Jul 21, 2013 at 02:41 PM 0
Share

Have I just to copy you code , replacing $$anonymous$$e? If yes , it doesn't work :(

avatar image Dimling Cl0ud · Jul 21, 2013 at 02:46 PM 0
Share

Well, you have rewrite the code so it matches your object names!

avatar image Cl0ud · Jul 21, 2013 at 03:10 PM 0
Share

I've done that , nothing to do :/

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

18 People are following this question.

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

Related Questions

Shoot Bullet At Touch Position : 2D 1 Answer

Need a script for a gun that shoots bullet using raycast 2 Answers

Bullet not moving 1 Answer

2d shooting Left / Right 1 Answer

Troubles With A Shoot Script 1 Answer


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