- Home /
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..:)
Is this a platformer where you can control the jump by keys or do you need him to jump automatically from any given point A to B ?
The player jumps automatically from any given point A to B...when clicked on point B
Answer by robertbu · Aug 26, 2014 at 08:49 PM
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;
}
Hey thanks.!! I needed this only... One more thing..Could you convert 'Hop' function for C#. Actually I use C# ..but I can't figure out what to place in line 16 and 27 ..to convert this to C# : Line 16 - if (hopping) return; Line 27 - yield;
Lines 16 and 27 are a bit tricky. Here is the conversion:
using System.Collections;
using UnityEngine;
public class Hopper : $$anonymous$$onoBehaviour {
public float hopHeight = 1.25f;
private bool hopping = false;
void Update() {
if (Input.Get$$anonymous$$ouseButtonDown(0)) {
Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pos.z = 0.0f;
StartCoroutine(Hop(pos, 0.75f));
}
}
IEnumerator Hop(Vector3 dest, float time) {
if (hopping) yield break;
hopping = true;
var startPos = transform.position;
var timer = 0.0f;
while (timer <= 1.0f) {
var height = $$anonymous$$athf.Sin($$anonymous$$athf.PI * timer) * hopHeight;
transform.position = Vector3.Lerp(startPos, dest, timer) + Vector3.up * height;
timer += Time.deltaTime / time;
yield return null;
}
hopping = false;
}
}
Okay thanks...but one more prob now.. I got this working in the test proj..But when implemented to $$anonymous$$e...I got this :
When clicking other objects ...I passed their local positions in Hop function ....so that the player hops to the object position....but player does not hop...it only moves to the target position. Also I've replaced transform.position to transform.localposition inside Hop function, since I am not dealing with world coordinates.
So..Am I doing something wrong here...Please explain.
I need to see your code, but localPosition is only going to work if both the hopping objects and the target objects have the same parent, and you must change lines 21 and line 26. That is everything (start and end) must be in the same local coordinate system. It might be easier to just use world coordinates, even if that means converting local to world.
ok Here is the code I used :
public void DoHop(Vector3 dest, float time)
{
// dest = Camera.main.ScreenToWorldPoint(dest);
StartCoroutine(Hop(dest, time));
}
IEnumerator Hop(Vector3 dest, float time) {
if (hopping) yield break;
hopping = true;
var startPos = transform.localPosition;
var timer = 0.0f;
while (timer <= 1.0f) {
var height = $$anonymous$$athf.Sin($$anonymous$$athf.PI * timer) * hopHeight;
transform.localPosition = Vector3.Lerp(startPos, dest, timer) + Vector3.up * height;
timer += Time.deltaTime / time;
yield return null;
}
hopping = false;
}
The above is the Hopping Class.
Now When clicking other objects 'On_ChnagePlayerPosition (GameObject target)' will change player position. And I did this :
public void On_ChnagePlayerPosition (GameObject target)
{
Vector3 targetPos = new Vector3(target.transform.localPosition.x, target.transform.localPosition.y + 30 , target.transform.localPosition.z);
playerGO.GetComponent<Hopping>().DoHop(targetPos, 0.75f);
}
So is this correct?
Answer by OSMET · Jan 04, 2018 at 12:56 PM
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;
}
}
Your answer
Follow this Question
Related Questions
Cloud recognition in Vuforia 0 Answers
Load a Level on Touch 1 Answer
How to make blinking eyes effect like in scp:cb? 0 Answers
Change Camera 3 Answers