Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 HipsterSoap · Jul 16, 2016 at 07:15 PM · c#2d-platformerbulletbullet-prefab

Trying go get bullet to fire correctly when play turns around in 2D platformer

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (BulletCollision))]
 public class Bullet : MonoBehaviour{
 
     public float bulletSpeed = 9;
     float gravity = 0;
     Vector3 velocity;
 
     Controller2D controller;
     BulletCollision bulletController;
 
     int flipNumber = 1;
 
     void Start () {
         bulletController = GetComponent<BulletCollision> ();
         controller = GetComponent<Controller2D> ();
     }
 
     void Update () {
         /*
         if (controller.collisions.faceDir != 1) {
             flipNumber = -1;
         } else {
             flipNumber = 1;
         }*/
 
         velocity.x = bulletSpeed * controller.collisions.faceDir;
         velocity.y += gravity * Time.deltaTime;
         bulletController.Move (velocity * Time.deltaTime);
     }
 }



I need to make the bullet coming from the player to fire in the correct direction. My thought was that I need a way to reference the collisions.faceDir variable form the Collisions2d script without having to put it onto my bullet prefab. If anyone could help me with how to do this or with some other solution that would be great!

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 Zehru · Jul 17, 2016 at 01:27 AM

@HipsterSoap HI! If you use a "rigidbody2d" on the bullet, it might be simpler to achieve the results you want. You can use "Rigidbody2d.Impulse" to make the bullet fly to the correct direction( use a vector2 if the game is 2d, so you can do: right, left, up, down and diagonals easily). To check the direction, you should use "transform.localrotation.z" There are some good tutorials about this on a youtube channel called CasanisPlays( in a 2d game prototyping), I also learned there ^_^ I hope this answer was useful. cheers

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 HipsterSoap · Jul 17, 2016 at 05:44 PM 0
Share

I've thought about that but ive been using raycasting for everything. Im not sure that I could keep all the stuff ive built so far if i made the bullets a rigid body

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Flip over an object (smooth transition) 3 Answers

bullets kill all enemies, not just the ones hit. 1 Answer

animation script for 2D platformer issue 2 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