Mingyu Kim
Mingyu Kim

Reputation: 127

How to make a gameobject speed increase continuously when tapping quickly

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.

VIDEO

Upvotes: 2

Views: 1144

Answers (4)

Mingyu Kim
Mingyu Kim

Reputation: 127

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.

Upvotes: 1

verified_tinker
verified_tinker

Reputation: 665

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 be ForceMode.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, where rb is a reference to the object's RigidBody. Do make sure to use Vector2.up if you're working in 2D, and ensure that it does in fact point up.

Good luck!

Upvotes: 1

Whitebrim
Whitebrim

Reputation: 394

Use Rigidbody2D component and addForce to it

Upvotes: 2

Omar Ghannou
Omar Ghannou

Reputation: 617

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.

Upvotes: 2

Related Questions