Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 rickymanalo · Aug 18, 2021 at 02:32 PM · rigidbodyaddforceangularvelocity

RigidBody AddForce not pushing rb to forward when adding angularVelocity

I need to rotate a RigidBody from a script that is pushed by an AddForce(, ForceMode.VelocityChange) from another script. So far, angularVelocity is making the RigidBody rotate, but somehow the AddForce and angularVelocity doesn't mix as I hoped it would. What happens is that the RigidBody rotates in place and is still following it's initial trajectory.

My code for AddForce is;

 rb.AddForce(transform.forward * projectileSpd * Time.deltaTime, ForceMode.VelocityChange);

And this is my code that uses angularVelocity;

  void SeekTarget(){
     rb = gameObject.GetComponent<Rigidbody>();
     Vector3 turnDir = closestTarget.transform.position - rb.position;
     turnDir.Normalize();
 
     float rotateAmt = Vector3.Cross(turnDir, transform.forward).y;
     rb.angularVelocity = rb.transform.up * -rotateAmt * turnSpd * Time.deltaTime;
 }

Both of them are called in FixedUpdate

UPDATE:

I'm adding I bit more detail so you guys can visualize my dilemma. So, here is how I made my projectile.

 Projectile prefab
 --Projectile handler script
   |--rb.AddForce
 --Seek handler script
   |--rb.angularVelocity

My function for firing the projectile

 void FireProjectile(GameObject projectile){
     Rigidbody rb = projectile.AddComponent<Rigidbody>();
     rb.useGravity = false;
 
     ProjectileHandler projectileHandler = projectile.GetComponent<ProjectileHandler>();
     projectileHandler.mSpeed = projectileSpd;
     
     rb.AddRelativeForce(Vector3.forward * projectileSpd * Time.deltaTime, ForceMode.VelocityChange);
     projectile.transform.parent = null;
 }

So, I think, being that the rb of the both scripts are the same, AddForce and angularVelocity should mix. Unfortunately, it doesn't. Btw, I get the projectile from a pool before I fire it, which I parent to the "gun".

I remembered something. I don't have a RigidBody initially on the prefab itself. Because the projectile GameObject doesn't follow it's "gun" parent if it initially do IIRC. So what I did is, if I'm about to fire the projectile, I'll do a 'AddComponent()' then AddForce on that rb. But still, seek handler script would get the same rb, right?

This is the video of how my game is currently working. Sorry for it being quite late. The white cylinder is what the projectile should point towards to. https://drive.google.com/file/d/1O1J6BJ7pdDe5Cx_azIBqKIF4FEGJBLyH/view?usp=sharing

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 Acdia · Aug 18, 2021 at 04:02 PM

If you call rb.AddForce() from another script, transform.forward is forward for the object the script is called from (assuming it is on another GameObject). You would need to use the Transform of the projectile or use rb.AddRelativeForce(Vector3.forward * projectileSpd * Time.deltaTime, ForceMode.VelocityChange)

Comment
Add comment · Show 3 · 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 rickymanalo · Aug 19, 2021 at 05:53 AM 0
Share

The GameObjectthat calls rb.AddForce() is indeed a different GameObjectfrom the projectile. I used a different script as abstraction to apply attributes to GameObjects because I might be able to use them for other stuff. I'll try this later

UPDATE: It still doesn't work, whether I use AddForce(projectile.transform.forward) or AddRelativeForce(Vector3.forward). Do I need to change something with my rotate rb code?

avatar image Acdia rickymanalo · Aug 19, 2021 at 09:34 PM 0
Share

Hmm... I admit, I didn't expect that still not to work. The issue sounds like having the world, instead of the local, coordinate system. But with the GameObject being part of the Projectile (or AddRelativeForce) I would expect it to be the correct one. Currently the only things I can think of, are having the wrong rb or calling AddForce only once, which both seem unlikely due to your description. Other than that it would be interesting to know what happens, if you initially rotate the projectile from sceneview and what the velocity and angular velocity are actually doing on runtime.

avatar image rickymanalo Acdia · Aug 20, 2021 at 01:42 PM 0
Share

Hey, I remembered something relating to my rb. Check my update

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

172 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

Related Questions

Stopping a rigidbody immediately 2 Answers

AddForce not influenced by rotation 1 Answer

Add force to Rigidbody towards clicked direction using Raycast 0 Answers

How do I add force To rigidbody based On Character's Walking direction? 0 Answers

Add force to a rigid body with raycast 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