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 Jens12341234 · Feb 24, 2021 at 09:54 AM · gameobjectplayer

How do I spawn a bullet correctly and add velocity to it?

In my game each player has a shootpoint so the game knows where I want the bullet to be spawned, but I encountered some problems.
Every time when player 1 joins, it spawns a bullet at the spawn location of player 1 and the shootpoint. When player 2 spawns and shoots, the bullet only spawns from the shootpoint. Both player 1 and 2 use the same script. Here is my code:

 public void Shoot(InputAction.CallbackContext context){
         Instantiate(Bullet, shootpoint.position, shootpoint.rotation);
         if(gameObject.transform.rotation.eulerAngles.y == -180){
             Bullet.gameObject.GetComponent<Rigidbody2D>().velocity = Vector2.left * force;
         } else if(gameObject.transform.rotation.eulerAngles.y == 0){
             Bullet.gameObject.GetComponent<Rigidbody2D>().velocity = Vector2.right * force;
         }
     }


As you can see I am using the new inputsystem and I am also trying to make the bullet fly a little, you know like a real bullet... In the beginning I did it with 2 separate scripts but I think it's better if I do it with 1 script.

Does anyone know how to fix that bug and/or make the bullet fly?

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 GeroNL · Feb 24, 2021 at 02:18 PM

Try to change these line :

 Instantiate(Bullet, shootpoint.position, shootpoint.rotation);

to be :

 Instantiate(Bullet, shootpoint.position, shootpoint.rotation, give it the transformation that you want);


Hope it help.

Comment
Add comment · Show 2 · 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 Jens12341234 · Feb 24, 2021 at 05:28 PM 0
Share

I used the following line:
Instantiate(Bullet, shootpoint.position, shootpoint.rotation, BRB.velocity = Vector2.left * force);
(The BRB is the bullet rigidbody) When I do that, it gives me an error that I can't use Unity.Vector2, I need to use Unity.Transform.

is there a transform method that I can use to make the bullet fly?

avatar image GeroNL Jens12341234 · Feb 25, 2021 at 01:30 AM 0
Share

That place is just for tansform, like position and rotation,. try to just change the line, if you want force move, you need to keep it into variable :

 Rigidbody clone;
 clone = Instantiate(.....)

then use the clone to give it velocity or etc.

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

190 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

Related Questions

How to pass a gameobject between scenes 3 Answers

I am getting player tag not the other object that i want to destroy. 1 Answer

rigidbody.velocity not working on x axis 2 Answers

Destruction Help!!!!! 1 Answer

{Easy question} How to disable script from other gameObject. 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