Making a chain of procedural jumps for board game in Unity C#

77 Views Asked by At
   I need to make a procedural jump in a board game which is called every time when I need to do so in the script. This allows player to move between waypoints. So for example the player gets the number 6 from a game dice and the script has to make 6 jumps through waypoints.

This is my script I've tried to use **Vector3.MoveTowards ** to move between waypoints but it doesn't work the way I imagined it would. I've also tried to use animation of jump but it's tied to a certain place so I cannot move between 2 and 3 point if animation is between 1 and 2 point. Please say me. Can I use an animation and how can I use it? Or how can I modify my script so it's:

  1. take next point
  2. move to it
  3. make this point as a current
  4. take next point ...
1

There are 1 best solutions below

2
KiynL On BEST ANSWER

For this, it is necessary to place the Transform fields in an array. Then implement a code similar to what I have written below. With the help of DoTweenPro plugin and using the Jump command, your problem will be solved.

public int goTo = 0;
public Transform[] points;
private int currentIndex = 0;
public float jumpPower = 1f;
public float jumpDuration = 1f;
void JumpTo(int index) // This is a recursive function that moves the character closer to the desired point with each jump.
{
    if (currentIndex != index)
    {
        currentIndex += index > currentIndex ? 1 : -1;
        transform.DOJump(points[currentIndex].position, jumpPower, 1, jumpDuration).OnComplete(() =>
        {
            if (index != currentIndex)
            {
                JumpTo(index);
            }
        });
    }
}

After specifying the transforms in the scene, just add a short test like below to the above script and that's it!

private void Update()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        JumpTo(goTo);
    }
}

enter image description here