1

What I want to do is, the longer player holds the screen, the longer distance the object will jump when they release the hold.

Here is my code:

private float timeBeginTap; 
switch (touch.phase)
{
    // Finger start touching the screen
    case TouchPhase.Began:
        print("Start tapping");
        timeBeginTap = Time.time;
        break;

    // Finger leaving the screen
    case TouchPhase.Ended:
        //when finger release,the object jump
        float timeHolding = Time.time - timeBeginTap;
        Debug.Log($"The player1 touched the screen for {timeHolding} seconds.");

        Vector2 jumpVelocity = new Vector2(timeHolding, timeHolding);
        rb.velocity = rb.velocity + jumpVelocity;

        break;
}

So with the code above I can get the value like 0.6324167, or 0.423567 for timeHolding, which is the amount of time that user hold the finger on screen.

And I able to make the object jump by using this code below:

Vector2 jumpVelocity = new Vector2(timeHolding,timeHolding);
rb.velocity = rb.velocity + jumpVelocity;

But the distance traveled or "force" with which the object jumps is very small and it doesn't make sense. Therefore I make an attempt with the code below:

float right = 5;
float up = 10;
Vector2 jumpVelocity = new Vector2(right*timeHolding,up*timeHolding);
rb.velocity = rb.velocity + jumpVelocity;

With the code above, the distance is of course longer. But it feels constant and not dynamic, because the relationship between the Vector2's X and Y value is always constant.

Therefore my question is,

What is the correct way to define the values of X and Y for a Vector2 object based on the value of Time?

If I missed anything, please guide me in the right direction.

Desired Outcome:

enter image description here

For example,the velocity added to the box is to let the box jump from pole to another pole and stand on it.

The velocity is define by using the time user hold on the screen,the longer the user hold on screen,the greater the velocity,means the x,y value of Vector2 is define by the timeHolding.

Now when I hold the screen for the long time,the box will jump to the 3rd or 4th pole,and it not guarantee will reach the pole.Like everything is by chance.

What I want is,the box will jump from 1st pole to 2nd pole,from 2nd to the 3rd and continuously.At the same time,the velocity to go higher or lower and the distance is depends on the time user hold the screen.

This is what I want and my limited understanding of the problem.

ken
  • 125
  • 5
  • Can you unpack for us what behaviour you want this to have, that would "feel" more "dynamic"? There's no one true standard way to make a time-varying vector, so the way you're doing it is just as "correct" as any other. What matters is the outcome. What specific outcome do you want, in terms of the arc the object travels, or the measurements of height and width it crosses? How does the current behaviour depart from that outcome? Once we have a clear definition of the behaviour you want, we can help guide you in how to get there. – DMGregory Oct 05 '19 at 12:29
  • @DMGregory Thanks for response.I added the desired outcome based on my limited understanding. – ken Oct 05 '19 at 15:09
  • How does this behaviour differ from what you get with your current code? – DMGregory Oct 05 '19 at 15:10
  • In my current code it can jump,but the problem now is,if I hold the screen in long time,the box is jump to 3rd or 4th pole.I want it jump to 2nd pole,then in 2nd pole it go to 3rd pole. – ken Oct 05 '19 at 15:21
  • So, is your velocity just too great then, if it's going too far? Would using a smaller number for right and up be enough? Or do you need to cap some maximum hold time? I still can't understand what you feel is "not dynamic" enough about your current solution. – DMGregory Oct 05 '19 at 17:41
  • so would it possible to know how much velocity needed to let the box jump from pole to pole? so in this case,I can calculate how much time needed to on screen. Is it possible to do that? – ken Oct 06 '19 at 07:47
  • ok let me take a look at the answer 1st..it have a lot to look at..thanks – ken Oct 06 '19 at 14:14
  • @DMGregory Hey sir,I read on the answer u provide,based on this answer,the value of force in this public static bool SetTrajectory(this Rigidbody2D rigidbody2D, Vector2 target, float force, float arch = 0.5f) is determine by time user hold the screen,this is what I think the hold time factor into this calculation. – ken Oct 07 '19 at 07:34
  • so the longer time user hold the screen,the greater the force in the function.based in my so limited understanding to this problem – ken Oct 07 '19 at 07:43
  • Or,the time user hold the screen,will affect the trajectory which in the answer is green,blue and yellow position. For example,if the time is short,the object will jump closer to the origin,if longer the user hold,then will jump closer or beyond the target.But my problem is,how to connect the relationship between time and the "shape" of trajectory(aka force). – ken Oct 07 '19 at 08:09

0 Answers0