• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by superbsumit · Aug 26, 2014 at 07:18 PM · 2d-physicsparabola

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..:)alt text

screen shot 2014-08-27 at 12.05.14 am.png (123.4 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoseKills · Aug 26, 2014 at 08:35 PM 0
Share

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 ?

avatar image superbsumit · Aug 28, 2014 at 05:17 PM 0
Share

The player jumps automatically from any given point A to B...when clicked on point B

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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;
 }


Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image superbsumit · Aug 28, 2014 at 05:15 PM 0
Share

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;

avatar image robertbu · Aug 28, 2014 at 06:22 PM 1
Share

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;
     }
 }
avatar image superbsumit · Aug 28, 2014 at 06:57 PM 0
Share

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.

avatar image robertbu · Aug 29, 2014 at 02:05 AM 0
Share

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.

avatar image superbsumit · Aug 29, 2014 at 03:39 AM 0
Share

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?

Show more comments
avatar image
0

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;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by bruevich · Sep 14, 2018 at 11:40 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cloud recognition in Vuforia 0 Answers

FMOD FAILED TO INITAILIZE THSI MAY BECAUSE OF YOUR SOUND 1 Answer

Adding force to 2D RigidBody has no effect. 1 Answer

HingeJoint2D.jointAngle flips between two values based on rotation 0 Answers

2D Isometric Rigid Body Collision Resolution (collide and side) Not working 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges