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:
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.
pole
.I want it jump to 2ndpole
,then in 2ndpole
it go to 3rdpole
. – ken Oct 05 '19 at 15:21right
andup
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:41box
jump frompole
topole
? so in this case,I can calculate how muchtime
needed to on screen. Is it possible to do that? – ken Oct 06 '19 at 07:47force
in thispublic static bool SetTrajectory(this Rigidbody2D rigidbody2D, Vector2 target, float force, float arch = 0.5f)
is determine bytime user hold the screen
,this is what I think thehold time
factor into this calculation. – ken Oct 07 '19 at 07:34force
in the function.based in my so limited understanding to this problem – ken Oct 07 '19 at 07:43time
user hold the screen,will affect thetrajectory
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 betweentime
and the "shape" oftrajectory
(aka force). – ken Oct 07 '19 at 08:09