Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Jump to a specific position without Rigidbody

Discussion in '2D' started by yisusgamer55, Jun 30, 2020.

  1. yisusgamer55

    yisusgamer55

    Joined:
    May 10, 2019
    Posts:
    7
    Hi, I am trying to make a Point-and-click style game.
    I use a Pathfinding system to find the paths.
    What I'm trying to do now is that when the player collides with a special collider, he jumps to a certain position (without physics).
    I have no idea how to do this, thanks for the help.
    PS: If it helps, I would like to do something like this game:


    Thanks again!
     
    katarina442 likes this.
  2. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    If you're using Unity native Pathfinding system, i asume it's a 3d game?
    Or it's a 2'5d game like the video you uploaded?

    You can make it without Physics using the projectile motion equations.
    https://en.wikipedia.org/wiki/Projectile_motion
    If you're bad at maths i can help you with it, but i need to know if it's a 2d game or 3d game (a video of the ingame would be awesome)
     
  3. yisusgamer55

    yisusgamer55

    Joined:
    May 10, 2019
    Posts:
    7
  4. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    Sry for the time, I had too much work yesterday.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Jump : MonoBehaviour
    4. {
    5.     /// <summary>
    6.     /// speed of the jump animation. Jump animation works also with Time.timeScale so you would not need to change this
    7.     /// </summary>
    8.     public float animationSpeed = 1;
    9.  
    10.     /// <summary>
    11.     /// maxheight reached in units at the jump. Took as reference the highest point between the origin and the destiny of the jump. It can't be negative
    12.     /// </summary>
    13.     public float jumpHeight = 1;
    14.  
    15.     private Vector2 diff, origin, destiny, speed;
    16.  
    17.     /// true if guy gameObjecct is jumping
    18.     private bool move;
    19.  
    20.  
    21.     /// time spent since last jump
    22.     private float time;
    23.  
    24.     private void Update()
    25.     {
    26.         //when we click at the screen we setUp the jump. Just change the condition from Input.GetMouseButton(0) to the statement you wish
    27.         if (Input.GetMouseButtonDown(0))
    28.         {
    29.             time = 0;
    30.             move = true;
    31.  
    32.             origin = transform.position;
    33.             destiny = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    34.             diff = destiny - origin;
    35.  
    36.             CalculateSpeed();
    37.         }
    38.      
    39.         //movement
    40.         if (move)
    41.         {
    42.             time += Time.deltaTime * animationSpeed;
    43.             transform.position = new Vector2(origin.x + speed.x * time, origin.y + speed.y * time + 0.5f * Physics2D.gravity.y * Mathf.Pow(time, 2));
    44.  
    45.             //when we pass the objective point
    46.             if ((diff.x > 0 && transform.position.x >= destiny.x) || (diff.x < 0 && transform.position.x <= destiny.x))
    47.             {
    48.                 //reset time and stop moving
    49.                 time = 0;
    50.                 move = false;
    51.                 //set position to the exact position
    52.                 transform.position = destiny;
    53.             }
    54.         }
    55.     }
    56.  
    57.     /// <summary>
    58.     /// set speed var with the value the gameObject shall have in order to reach the destiny point
    59.     /// </summary>
    60.     private void CalculateSpeed()
    61.     {
    62.         float maxHeight = diff.y > 0 ? diff.y + jumpHeight : jumpHeight;
    63.  
    64.         var speedY = Mathf.Sqrt(-2 * Physics2D.gravity.y * maxHeight);
    65.         speed = new Vector2
    66.         (
    67.             diff.x * Physics2D.gravity.y / (-speedY - Mathf.Sqrt(Mathf.Abs(Mathf.Pow(speedY, 2) + 2 * Physics2D.gravity.y * diff.y))),
    68.             speedY
    69.         );
    70.     }
    71. }
    72.  
    Just take this and place it at one gameObject to see how it works. Some global vars are there in order to swap it from the inspector and see the changes.
    It uses as gravity constant Physics2D.gravity value (that's just a Vector2, a number, you'll never need a Rigidbody2D) so if you want some effect like moon gravity, just swap the Vector2 to something smaller (by default it's (0, -9.8...)).
    Remember also it ignores Physics2D.gravity.x, it only takes the vertical value so if you change the gravity direction, it won't work as expected. Hope it helps :)

    PD: Funny beautiful character ^^
     
    dweekdev likes this.
  5. yisusgamer55

    yisusgamer55

    Joined:
    May 10, 2019
    Posts:
    7
    Thank you very much! I'll prove it to see that!
     
  6. yisusgamer55

    yisusgamer55

    Joined:
    May 10, 2019
    Posts:
    7
    OMG, YES, YES, YESSS. It is exactly what I wanted. Thank you very much again!!! :D
     
    dweekdev likes this.