Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to make hop game?

Discussion in 'Getting Started' started by jamesnguyen121294, Dec 13, 2016.

  1. jamesnguyen121294

    jamesnguyen121294

    Joined:
    Dec 7, 2016
    Posts:
    6
    qwedanejapyaoo likes this.
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,950
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You could always beg, borrow or steal the $15. Of course once you had the asset, you probably wouldn't know what to do with it. Which puts you back at the learn section.
     
  4. Fabian-Haquin

    Fabian-Haquin

    Joined:
    Dec 3, 2012
    Posts:
    231
    I understand it could be helpful to have some example like this to learn how it work.

    But it is a very simple game and you should indeed learn the basis of programming and have a least a rough understanding of how it work globally.

    Then you will be able to start creating this game by yourself.

    Take a look at Unity examples projects: https://unity3d.com/learn/resources/downloads
     
    Tset_Tsyung likes this.
  5. jamesnguyen121294

    jamesnguyen121294

    Joined:
    Dec 7, 2016
    Posts:
    6
    i just ask algorithm in order to the ball drop exact on platform. i make it but don't drop on platform anytime.
    see my game and tell me how to fix:

    distance between 2 platform is 1.5f
     
  6. Fabian-Haquin

    Fabian-Haquin

    Joined:
    Dec 3, 2012
    Posts:
    231
    Assuming you choose when you jump, you keep an offset if you jump too late:

    When you jump, you should always aim the center depth of the next platform so the speed may change smoothly.
    You need more control of your ball movements.

    Your ball position speed automatically vary on Z Axis by checking next platform Z position, controlled on X Axis and a Mathf.Sin for Y Axis.

    For Z speed, you can try something like that:
    Code (CSharp):
    1. float distToNext = Mathf.Abs(ballPosition.z - nextPlatformPosition.z);
    2.  
    3. float zSpeed = speed * Time.deltaTime  * (distToNext / distBetweenPlatforms);
    But the ball will gradually slow down while approaching the next platform.

    Instead of that, you can also use a Vector3.Lerp function with an AnimationCurve to do a lerp after each jump to have the center of the nextplaform as the destination.

    Or you can use a function to predict the velocity needed for a rigidbody to reach a choosed destination:
    http://answers.unity3d.com/questions/398953/calculate-initial-velocity-to-reach-destination-ba.html
     
    Last edited: Dec 14, 2016
  7. jamesnguyen121294

    jamesnguyen121294

    Joined:
    Dec 7, 2016
    Posts:
    6
    thanks for helping. but your code apply to ball or platform. My code just platform move Z axis. and the ball does not move z axis. It has rigidbody and jump by this code:
    var g = Physics.gravity.magnitude; // get the gravity value
    var vSpeed = Mathf.Sqrt(2 * g * jumpHigh); // calculate the vertical speed
    rigidbody.velocity = newVector3(0, vSpeed, 0); // jump ball

    the ball always has Z = 0.
     
  8. Fabian-Haquin

    Fabian-Haquin

    Joined:
    Dec 3, 2012
    Posts:
    231
    Well that's a bad approach to move your platforms instead of your ball. You are moving X objects when you could only move one.
     
    Ryiah likes this.
  9. jamesnguyen121294

    jamesnguyen121294

    Joined:
    Dec 7, 2016
    Posts:
    6
    i created a parent platform GameObject after that i added child platform into parent platform. i just move parent platform. i just don't know how to code to the ball drop on each platform at Z = 0.
     
  10. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,026
    Unless you're making an infinite runner in which case you might justify doing it because of floating point precision issues.

    That being said you're still correct that the recommended approach is to move the ball and simply move the world back to origin (0,0,0) every so often which likewise fixes the floating point precision problems while impacting performance less.
     
    Fabian-Haquin likes this.
  11. Fabian-Haquin

    Fabian-Haquin

    Joined:
    Dec 3, 2012
    Posts:
    231
    Moving "only the parent" is still like moving all platforms.

    If you really want to keep it like that, everthing I said before is still effective but it will apply to the parent instead of the ball, except for the predicting velocity stuff.
     
  12. jamesnguyen121294

    jamesnguyen121294

    Joined:
    Dec 7, 2016
    Posts:
    6
    Thank for your helping Fabian-Haquin. And i had a problem. See this:

    the problem is:
    when i touch on screen and move ball the ball jump lower than dont touch screen. (You can see it at 0:18). I don't know why met this problem.
    this is my jump ball code:

    Code (CSharp):
    1. var g = Physics.gravity.magnitude; // get the gravity value
    2. var vSpeed = Mathf.Sqrt(2 * g * jumpHigh); // calculate the vertical speed
    3. ri.velocity = newVector3(0, vSpeed, 0); // jump ball
    and this is my move ball code:
    Code (CSharp):
    1. void FixedUpdate () {
    2.         //Platform not run, dont rotate ball
    3.         if (GroundController.instance.speedGround > 0)
    4.         {
    5.             // rotate ball
    6.             transform.Rotate(new Vector3(Time.deltaTime * _rotateSpeed, 0, 0));
    7.         }
    8.  
    9.         if (isDragging)
    10.         {
    11.             //Get mouse position
    12.             var _mousePos = Input.mousePosition;
    13.             _mousePos.z = 10f; // select distanceX = 10 units from the camera
    14.             //Get position mouse in Screen
    15.             Vector3 mousePos = Camera.main.ScreenToWorldPoint(_mousePos);
    16.             Vector3 temp = transform.position;
    17.             //Get new position for ball
    18.             float _newPos =
    19.                 (mousePos.x >= transform.position.x) ? mousePos.x - distanceX : distanceX + mousePos.x;
    20.             temp.x = _newPos;
    21.             temp.x =
    22.                 Mathf.Clamp(temp.x, GroundController.instance._minPosX,
    23.                             GroundController.instance._maxPosX);
    24.             //distanceY = Mathf.Abs(distanceY - mousePos.y);
    25.             //ri.velocity = new Vector3(0, distanceY * 0.1f, 0);
    26.             transform.position =
    27.                 Vector3.Lerp(transform.position, temp, smoothSpeed);
    28.         }
    29.         if (Input.GetMouseButtonDown(0))
    30.         {
    31.             var _mousePos = Input.mousePosition;
    32.             _mousePos.z = 10;
    33.             Vector3 pressPos = Camera.main.ScreenToWorldPoint(_mousePos);
    34.             //Get distanceX beetween ball and the click mouse
    35.             distanceX = Mathf.Abs(pressPos.x - transform.position.x);
    36.             //Start drag mouse
    37.             isDragging = true;
    38.         }
    39.         if (Input.GetMouseButtonUp(0))
    40.         {
    41.             //End drag mouse
    42.             isDragging = false;
    43.         }
    44.     }
    Hope you help me.
     
  13. Fabian-Haquin

    Fabian-Haquin

    Joined:
    Dec 3, 2012
    Posts:
    231
    Where is your vSpeed computed ? Update() ?

    You should not handle one axis by using transform.position and another by using the rigidbody.
    You have to move both axis by using the rigidbody or the transform...

    I think you are overriding your transform Y position somehow when your drag your ball but I don't know where and when you move your ball on the Y axis so...
     
  14. jamesnguyen121294

    jamesnguyen121294

    Joined:
    Dec 7, 2016
    Posts:
    6
    Code (CSharp):
    1. void OnCollisionEnter(Collision col){
    2.    if(col.gameObject.tag == "platform"){
    3.        var g = Physics.gravity.magnitude; // get the gravity value
    4.        var vSpeed = Mathf.Sqrt(2 * g * jumpHigh); // calculate the vertical speed
    5.        ri.velocity = newVector3(0, vSpeed, 0); // jump ball
    6.    }
    7. }
    i computed vSpeed when the ball touch platform.
     
  15. Fabian-Haquin

    Fabian-Haquin

    Joined:
    Dec 3, 2012
    Posts:
    231
    I will not find a solution for you but give you tips to find the solution by yourself.

    • Output the ball Y value using Print() or Debug.Log() or display the value on the screen to check if the ball is indeed going less high.
    • Put the Scene view aside with a side-ortho view and use the pause/step buttons to visually see it without perspective deformation.
     
    noumannoor987 likes this.
  16. kuteyash

    kuteyash

    Joined:
    Oct 22, 2020
    Posts:
    3
     
  17. kuteyash

    kuteyash

    Joined:
    Oct 22, 2020
    Posts:
    3
    I want to move my 3D gameObject sphere in my game just like Hop game sphere but I don't want to jump that object I just want to move my gameObject forward on plane surface but it should follow the direction of player's Finger like this Hop game means How to move 3d Object Along with Finger I have one code on that but using that code object is moving left and right but only when mouse point or touch is on that gameObject only if I touch on screen it not moving
     
  18. unity_rzQSwUvJF1UZXQ

    unity_rzQSwUvJF1UZXQ

    Joined:
    Oct 28, 2020
    Posts:
    10
    Hi there! Did you resolve that problem? I am having the same issue on my project.
    Hope you could help me