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 Raffert · Mar 05, 2021 at 09:43 PM · movementrigidbody2drigidbody.addforcerigidbody.velocityrigidbody-collision

Rigidbody2D.AddForce works only once?

this code is supposed to allow the user to drag with their mouse to launch the object, and then the object should infinitly bounce off obstacles without losing velocity.


i made a simple scene for testing: (both obstacles only have a BoxColider and a sprite renderer scene


but when i launch it to the top it bounces of, and then when it hits the bottom one afterwards it just stops. however i want it to just keep bouncing between the top and bottom one.


this is the script:(attached to the main character (the circle))

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Utility : MonoBehaviour
 {
     public float PushForceMultiplier;
     public Vector2 MaxForce;
     public Vector2 MinForce;
     public Rigidbody2D rb;
 
     private Vector3 StartVector;
     private Vector3 EndVector;
     private Vector2 Force;
 
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             StartVector = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             StartVector.z = 0;
         }
         else if (Input.GetMouseButtonUp(0))
         {
             EndVector = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             EndVector.z = 0;
 
             Force = new Vector2(Mathf.Clamp(StartVector.x - EndVector.x, MinForce.x, MaxForce.x), Mathf.Clamp(StartVector.y - EndVector.y, MinForce.y, MaxForce.y));
  
             rb.velocity = new Vector2(0, 0);
 
             rb.AddForce(Force * PushForceMultiplier, ForceMode2D.Impulse);
         }
     }
 
     void OnCollisionEnter2D(Collision2D CollisionData)
     {
         rb.velocity = new Vector2(0, 0);
         rb.AddForce((-Force * PushForceMultiplier) * new Vector2(-1, 1), ForceMode2D.Impulse);
     }
 }

all help is very appreciated!

2021-03-05-22-31-23-glow-sphere-samplescene-pc-mac.png (41.1 kB)
2021-03-05-22-31-23-glow-sphere-samplescene-pc-mac.png (41.1 kB)
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
1
Best Answer

Answer by SpaceManDan · Mar 05, 2021 at 10:05 PM

You are using ForceMode2D.Impulse, https://docs.unity3d.com/ScriptReference/ForceMode.html

You want ForceMode2D.Force (using mass) or ForceMode2D.Acceleration (ignoring Mass)

Impulse = Add an instant force impulse to the rigidbody, using its mass.

That being said, you could just turn the rigidbody drag off and it will just keep flying around until you apply another force to it to stop it... or say, turn the drag back up to stop it when you are ready for it to stop.

Comment
Add comment · Show 2 · 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 Raffert · Mar 05, 2021 at 10:33 PM 0
Share

Thank you for your answer! You really helped me out!

avatar image SpaceManDan Raffert · Mar 05, 2021 at 10:35 PM 0
Share

For sure! Good luck!

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

170 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

Related Questions

Do rigid bodies with force add force to other objects? 0 Answers

Stop movement of rigid bodies 2D after collision 1 Answer

Jumping problems (how to stop a force) 1 Answer

How do I stop a RigidBody2D from moving after applying addForce? 1 Answer

Rigidbody2D adding force vs modifying velocity for character jump 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