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 Slicery · Jun 21, 2014 at 03:30 PM · 2dbulletshootleftright

2d shooting Left / Right

Hi,

Im currently working on a 2d sidescroll shooter and now i have the problem that my Bullets flying to the right direction. My idea is it to tell my "Fire" script to check if the player is currently facing to the left or right and give the bullet speed in this direction. But i dont have an idea to implement this in my script. : /

hope somebody can help me out with this.

Here is the script:

  using UnityEngine;
 using System.Collections;
 
 public class FirePistol : MonoBehaviour {
     
     //Firing variables
     public float fireFrequency = 0.1f;
     public float bulletVelocity = 1;
     float lastShot;
     public bool isFiring = true;
     
     //Holds a link to the object that is to be fired
     public GameObject bulletObject;
     
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         if(Input.GetButton("Fire1"))
         {
             fireGun();
         }
     }
 
 
 
     //Checks the time since the last shot and whether the gun can fire, if it can it will call Fire()
     void fireGun()
     {
         //print ("fired");
         if(Time.time > lastShot + fireFrequency)
         {
             if(isFiring == true)
             {
                 Fire();
             }
         }
     }
     
     //Instantiates a new bullet object and assignes its starting variables
     //Can also handle guided munitions if you want to fire rocket prefabs
     void Fire() {
         
         lastShot = Time.time;
 
 
         //Quarternion.Identity = default rotation
         GameObject newBullet = Instantiate(bulletObject, (transform.position + (transform.up/20)), Quaternion.identity) as GameObject; 
         newBullet.transform.rotation = gameObject.transform.rotation; //Rotate the same direction as the ship it is fired from
         newBullet.rigidbody2D.AddForce(transform.up * bulletVelocity); //Give initial speed, needs to be high for bullets and lower for self guided munitions. 
 
     }
     
     //Toggles the isFiring bool variable. 
     void ToggleFiring()
     {
         //print ("TOGGLE");
         isFiring = !isFiring;
     }
     
 }


on this line: newBullet.rigidbody2D.AddForce(transform.up * bulletVelocity);

i tested what happens if i change / bulletVelocity to -bulletVelocity and the bullet flews to the left so as is sad with simple left right check it should work.

sorry for my english.

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 tebandesade · Jun 24, 2014 at 07:31 PM

 public bool facingright=True;
 
 void FixedUpdate () 
 {
         GetComponent().velocity = new Vector2(move(where the character is moving, try calling the method of the class of the character) * maxSpeed( bullet velocity), GetComponent().velocity.y);
         if(move > 0 && !FacingRight)
         {    
             Flip();
         }
         else if ( move < 0 && FacingRight)
         {
             Flip();
         }
 
 
 void Flip()
     {
         FacingRight = !FacingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }

something like that. Just call the method that moves the character . To do so just reference it at the beginning making the type the name of the script.

Comment
Add comment · Show 1 · 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 meat5000 ♦ · Jun 24, 2014 at 07:31 PM 0
Share

Formatting fail :P

Fixed it for you

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Projectile move towards mouse cursor 3 Answers

Shoot Bullet At Touch Position : 2D 1 Answer

Problem creating a 2D scroller shooting game 2 Answers

How do I make my sprite go left or right when it hits a trigger? 0 Answers

Shoot 10 bullets at once 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