Player Jump 2D with little parabolic effect.

Hi all,

I am creating a 2D game, I wanted to implement jump for my player…like in a parabolic angle…
Currently I am just moving my player to the target position with MoveTowards method. I need to implement parabolic angle to it.

I’ve attached a screenshot of three objects with my player, now scenario is …when I click on a gameobject (other than the one on which player is currently) , the player should move above it (like it is at current state) …with some parabolic angle…with some smooth movements and with some sprite animations,

Please Suggest me how can I make this behaviour. Also I am using NGUI, if this helps…:)[31581-screen+shot+2014-08-27+at+12.05.14+am.png|31581]

Here is a script that demonstrates simulated jumping. It uses a Sine function for the jump. Before integrating it into your app, test it:

  • Start a 2D project
  • Make sure the camera is orthographic
  • Add a sprite
  • Add this script to the sprite
  • Run the app and click with the mouse.

The ‘Hop’ coroutine in the following script is what I believe you need for your app:

#pragma strict

public var hopHeight = 1.25;

private var hopping = false;

function Update() {
 	if (Input.GetMouseButtonDown(0)) {
 		var pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 		pos.z = 0.0;
 		Hop(pos, 0.75);
 	}
}

function Hop(dest : Vector3, time : float) {
	if (hopping) return;
	
	hopping = true;
	var startPos = transform.position;
	var timer = 0.0;
	
	while (timer <= 1.0) {
		var height = Mathf.Sin(Mathf.PI * timer) * hopHeight;
		transform.position = Vector3.Lerp(startPos, dest, timer) + Vector3.up * height; 
	
		timer += Time.deltaTime / time;
		yield;
	}
	hopping = false;
}

Here is a c# version of the @robertbu code snippet with removed the redunant first loop iteration, fixed errors, JumpProgress property and the ability to jump in another direction stopping the previous jump.

public class JumpPerformer : MonoBehaviour {
    private IEnumerator activeJumpCoroutine;

    public float JumpProgress { get; private set; }

    public void Jump(Vector3 destination, float maxHeight, float time) {
        if (activeJumpCoroutine != null) {
            StopCoroutine(activeJumpCoroutine);
            activeJumpCoroutine = null;
            JumpProgress = 0.0f;
        }
        activeJumpCoroutine = JumpCoroutine(destination, maxHeight, time);
        StartCoroutine(activeJumpCoroutine);
    }

    private IEnumerator JumpCoroutine(Vector3 destination, float maxHeight, float time) {
        var startPos = transform.position;
        while (JumpProgress <= 1.0) {
            JumpProgress += Time.deltaTime / time;
            var height = Mathf.Sin(Mathf.PI * JumpProgress) * maxHeight;
            if (height < 0f) {
                height = 0f;
            }
            transform.position = Vector3.Lerp(startPos, destination, JumpProgress) + Vector3.up * height;
            yield return null;
        }
        transform.position = destination;
    }
}

WOW! You saved my day! Thank you a lot!