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 Inan-Evin · Oct 12, 2011 at 10:57 AM · addforceforceshell

AddForce at Local ??

Hello everyone, i'm trying to create emtpy shells after i fire, and adding them some force to spread, but i have a problem. My code is :

 var newshell =Instantiate(shell,shellspreader.transform.position,Quaternion.identity);
 newshell.transform.Rotate(new Vector3(0,0,Random.Range(-180,180)));
 newshell.transform.Rotate(new Vector3(0,-90,0));
 newshell.rigidbody.AddForce(1000,0,0);

everything is OK,almost, there is one thing. When i fire, the shell goes to the right at 1000 force as i wanted, when i turn right in 90 degrees and fire,shells go forward at 1000, when i turn right in 180 degrees(which is my back) , the shells go left at 1000 force, when i turn right in 270 degrees, or left in 90 degrees the same, shells go back, it's like they always want to go to the same direction. Here is the photo explanation about my problem :

alt text

any ideas how to fix it ?

Thanks :)

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
1
Best Answer

Answer by aldonaletto · Oct 12, 2011 at 11:26 AM

You should use the weapon transform as a reference to apply the force:

   ...
   newshell.transform.Rotate(new Vector3(0,-90,0));
   newshell.rigidbody.AddForce(transform.right * 1000);
   ...

This will work if this script is attached to the weapon (or to the player, or to any object aligned to the weapon), since transform.right is used to direct the force.

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 Inan-Evin · Oct 12, 2011 at 11:36 AM 0
Share

thanks, it works perfectly :).

avatar image
1

Answer by Ecnalyr · Oct 12, 2011 at 11:19 AM

Change the line to: change AddForce (uses global space) to AddRelativeForce (uses local space).

I think this should work. . .


var newshell =Instantiate(shell,shellspreader.transform.position,Quaternion.identity);
newshell.transform.Rotate(new Vector3(0,0,Random.Range(-180,180)));
newshell.transform.Rotate(new Vector3(0,-90,0));
newshell.rigidbody.AddRelativeForce(1000,0,0);

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 Inan-Evin · Oct 12, 2011 at 11:28 AM 0
Share

didn't work, now shells always go to back, it just changed the position they always go, but you are right, i think it's about that global or local space issue, thanks but still need help :).

avatar image Ecnalyr · Oct 12, 2011 at 12:53 PM 0
Share

Need to change the direction of the applied AddRelativeForce.

(1000, 0, 0) apparently is sending them backwards because that is the direction you are telling them to go.

See: http://unity3d.com/support/documentation/ScriptReference/Rigidbody.AddRelativeForce.html

Try (0,1000,0), (0,-1000,0), (0,0,1000), and (0,0,-1000) depending on what direction the object is facing, one of those should work.

avatar image aldonaletto · Oct 12, 2011 at 01:38 PM 0
Share

It's going to the wrong direction because AddRelativeForce is relative to the shell rigidbody, which was rotated to a random angle around z and to -90 around y.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Force applied to a Sphere from a paddle(Cube,Rectangle) 1 Answer

Adding Drag/Force to object on a slope/Hill 1 Answer

adding force 1 Answer

[Solved]Why doesn't this Rigidbody.AddForce work the way I tell it to? 1 Answer

Calculate Force needed to push RigidBody to desiref position 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