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 /
  • Help Room /
avatar image
0
Question by HongCruise · Dec 14, 2020 at 07:17 PM · projectiles

shooting projectile without inheriting parent's momentum

Hi all, not sure if anyone has ever experienced this issue but I'm trying to shoot a projectile out of a moving "parent" object. Both objects move off of rigidbody physics so I'm using addforce for the projectile. Whenever I move the "parent" object and turn while it's moving then shoot the projectile, I instantiate the projectile, copy the projectile spawn position to the project's position, copy the rotation of the "parent" to the projectile then add force to the projectile. It'll shoot but I notice it moves in the direction of the momentum of the "parent" rather than straight forward from where it was created. I'm pretty sure this is just me not understanding how rigidbody physics works for adding force but if anyone has any ideas suggestions as to how to make the projectile move straight forward from where it's created rather than copying the momentum of the parent I'd appreciate it greatly. My code for creating a projectile looks like this:

//create bullet GameObject bullet = Instantiate(bulletPrefab);

//set location where bullet will spawn from bullet.transform.position = bulletSpawn.position;

//set direction of the bullet bullet.transform.rotation= bulletSpawn.transform.rotation;

//add force to the bullet bullet.GetComponent().AddForce(bulletSpawn.forward * bulletSpeed, ForceMode.Impulse);

//destroy bullet after x time Destroy(bullet, lifeTime);

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by HongCruise · Dec 18, 2020 at 05:25 PM

Sorry forgot to finalize my discovery or at least what I hope is the answer: GameObject bullet = GameObject.Instantiate(bulletPrefab, bulletSpawn.transform.position, bulletSpawn.transform.rotation);

bullet.GetComponent().velocity = (bullet.transform.forward * bulletSpeed) + this.GetComponent().velocity;

Destroy(bullet, bulletLifeTime);

I hope this helps others if they ever run into this issue.

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 xxmariofer · Dec 15, 2020 at 04:22 PM

Hello, adding force simply adds force in a direction so if it has a previous force it will not get override, you should remove the previous acceleration before adding the force

 //create bullet GameObject bullet = Instantiate(bulletPrefab);
 
 //set location where bullet will spawn from bullet.transform.position = bulletSpawn.position;
 
 //set direction of the bullet bullet.transform.rotation= bulletSpawn.transform.rotation;
 
 //remove forces
 bullet.GetComponent<RigidBody>().velocity = Vector3.zero;
 //add force to the bullet 
 bullet.GetComponent<RigidBody>().AddForce(bulletSpawn.forward * bulletSpeed, ForceMode.Impulse);
 
 //destroy bullet after x time 
 Destroy(bullet, lifeTime);
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 HongCruise · Dec 16, 2020 at 12:30 AM 0
Share

Thanks for helping xxmariofer! I tried your code and unfortunately it's producing the same results but I was sure what you said was the case. From what I can tell applying the rotate of the bulletspawn for whatever reason is definitely carrying over the velocity strangely enough. It's even weirder that most tutorials online use this but I guess not many deal with no gravity travel at a higher speed while turning so they wouldn't notice this issue. After an exhausting research I think I somehow found a solution that does work: GameObject bullet = GameObject.Instantiate(bulletPrefab, bulletSpawn.transform.position, bulletSpawn.transform.rotation); bullet.GetComponent().velocity = (bullet.transform.forward * bulletSpeed) + this.GetComponent().velocity; Destroy(bullet, bulletLifeTime);

if you ask me why this works I can't explain it as I'm still trying to grasp how unity's rigidbody works

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

211 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

Related Questions

Bullet goes sideways when the player gets hit 1 Answer

projectile travels parallel to ground and not local space 1 Answer

How to spawn projectiles on the server from a client. 0 Answers

enemy projectiles 0 Answers

2.5D Projectile Help 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