• 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 /
  • Help Room /
avatar image
0
Question by inf3rno24 · Jan 23, 2016 at 12:37 PM · unity 52dbullettop down shooterfiring

How to make bullet shoot in same direction as player faces in 2D Top Down Shooter

hi, This is a 2D game. This is the code I have put together for when the player presses the left click button an object should fire towards move in the same direction of the player, I think the code might be right but Here it is:

using UnityEngine; using System.Collections;

public class PlayerShooting : MonoBehaviour {

 public Rigidbody2D bulletPrefab;
 public float attackSpeed = 0.5f;
 public float coolDown;
 public float bulletSpeed = 500;
 public float yValue = 1f; // Used to make it look like it's shot from the gun itself (offset)
 public float xValue = 0.2f; // Same as above


 // Update is called once per frame
 void Update()
 {

     if (Time.time >= coolDown)
     {
         if (Input.GetMouseButton(0))
         {
             Fire();
         }
     }
 }

 void Fire()
 {
     //Rigidbody2D bPrefab = Instantiate(bulletPrefab,transform.position,Quaternion.identity) as Rigidbody2D;

     Rigidbody2D bPrefab = Instantiate(bulletPrefab, new Vector3(transform.position.x + xValue, transform.position.y + yValue, transform.position.z), transform.rotation) as Rigidbody2D;
     bPrefab.GetComponent<Rigidbody2D>().AddForce(transform.up * bulletSpeed);

     coolDown = Time.time + attackSpeed;
 }

}

, I am not sure what to put where in the hierarchy, anyway can someone help me with this please? I have the "Player_Hero" and that is all, i have a image for the sprite but, i have put that under the "Player_Hero" this is called (Fire_1) as it is a spell :) yeh please can someone help me?

Thank_you();

Comment
Add comment · Show 1
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 andrew-lukasik · Jan 23, 2016 at 01:34 PM 0
Share

Hi. You need to provide more info otherwise it's only guesswork. For example:

 bPrefab.GetComponent<Rigidbody2D>().AddForce(transform.up * bulletSpeed);

means that you are applying force along +Y local axis of this PlayerShooting component's transform. Y is often Up so... it good, bad? Try changing it to "transform.right" or "-transform.right"

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to make top down particles splatter in Particle system unity 2d? 0 Answers

No overload for method 'fireBullet' takes 0 arguments 1 Answer

Raw image flickering when texture changed 1 Answer

Stuck inside of Composite Collider when using Platform Effector 2D 2 Answers

Unity 2D Lighting From Universal Render Pipeline Not Working 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