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 impurekind · Aug 20, 2018 at 07:43 PM · velocityspeedforcemotion

How do I give my ejected bullet shell a speed plus the player's current speed?

If my player is running around in the game and I eject a shell from his gun when I fire, how do I make sure the speed the shell travels is in addition to the speed the player is already moving?

Basically, I want it to always look like the shell is flying out and off to the right of the gun regardless of what direction the player is running around in or how fast they are moving, as it looks wrong just now with the shell looking like it's flying more to the left if I move right and more to the right if I move left, so I want to set the shell's speed on top of whatever speed the player is already moving in whatever direction.

You know, like if I throw something while stationary vs in a vehicle moving at 60mph, the object will travel at its normal speed plus my current speed so it will actually be moving faster if I throw it from the moving car.

Comment
Add comment · Show 4
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 Piyush_Pandey · Aug 21, 2018 at 10:07 AM 0
Share

You can use the reference of the rigibody of the parent. It will give you the current velocity of the player and then you can add it to the shell.

avatar image impurekind Piyush_Pandey · Aug 21, 2018 at 05:01 PM 0
Share

Which is done how?

avatar image JVene · Aug 21, 2018 at 06:18 PM 0
Share

Shell's and bullets appear to be slightly confused in your post, though I'm sure not in your $$anonymous$$d. That is to say I think you're asking about two separate object behaviors.


For a shell ejection a lot depends on motion change and the relative measurement you're observing. If the weapon is moving at a constant rate throughout the duration of the ejection, the relative trajectory of the shell should be consistent relative to the weapon, but will not remain consistent relative to the world (which can be difficult to correctly 'see' at speed). When the weapon changes direction or speed during or just after ejection, there won't be a consistent trajectory relative to the weapon.


If the shell and/or bullet are child objects of the weapon, they're moving with the weapon. If they are 'unparented' at ejection (where firing force was added to the child before unparenting), the local system should do what you're asking about until it is uncoupled. That is a rather common solution.

avatar image impurekind JVene · Aug 21, 2018 at 07:06 PM 0
Share

No, it's all just the shell casings ejecting from the gun that I'm on about. $$anonymous$$y bad if I've not been properly clear. The shell casings aren't children of the gun; they're just their own prefab that I instantiate and "eject" (via applying force) out the side of the gun each time the player fires.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JVene · Aug 21, 2018 at 08:37 PM

Get the Rigidbody component of the gun. Instantiate the casing, let's say it's 'c'. Get 'c's Rigidbody, then copy the gun's velocity with something like:

  c_rb.velocity = gun_rb.velocity;

Where c_rb is the casing's Rigidbody, and gun_rb is the gun's Rigidbody. Then apply the ejection force.

Comment
Add comment · Show 7 · 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 impurekind · Aug 23, 2018 at 01:45 PM 0
Share

Well, I tried the following and it didn't work (the part at the bottom with "shellsRigidbody.velocity = gunsRigidbody.velocity;" says there's an error with both of those parts):

This is what I have inside my Update function:

 GameObject gun = GameObject.Find("GunNewPC");
 
             if (gun != null)
             {
                 Rigidbody gunsRigidbody;
                 gunsRigidbody = GetComponent<Rigidbody>();
             }
 
             GameObject shell = GameObject.Find("BulletShell");
 
             if (shell != null)
             {
                 Rigidbody shellsRigidbody;
                 shellsRigidbody = GetComponent<Rigidbody>();
             }
 
             Instantiate(bulletShell, transform.position, transform.rotation); // This is set to just off to the side of the gun
 
             shellsRigidbody.velocity = gunsRigidbody.velocity;


What did I do wrong?

Note: The object this script is attached to is a child of my gun object, which is in turn a child of my player object, just in case that's relevant.

avatar image JVene impurekind · Aug 23, 2018 at 06:25 PM 0
Share

This is a simple little issue. The gunsRigidbody and the shellsRigidbody are created inside the brackets of an 'if' statement, which means they completely evaporate outside the brackets of those 'if' statements. The are considered local (and only local) to the block of code between those brackets. The technical phrase is that they only have scope inside the brackets.


$$anonymous$$ove the declaration of Rigidbody gunsRigidbody and Rigidbody shellsRigidbody outside (and before you use them) the brackets of the if statments. This does not apply to the call to GetComponent, those are not affected or part of the problem.


Note, this means that one or the other could be null, so consider testing.

avatar image impurekind JVene · Aug 23, 2018 at 11:31 PM 0
Share

Note, it's not working. Here's what I have so far:

 void Update()
     {
         if (OVRInput.GetDown(OVRInput.Button.SecondaryIndexTrigger) || Input.Get$$anonymous$$ouseButtonDown(0)) // Secondary means Right Hand for Oculus Touch
         {
             audioSource.Play();
             RaycastGun();

             Rigidbody gunsRigidbody;
             Rigidbody shellsRigidbody;
 
             Instantiate(bulletShell, transform.position, transform.rotation); // This is set to just off to the side of the gun
 
             GameObject gun = GameObject.Find("GunNewVR");
             gunsRigidbody = gun.GetComponent<Rigidbody>();
 
             GameObject shell = GameObject.Find("BulletShell");
             shellsRigidbody = shell.GetComponent<Rigidbody>();
 
             shellsRigidbody.velocity = gunsRigidbody.velocity;
 
             Instantiate(gunFlash, shotOrigin.position, transform.rotation); // The barrel of the gun
         }
     }

Clearly I'm not understanding something here (and this is my trying to interpret what you're saying and put it in there correctly)--maybe there's something you think is obvious but isn't to me--so if you could just show me how it's supposed to be written properly, it really would be appreciated.

I'm not learning a single thing from all my mistakes; I'm just wasting time making a lot of mistakes.

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

93 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

Related Questions

Using rigidbody.AddForce and speeding the object 2 Answers

How to get collision force, not relative velocity? 1 Answer

i am not able to increase the speed of ball perodically in pong, in this code the speed of ball decrease while playing so help with this, how to increase speed of ball perodically?, 0 Answers

Make an object move forward on its own 2 Answers

How to control the speed of the projectile motion? 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