I’m not too sure how Crossy roads does its jumping, but I’m sure all the object are on a form of grid using multiple units of 1 with exact coordinates, e.g 4,4,4 etc unless I’m wrong?
I’ve been converting some of my Magica Voxel object to blender, and added them into a scene. see png
I’ve been trying to get the jumping correct I’m using this routine.
private void Launch(Vector3 startposition, Vector3 endposition)
{
// source and target positions
Vector3 pos = startposition;
Vector3 target = endposition;
// distance between target and source
float dist = Vector3.Distance(pos, target);
// rotate the object to face the target
//transform.LookAt(target, this.transform.up);
// calculate initival velocity required to land the cube on target using the formula (9)
float Vi = Mathf.Sqrt(dist * -Physics.gravity.y / (Mathf.Sin(Mathf.Deg2Rad * mAngle * 2)));
float Vy, Vz; // y,z components of the initial velocity
Vy = (Vi * Mathf.Sin(Mathf.Deg2Rad * mAngle));
Vz = Vi * Mathf.Cos(Mathf.Deg2Rad * mAngle);
// create the velocity vector in local space
Vector3 localVelocity = new Vector3(0, Vy, Vz);
// transform it to global vector
Vector3 globalVelocity = transform.TransformDirection(localVelocity);
// launch the cube by setting its initial velocity
mRigidBody.velocity = globalVelocity;
// after launch revert the switch
//mIsJumping = false;
}
This works fine but I cannot speed up the jumping, as far as I can see, so the character jumps to slowly for my liking.
-
- I know the angle I want e.g 45/60 degrees
-
- I know the distance it must travel
but I’m not to sure how to use the speed I want.
I did think of creating a series of points in an arc and movingtowards these then I could use a speed but I though that might be stupid, any idea’s?
Now when my avie gets close to a building see screenshot, it calculates the difference smaller than the maximum jumping, and only allows it to jump that far, I’m not sure if this is correct how it should be done, what do you think?
Cheers for advice.
Since you want to move between discretely-spaced points and you want to have variance in your speed, the easiest way to do this is going to be to parametrically calculate your intermediate positions.
You’re right that crossy road and frogger use quantized locations, with some exceptions, such as riding a log in the case of frogger.
But if you are at Point1 and want to move to Point2, you can move parametrically between it using Lerp, and optionally adding a vertical offset. For instance… (and I’m just gonna type this, not actually code or test it):
When you jump you would start a coroutine, like so:
// assuming this script is on the character we are moving!
IEnumerator Jump( Vector3 Point1, Vector3 Point2, float TimeToJump)
{
for (float time = 0; time < TimeToJump; time += Time.deltaTime)
{
float fraction = time / TimeToJump; // will range from 0 to 1 (but might never be precisely 1)
// this will move it straight along
Vector3 position = Vector3.Lerp( Point1, Point2, fraction);
// now here you want to bop it up a bit
float LiftTheta = (time * Mathf.PI) / TimeToJump;
float LiftAmount = 10;
// another idea: if you use a public AnimationCurve you can visually set an jump arc in the inspector
// another idea: or you could calculate a quadratic curve or any other curve you want.
position += Vector3.up * Mathf.Sin( LiftTheta) * LiftAmount;
// finally assign this position to our transform
transform.position = position;
yield return null;
}
transform.position = Point2; // make sure we are precisely where we need to be at end
}
You will need some kind of interlock to make sure the player cannot control anything during the coroutine.
Hi @Kurt-Dekker ,
I like your idea, I slapped my head when i saw your sin idea, I should of though of that, I created another idea using an animation which works but the problem is that it goes though walls or buildings in my case.
so I’m using a rigidbody trying to use MovePostion is no good as it again passes though the buildings, the only way I can think of is using is AddRelativeForce(mDirection * 64.12635F, ForceMode.Force); and stopping it
64.12635F = about one unit or there about’s
public void FixedUpdate()
{
if (mFirstInput == true)
{
//Debug.Log("Jumping is: " + mIsJumping);
CurrentLeapTime += Time.deltaTime * 5;
Perc = CurrentLeapTime / LeapTime;
mRigidbody.AddRelativeForce(mDirection * 64.12635F, ForceMode.Force);
//Vector3 mPos = Vector3.MoveTowards(mRigidbody.position, mEndPosition , Perc);
//mRigidbody.MovePosition(mPos);
//Debug.Log("Perc " + Perc) ;
if (Perc > 0.95)
{
Perc = 1;
mFirstInput = false;
mIsJumping = false;
mRigidbody.velocity = Vector3.zero;
mRigidbody.angularVelocity = Vector3.zero;
mTransform.position = mEndPosition;
}
}
}
I also just found out that AddRelativeForce can allow it to pass though the building as well, if I bash the keys a few times which I thought it would not?
Just one more think I was using lerp see below for the movemnt but again I had the problem of walking though walls etc.
// if (mFirstInput == true)
// {
// //Debug.Log("Jumping is: " + mIsJumping);
// CurrentLeapTime += Time.deltaTime * 5;
// Perc = CurrentLeapTime / LeapTime;
// this.transform.position = Vector3.Lerp(mStartPosition, mEndPosition, Perc);
// //Debug.Log("Perc " + Perc) ;
// if (Perc > 0.95)
// {
//Perc = 1;
//mFirstInput = false;
//mIsJumping = false;
////this.transform.position = mEndPosition;
// }
// }
what I’m I doing wrong here?
Cheers
If you are moving on a grid, you probably want to use a correlated grid of data to decide if you can move there, rather than the physics collision system.
And for parametrically moving it, turn off or delete the rigidbody because the code I wrote up there will do the moving for you, precisely in the curve you specify. A rigidbody is only going to fight it. Either make the RB an .isKinematic RB, or else delete it and re-add it when you land, or just don’t use rigidbodies for this solution. Such trivial grid movement can be done just manually and far more reliably than by using physics!