so I am trying to achieve this feat of increasing my game object speed increase if I start tapping continually and then stop and get back to normal when I start tapping slowly. I am attaching a video below which have the cube and I have a sphere and I want to have exactly this effect. I am kinda new to unity. I appreciate any help that I cam get thanks.
-
2Hi! Are you using a rigidbody with your sphere? – Delunado Jul 26 '20 at 10:36
-
1Yes I am using a rb – Mingyu Kim Jul 26 '20 at 16:47
4 Answers
Use Rigidbody2D component and addForce to it

- 3,499
- 2
- 25
- 39

- 396
- 2
- 9
-
There is no Physics2D component. There is a [Physics2D class](https://docs.unity3d.com/ScriptReference/Physics2D.html) but it's not a component and it's not used to add force to objects. Did you mean [Rigidbody 2D](https://docs.unity3d.com/Manual/class-Rigidbody2D.html)? – joll05 Jul 26 '20 at 11:13
-
you can check for the touches then you add force up. the following script will add speed to the box every touch
public Rigidbody2D myRb;
float speed = 5f;
void Update()
{
if (Input.touchCount > 0)
{
for(int i = 0; i < Input.touchCount ; i++)
{
myRb.AddForce(Vector2.up * speed);
}
}
}
dont forget to assign your object's Rigidbody to the script, also adjust speed as you want.

- 557
- 4
- 9
-
1Thanks. I will surely check this, btw I am using 3d, so I would just need to change the Rigidbody and Vector2, right? – Mingyu Kim Jul 26 '20 at 16:48
-
1you're welcome, yes exactly you will change Rigidbody2D to Rigidbody and Vector2.up to Vector3.up or manually Vector3(0,1,0) and customize it as you want – Omar Ghannou Jul 26 '20 at 18:20
As others have said, you need to check for taps and add force/velocity to your object for each one.
If the object is going up, as in the video, there's no need to write code that slows it down — gravity will do that for you. Just make sure "Use Gravity" is enabled on your RigidBody(2D) component.
Omar Ghannou put up the code for it, so I won't copy-paste it, but I wanted to add some notes:
If you're trying to match the video you showed exactly, you might want to add a 2nd argument to the
AddForce()
function: ForceMode. In your case, that would beForceMode.Impulse
. It does what it sounds like: add quick "bursts" of force. Experiment to find what you like best.If you're only looking to make your object go up, you might want to add constraints to your RigidBody to prevent it from moving on other axes. This may help you avoid slight and/or accidental movement, though be aware that you may not find out about bugs that would cause it.
If you'd like for the object to fall faster, you can modify global gravity inside Project Settings. Of course, this will affect all objects. If you just want the object to rise slower, simply decrease the amount of force added.
If you've set your RigidBody(2D) to anything other than Dynamic, you probably should change it back — it takes away a lot of the programming, which can frustrate devs who want finer control (I hate it) but is great for beginners/quick prototyping.
- If you still want to use Kinematic, you can replace
AddForce()
by simply adding to the object's velocity:rb.velocity += someVelocity * Vector3.up
, whererb
is a reference to the object's RigidBody. Do make sure to useVector2.up
if you're working in 2D, and ensure that it does in fact point up.
- If you still want to use Kinematic, you can replace
Good luck!

- 625
- 5
- 17
-
1Thanks a lot, it really means a lot that you take time and write all that just to help me. Btw my bad I should have clarified I am in a 3d environment and all objects are 3d. But I don't think it still changes so much as the perspective is still kind of 2d with 3d objects. But surely thanks a lot I willl surely try and tweak and try all these tomorrow and update here. – Mingyu Kim Jul 26 '20 at 16:52
Thanks a lot everyone for all the help, you guys are so great. I really appreciate you all taking time and helping me. It really means a lot I'm sp happy. I will try all of these method and will update everyone here.

- 127
- 1
- 11
-
1You're welcome! However, if you want to keep posting on StackOverflow and not have your questions closed all the time, you should really read up on the basic rules. For one, you should never use an answer as a comment, and you should generally avoid using comments as direct messages, too. – verified_tinker Jul 26 '20 at 20:26