• 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 _Newbz · Jun 30, 2015 at 01:08 AM · c#topdown

2d top down shooting system help ?

I have a script to fire bullets in a 2d top down space

  using UnityEngine;
 using System.Collections;
 
 public class Shoot : MonoBehaviour {
     
     public GameObject BulletClone;
     
     // Use this for initialization
     void Start () {
         
     }
     // Update is called once per frame
     void Update () {
     if (Input.GetMouseButtonDown(0)) {
         
     }
             Plane zeroPlane = new Plane( Vector3.up, Vector3.zero );
             Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
             float distance;
             
             if( zeroPlane.Raycast( ray, out distance ) )
             {
                 Vector3 outputPosition = ray.origin + ray.direction * distance;
                 Debug.Log( "Position: " + outputPosition  );
                 
                 //You can use this position to rotate the bullet like so
                 BulletClone.transform.LookAt( outputPosition );
             }
         }
     }

but the bullet does not appere

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
0

Answer by Dlonyar · Jun 30, 2015 at 07:37 AM

Is BulletClone the prefab of the bullet? If so, you did not instantiate it.

Comment
Add comment · Show 10 · 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 _Newbz · Jul 01, 2015 at 10:28 PM 0
Share

How could i do that ?

avatar image Dlonyar · Jul 02, 2015 at 07:01 AM 0
Share

http://docs.unity3d.com/ScriptReference/Object.Instantiate.html

avatar image _Newbz · Jul 02, 2015 at 08:04 PM 0
Share

Now the bullets appear, but they just spawn a lot of bullets without me pressing and the clones just fall down : using UnityEngine; using System.Collections;

 public class Shoot : $$anonymous$$onoBehaviour {
     
     public GameObject BulletClone;
     
     // Use this for initialization
     void Start() {
         for (int i = 0; i < 10; i++) {
             Instantiate (BulletClone, new Vector3 (i * 2.0F, 0, 0), Quaternion.identity);
         }
     }
     // Update is called once per frame
     void Update () {
     if (Input.Get$$anonymous$$ouseButtonDown(0)) {
         
     }
             Plane zeroPlane = new Plane( Vector3.up, Vector3.zero );
             Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
             float distance;
             
             if( zeroPlane.Raycast( ray, out distance ) )
             {
                 Vector3 outputPosition = ray.origin + ray.direction * distance;
                 Debug.Log( "Position: " + outputPosition  );
                 
                 //You can use this position to rotate the bullet like so
                 BulletClone.transform.LookAt( outputPosition );
             }
         }
     }

avatar image Dlonyar · Jul 03, 2015 at 06:12 AM 0
Share

The bullets are falling probably because they have a RigidBody component attached to them with the "Use Gravity" option checked.

Instantiate() creates a clone of the GameObject passed in at the specified position and rotation. So call it whenever you want it to spawn and not at the start(eg. line 27-ish).

Unless you have an Object Pooling system, just call Instantiate() there.

Also, if BulletClone is a prefab, you should not be changing any of its values.

Finally, you need to add a velocity to the bullet so it moves in the direction you want.

avatar image _Newbz · Jul 06, 2015 at 09:42 PM 0
Share

they don't fall down , but still i have all the other problems . I don't know how to add velocity, and can you post the correct code. This is $$anonymous$$e using UnityEngine; using System.Collections;

 public class Shoot : $$anonymous$$onoBehaviour {
     
     public GameObject BulletClone;
     
     // Use this for initialization
     void Start() {
         for (int i = 0; i < 10; i++) {
             Instantiate (BulletClone, new Vector3 (i * 2.0F, 0, 0), Quaternion.identity);
         }
     }
     // Update is called once per frame
     void Update () {
     if (Input.Get$$anonymous$$ouseButtonDown(0)) {
         
     }
             Plane zeroPlane = new Plane( Vector3.up, Vector3.zero );
             Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
             float distance;
             
             if( zeroPlane.Raycast( ray, out distance ) )
             {
                 Vector3 outputPosition = ray.origin + ray.direction * distance;
                 Debug.Log( "Position: " + outputPosition  );
             for (int i = 0; i < 10; i++) {
                 Instantiate (BulletClone, new Vector3 (i * 2.0F, 0, 0), Quaternion.identity);
             }
                 
                 BulletClone.transform.LookAt( outputPosition );
             }
         }
     }

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

2D camera zoom smoothing and limitations? 1 Answer

C# LookAt Mouse 2D 2 Answers

I need some help with camera rotations 0 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