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 Naiakoa · Sep 13, 2014 at 09:31 PM · c#2d2d platformer

Cannot make bullet shoot right

Nomatter what I have tried for some reason the pullet refuses to shoot to the right or to the left. It is starting to get really annoying. All it keeps doing is shoot upwards and nothing more....

 public GUITexture AtkText;

 public Transform projectile;
 public int speed = 20;
 public bool cooldown = false;

     foreach (Touch touch in Input.touches)
     {
         if(AtkText.HitTest(touch.position) && touch.phase != TouchPhase.Ended && !cooldown)
         {
             Transform clone;
             var rot = projectile.rotation;
             projectile.rotation = rot * Quaternion.Euler(0, 0, 90);


             clone = (Transform)Instantiate(projectile, transform.position, projectile.rotation);
             cooldown = true;
             projectile.rigidbody2D.AddForce(projectile.transform.right * speed);
             clone.rigidbody2D.AddForce(clone.transform.right * speed);
         }
         else if(AtkText.HitTest(touch.position) && touch.phase == TouchPhase.Ended && cooldown)
         {
             cooldown = false;
         }
     }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by AngryBurritoCoder · Sep 13, 2014 at 09:41 PM

if you are trying to make it shoot where you touch, why not find x and z coords of the touch point in world space, and instantiate the projectile, make it projectile.transform.lookAt(new Vector(x,0,y)) and add force to it in the forward direction.

Comment
Add comment · Show 4 · 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 Naiakoa · Sep 13, 2014 at 09:51 PM 0
Share

Its not suppose to shoot at where you touch. The touch detections uses a GUI texture as a button to trigger the scripts so that the player shoots the bullet. Only directions it suppose to shoot throughout the whole thing is either Left or Right but it doesn't do either. Only shoots up.

avatar image AngryBurritoCoder · Sep 13, 2014 at 10:49 PM 0
Share

well then you must instantiate the bullet in its normal rotation so that its forward direction is um forward. ( i dont understand why you are multiplying it by 90 degrees on the z axis ?

then the rotation of the bullet changes either to point left or right ( depending on button pressed) by rotating 90 degrees around the y axis or -90 degrees. ( also depends on how your bullet is modelled , this is only correct if the bullets tip is pointing in forward direction, and forward direction is the z axis if you were wondering).

then after you rotate it, add force to it in the forward direction, because you rotated it to face right of left.

avatar image AngryBurritoCoder · Sep 13, 2014 at 10:53 PM 0
Share

wait are you working in 2d as in side view 2d? or top down 2d.

avatar image Naiakoa · Sep 13, 2014 at 11:55 PM 0
Share

2d side view like a platformer kinda like $$anonymous$$ario. And the bullet size at the moment is just a sphere for a temp texture till I get all mechanics working properly then the real development begins making the game run smoother and looking better. Shooting up is not a problem but I tend to not use it that way, so only left and right is more valuable.

avatar image
0

Answer by Naiakoa · Sep 22, 2014 at 06:37 AM

OK apparently I was really stupid when it came to scripting this =.= but for future reference and for anyone else here is the fix:

             cooldown = true;
             if(facingRight)
             {
                 Rigidbody2D bulletInstance = Instantiate(projectile, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
                 bulletInstance.velocity = new Vector2(speed, 0);
             }
             else
             {
                 Rigidbody2D bulletInstance = Instantiate(projectile, transform.position, Quaternion.Euler(new Vector3(0,0,180f))) as Rigidbody2D;
                 bulletInstance.velocity = new Vector2(-speed, 0);
             }
Comment
Add comment · 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

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

2D - Creating a basic teleport system 0 Answers

In my 2D platformer game, how would I create a height marker?, 1 Answer

Dialouge System [SOLVED] 1 Answer

Checking for an object at a certain position 2 Answers

2D Image not appearing in game but only in scene 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