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 ChilledSloth · May 12, 2020 at 07:12 AM · scripting problemshootingprefab-instance2d rotation

2D character not shooting in correct direction

hello, I'm making a simple mobile platformer. My character is flipping directions correctly but the spawned bullets don't travel in flipped direction and instead always go on the right side. Code for flipping my character:

     void flip()
         {
             facR = !facR;
             transform.Rotate(0f, 180f, 0f);
         }
 

Shoot script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class shoot : MonoBehaviour
 {
 
     public GameObject bullet;
     public Transform point;
   
     
 
    public void shootB()
     {
         
         Instantiate(bullet, point.position, point.rotation);
     }
 }


bullet instantiate:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class bulletscrp : MonoBehaviour
 {
     public Rigidbody2D rb;
     public float speed;
     // Start is called before the first frame update
     void Start()
     {
         rb = GetComponent<Rigidbody2D>();
         rb.velocity = new Vector2(speed, rb.velocity.y);
     }
 
    
 }
 

Inspector window and hierarchy: alt text

alt text

Pls help!

scr-ins.png (32.0 kB)
scr-main.png (46.3 kB)
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
1
Best Answer

Answer by Morphosys · May 12, 2020 at 10:26 AM

Hi, the problem you describe is pretty easy to fix, one of the ways to do that is the following one :

In your bullet prefab, replace your bullet script with one that contains :

 using UnityEngine;
 
 public class Bullet : MonoBehaviour
 {
 
     public float speed = 10f;
     public Rigidbody2D rbody;
 
     void Start()
     {
         rbody.velocity = speed * transform.right;
 
     }
 
 }

Here you are multiplying the speed (that is adjustable in your inspector) with the transform.right, allowing your character to shoot in the "right" axis with both positive and negative values.

(Edit 12 May 2020 at 12:30) : Was your shootpoint rotating too ? If so everything should work, if not I have a solution for that.

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 ChilledSloth · May 12, 2020 at 05:15 PM 0
Share

Wow! It fixed it. But why transform.forward didn't work tho. But thanks a lot

avatar image ADiSiN ChilledSloth · May 12, 2020 at 05:51 PM 1
Share

Hi!

Just for clarification - transform.forward is direction that pointing in z direction of the object. In 3D it will be front direction, but in 2D it's going to be direction into the screen, so transform.forward wouldn't work here, but you also didn't implement it.

Look at your bullet code:

  void Start()
  {
      rb = GetComponent<Rigidbody2D>();
      rb.velocity = new Vector2(speed, rb.velocity.y);
  }

The thing is that you always applied to the bullet positive speed value along X axis what means no matter what your object rotation is it will go towards same direction, because you didn't multiply your speed variable as xKarter implemented it in the answer that worked for you.


I hope that makes more clear why the answer fixed it for you and why it did not work previosly x)

avatar image ChilledSloth ADiSiN · May 13, 2020 at 03:52 AM 0
Share

Oh! Thanks for the explanation. It really helped me :)

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.

Follow this Question

Answers Answers and Comments

231 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Bullet only firing in 1 direction. 1 Answer

[Shooting shots Scripting] How to make the compiler know that I mean the empty quad by the Public Transform? 0 Answers

How would I add force/ burst fire to my gun? 2 Answers

How to get bullets to hit crosshair 2 Answers

Name of the prefab instantiated has clone suffixed 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