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 ragefordragons · May 11, 2018 at 12:28 AM · rigidbodyinstantiategravityphysics2dbullet

My 2d spawned bullet prefabs will not fly up, just fall

So this ones weird, my bullets are spawning but when they do they just fall down. IT looks like it has to do with gravity and the rigidbodies, but here is my code anyway:

 void Update () {

     handleShooting();
     handleMove();
     handlePause();
   
 }


 void handlePause()
 {
     if (Input.GetKeyDown(KeyCode.Escape))
     {
         cursorLock = !cursorLock;
     }

     if (cursorLock == true)
     {
         Cursor.visible = false;
         Time.timeScale = 1;
     } else
     {
         Cursor.visible = true;
         Time.timeScale = 0;
     }

 }

 void handleMove()
 {
     if (cursorLock == true)
     {
         transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f));

     }
 }

 void handleShooting()
 {
     
     if (canShoot == true)
     {

         shoot();
         StartCoroutine(fire());
     }
     
 }


 void shoot()
 {
     var bulletBullet = (GameObject)Instantiate(
      bullet,
      gun.position,
      gun.rotation);

     bullet.GetComponent<Rigidbody2D>().velocity = bullet.transform.forward * shootVelocity;
     

     Destroy(bulletBullet, 1.2f);

 }

any ideas?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by tormentoarmagedoom · May 11, 2018 at 07:54 AM

Good day.

Can you post a screenshot of the inspector?

Check the Rigidbody compoenent. Unmark the "Use gravity" and mark "is Kinematic".

Bye

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 Harinezumi · May 11, 2018 at 09:13 AM 1
Share

Why kinematic?

avatar image ragefordragons · May 15, 2018 at 01:57 PM 0
Share

Okay, so this is whats weird, my rigidbody 2d doesnt have th option to not use gravity.alt text

capture.png (152.0 kB)
avatar image Harinezumi ragefordragons · May 15, 2018 at 02:02 PM 0
Share

It seems the Editor is different for the Rigidbody2D, ins$$anonymous$$d of bool useGravity it has float gravityScale (weird decision to have different interface, in my opinion). Anyway, just set gravityScale to 0 to disable gravity.

avatar image ragefordragons Harinezumi · May 15, 2018 at 10:34 PM 0
Share

I turned gravity off, but now it doesn't move at all. How can I get it to shoott up if theres no gravity?

avatar image
1

Answer by Harinezumi · May 16, 2018 at 07:28 AM

Is the code still the same as what you posted originally? In that case, the problem is that you instantiate a bullet from the prefab ( bulletBullet), but then assign the velocity to the prefab bullet (I wonder how no one has noticed this so far):

 void shoot () {
     var bulletBullet = (GameObject)Instantiate(bullet, gun.position, gun.rotation);
     bullet.GetComponent<Rigidbody2D>().velocity = bullet.transform.forward * shootVelocity; // <-- this will not affect your bullet!
 }

There might be other problems, but this is the first step to make your bullet fly!
Also, you could store your bullet prefab by its Rigidbody2D, and then instantiate as such, so that you don't need to get the component. Like this:

 [SerializeField] private Rigidbody2D bullet = null; // variable for prefab to be assigned in Editor
 ...
 void shoot () {
     Rigidbody2D bulletInstance = Instantiate(bullet, gun.position, gun.rotation);
     bulletInstance.velocity = bulletInstance.transform.forward * shootVelocity;
 }
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
avatar image
0

Answer by Kciwsolb · May 11, 2018 at 01:29 PM

Since you are working in 2D, you probably don't want to use transform.forward. That is the Z axis (blue arrow). In 2D, this axis points directly away from the camera. So it probably isn't doing what you want. And it may look like it's falling because it has gravity checked? Can't be sure without an inspector screenshot. But anyways, try using either transform.up or transform.right (depending on which way your bullet sprite faces) instead, and uncheck gravity if it is checked.

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 ragefordragons · May 15, 2018 at 10:37 PM 0
Share

I assume this would work, but I cant test it because I'm being told the only way to stop the falling is to make the gravity zero, but then it cant move at all. Heres the editor if that helps:

alt text

capture.png (152.0 kB)

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

131 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

Related Questions

Bullet always facing the sky. 3 Answers

Applying force to a rigidbody 2 Answers

Bullet hell - how to make a lot of bullets? 3 Answers

Horizontal gravity on One gameobject 2 Answers

Shoot an object and have it move based on rotation 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