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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Jumping a ball on the basis of the side which I click on it using mouse

Discussion in 'Scripting' started by ketanjainn, Dec 27, 2019.

  1. ketanjainn

    ketanjainn

    Joined:
    Dec 20, 2019
    Posts:
    3
    I want to make a 2D ball jump as well as move it either to left or to the right (while in the air) based on the side of the ball which the player clicks (i.e if I click on the left half of the ball it should jump towards left and If I click on the right side of the ball, It should jump to the right)
    I am new to this platform so please mark any mistake you find.
    And also please explain the solution you give.
    Here is the code :-

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BallController : MonoBehaviour
    6. {
    7.     private Rigidbody2D rb;
    8.     public const float Jump_Amt = 20f;
    9.  
    10.     private void Awake()
    11.     {
    12.         rb=GetComponent<Rigidbody2D>();
    13.     }
    14. void Update ()
    15.     {
    16.         if (Input.GetMouseButtonDown(0))
    17.         {
    18.             Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    19.             Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
    20.            
    21.             RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
    22.            
    23.             if (hit.collider != null)
    24.             {
    25.                 if(rb = hit.transform.GetComponent<Rigidbody2D>())
    26.                 {                                  
    27.                    Jump();
    28.                 }        
    29.             }
    30.         }
    31.    
    32.     }
    33.  
    34.  
    35.     private void Jump()
    36.     {
    37.         rb.velocity = Vector2.up * Jump_Amt;
    38.     }
    39.  
    40. }

    And here is my project. Annotation 2019-12-27 194847.png
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Hi and welcome!

    If you want the ball to jump in the direction of the mouse, you could just form a Vector between the current ball position and the mousePos2D you calculate (by subtracting them). Depending on how you want the ball to behave, you could only take the x or y part of that vector, normalize it, amplify it, or leave it as it is, and then simply add it to the current ball (rigidbody) velocity or add it as force using AddForce, possibly using Impulse mode depending on what you want. This would always make the ball go towards the mouse position, even tho you may want to limit how often the player can make these inputs (for example only every 2 seconds, or when the ball does not move) to prevent the player from being able to fly the ball whereever he wants simply by clicking fast.

    As far as 'mistakes' go:
    • Everything to do with Rigidbodies (physics in general) should be executed in FixedUpdate(), so try to only collect player inputs in Update() and then execute your physics in FixedUpdate() based on the inputs you received. Update() is called once per frame, FixedUpdate() is called once per fixed timestep to allow for more precise physics calculations. Depending on the framerate and the fixed timestep, this may mean it gets called more often or less often than Update() - generally speaking less often on fast hardware and possibly more often on slow hardware.
    • Avoid using GetComponent in Update() if possible (which is often the case). The same goes for any variant of 'Find()'. Never use these outside of prototyping, as they will quickly become a performance nightmare and can always be avoided.
    • Consider taking a look at the C# naming conventions. You have to decide for yourself whether you want to stick with them or not, but it makes code look more uniform and thus easier to read and debug for others. As a rule of thumb, everything should be written camelCase, with method, class and property names using UpperCamelCase. Underscores are in Unity often used to indicate member variables, and in case you like the 'capslock constanty' conventions, there as well. Other than that, underscores are not really used in C#.
     
    ketanjainn likes this.
  3. ketanjainn

    ketanjainn

    Joined:
    Dec 20, 2019
    Posts:
    3
    First of all ThankYou for pointing out my mistakes.
    And I don't want to move the ball 'according to the mouse.
    I want the player to click with the mouse on the ball and for instance, if the player clicks on the right-hand side of the ball,
    the ball should jump towards the right
    If on the left side of the ball, the ball should jump towards the left.

    Recently what I did to solve this problem, I created two semi-circles and joined them using FixedJoints2D.
    But, now I am not able to make them jump now.
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Similar to what i said before, you could then just use the x-coordinate of the click plus the ball position to figure out the direction, then apply a force in some upwards angle (angle or magnitude depending on the click position or not, depending on what you want to do) in the direction of the click. It's more or less just a question of what exactly you want to implement. Anyways, i hope this helped you a bit.
     
  5. ketanjainn

    ketanjainn

    Joined:
    Dec 20, 2019
    Posts:
    3
    Yes, Thank You!