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 spooneystone · Jun 08, 2015 at 12:28 AM · transformrigidbody2daddforce

AddForce transform.right not working correctly in 2d

Why does the rigidbody2D addforce work when using transform.up perfectly but using transform.right doesn't work correctly. It seems to just transform the players position.

     public Transform fireDirection;
     public float speed = 1f;
     Transform collisionLinePoint1;
     Transform collisionLinePoint2;
     public int health = 100;
     Animator alienAnime;
     Rigidbody2D rb;
     bool colliding; 
     GameObject player;
     public Player_Control playerControl;
     Rigidbody2D playerRb;
 
     void Awake()
     {
         player = GameObject.FindWithTag ("Player");
         collisionLinePoint1 = transform.FindChild ("collision_S");
         collisionLinePoint2 = transform.FindChild ("collision_E");
         collisionLinePoint1.localPosition = new Vector3 (1.15f, 1.82f, 0f);
         collisionLinePoint2.localPosition = new Vector3 (1.15f, -1.94f, 0f);
     }
     // Use this for initialization
     void Start () {
 
         playerRb = player.GetComponent<Rigidbody2D> ();
         alienAnime = GetComponent<Animator> ();
         rb = GetComponent<Rigidbody2D>();
         player.GetComponent<Rigidbody2D> (); 
 
 
     
     }
 
     void Update () {
 
         Death ();
 
         if (Input.GetButtonDown ("Fire3")) {
             // Doesnt work correct???
             playerRb.AddForce (transform.right * 400);
             // This works fine
             playerRb.AddForce (transform.up * 400);
             
         }
 
     }


Comment
Add comment · Show 2
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 Calum1015 · Jun 08, 2015 at 03:04 AM 1
Share

To help you we need more information. What does it NOT do? What does it do? What do you mean by "doesn't work correct". Etc. UA useres will be able to help you more if you answer one of these questions.

avatar image spooneystone · Jun 08, 2015 at 11:45 AM 0
Share

Ok. When playerRb.AddForce (transform.up 400); is used you can see addForce is applying force using physics like it should. When I use playerRb.AddForce (transform.right 400); its doesn't appear to use force, it just transforms the players position to another location in one frame.

2 Replies

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

Answer by Guppie1337 · Jun 09, 2015 at 07:39 PM

One reason this could be an issue is because you're condition is only met once on the frame the button is pressed. If you're expecting the object to move immediately and gain the expected velocity, you can AddForce with ForceMode.Impulse.

 //Only returns true for the frame it was pressed.
 if (Input.GetButtonDown ("Fire3"))
 
 playerRb.AddForce (Vector2.right * 400, ForceMode2D.Impulse);

Keep in mind that transform.right and Vector2.right are essentially the same thing (1, 0). So by multiplying that by 400 you're getting (400, 0) every frame. Unless you've adjusted all other physics properties, this is gonna send your object flying with default values. Hope this helps.

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 The_Guy · Jun 08, 2015 at 07:44 AM

I think you mean to be using

playerRb.AddForce (Vector2.right * 400);

and also you could make 400 a public variable to edit it in the inspector on the fly.

Comment
Add comment · Show 4 · 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 spooneystone · Jun 08, 2015 at 11:46 AM 0
Share

Sorry still has same problem.

avatar image The_Guy · Jun 08, 2015 at 05:49 PM 1
Share

What are you trying to move and how much mass do you have set on the rigidbody? Collider materials automatically come with a friction property of 1, so there might be too much friction and 400 wont push him enough. Try a ridiculous number like 50000 and see if that pushes him. Also I think you want to try keeping rigidbody function inside of FixedUpdate() ins$$anonymous$$d of just Update().

avatar image spooneystone · Jun 09, 2015 at 06:31 AM 0
Share

Yea I can see what's happening now. it does move more but still looks like its transporting to the position rather that physics moving him. I think I have mistaken what addforce is. I changed the if (Input.GetButtonDown ("Fire3")) to (Input.GetButton ("Fire3")) and I can see it works correctly when I hold down "Fire3". Is there anything else available to add sudden force to a object in one frame in 2D like Explosive force?

avatar image The_Guy · Jun 09, 2015 at 02:52 PM 0
Share

To achieve the sudden force you would want to specify Impulse in an AddForce call. So it would look like this: myRigidbody2D.Addforce(Vector2.right, Force$$anonymous$$ode2D.Impulse);

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Best way to move a Rigidbody2D from point A to point B (Mouse Position) 3 Answers

What is equivalent to Transform.up when moving and rotating the RigidBody2D component instead? 1 Answer

,How to change the direction of an already moving GameObject 2 Answers

Object keeps moving after hit 0 Answers

Will not rotate when going different speed? 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