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

Random ball direction on click

Discussion in 'Scripting' started by CodeWurm, Aug 2, 2016.

  1. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    Somebody can help me with this small game I'm making.
    I want when I click on the ball it will go a random direction, up, right, left, under.
    Right now I have this code just to test the onclick method
    Code (CSharp):
    1. public class BallClickScript : MonoBehaviour {
    2.  
    3.     void OnMouseDown()
    4.     {
    5.         {
    6.             Destroy (gameObject);
    7.         }
    8.     }
    9. }
    10.  
    I tried something with transform but I don't realy know how to exactly write it down, because I'm a beginner.
     
  2. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Do you want to apply a force so it kinda bounces away? or simply move it in a random direction
     
  3. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    I realy want a smooth bouncing away when I click on the ball, I already have a gravity force on a ball so it will go down.
     
  4. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    Anyone can still help me with my problem??
     
  5. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    To pick up a random direction use Random.onUnitSphere. Then you can use that direction to apply a force to the ball with Rigidbody.AddForce. Pay attention to ForceMode, that parameter will depend on how you want to apply the force (either a sudden kick or a continuous force over time).
     
  6. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    Oke so understand what you explained I have tested but now I just have a force that hits the ball and random direction
    But I have now idea where to put the rigidbody.addforce code, here is my code.

    Code (CSharp):
    1. public class BallClickScript : MonoBehaviour {
    2.  
    3.     public float thrust;
    4.     public Rigidbody rb;
    5.  
    6.  
    7.     void Start()
    8.     {
    9.         rb = GetComponent<Rigidbody>();
    10.     }
    11.  
    12.     void FixedUpdate()
    13.     {
    14.         rb.AddForce(0,0, thrust, ForceMode.Impulse);
    15.     }
    16.  
    17.     void OnMouseDown()
    18.     {
    19.         {
    20.             //Destroy (gameObject);
    21.             //transform.position = new Vector3(3,4,0);
    22.  
    23.             // Zorgt ervoor dat er een random kracht op de bal komt.
    24.             GetComponent<Rigidbody>().velocity = Random.onUnitSphere * 10;
    25.  
    26.  
    27.         }
    28.     }
    29. }
     
  7. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    Don't do anything in FixedUpdate, that would be applying the force 30x per second.
    Where you're setting the velocity right now, you'd do AddForce instead, with Random.onUnitSphere * thrust.
     
  8. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    This what I got, I'm getting a error
    MissingReferenceException: The object of type 'Object' has been destroyed but you are still trying to access it.


    Code (CSharp):
    1. public class BallClickScript : MonoBehaviour {
    2.  
    3.     public float thrust;
    4.  
    5.     void Start()
    6.     {
    7.         thrust = 5f;
    8.     }
    9.  
    10.     void OnMouseDown()
    11.     {
    12.         {
    13.             //Destroy (gameObject);
    14.             //transform.position = new Vector3(3,4,0);
    15.  
    16.             // Zorgt ervoor dat er een random kracht op de bal komt.
    17.             GetComponent<Rigidbody>().AddForce = Random.onUnitSphere * thrust;
    18.  
    19.         }
    20.     }
    21. }
    22.  
     
  9. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    AddForce is a method, not a variable.
    GetComponent<Rigidbody>().AddForce(Random.onUnitSphere * thrust, ForceMode.Impulse);
     
  10. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Code (csharp):
    1.  
    2. var rigidbody = GetComponent<Rigidbody>();
    3. if(rigidbody == null) return;
    4. rigidbody.AddForce(Random.onUnitSphere * thrust);
    5.  
     
    StudioZooka likes this.
  11. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    Oke I fixed that but where can I write that I only want to add to force to right or left because I'm making a 2d ball game where you need to keep the ball in the air, now the ball also use the Z as and I don't want that.
    And the force I'm using doesn't push the ball up in the air how do I fix that?
     
  12. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    In order constraint it in the X-Y plane, just put zero in the Z-component of the randomDirection vector:
    Code (CSharp):
    1. Vector3 GetRandomDirection2D()
    2. {
    3.     var dir = Random.onUnitSphere;
    4.     dir.z = 0.0f;
    5.     return dir.normalized;
    6. }
    You have to apply a force that overcomes the force due to gravity, which is Fg = mass*g, where g is 9.81 by default in Unity.
     
  13. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    Getting a error saying unexpected symbol {

    Code (CSharp):
    1. void OnMouseDown()
    2.     {
    3.         {
    4.             //Destroy (gameObject);
    5.             //transform.position = new Vector3(3,4,0);
    6.  
    7.             // Zorgt ervoor dat er een random kracht op de bal komt.
    8.             GetComponent<Rigidbody>().AddForce(Random.onUnitSphere * thrust, ForceMode.Impulse);
    9.  
    10.             Vector3 = GetRandomDirection2D()
    11.             {                                                        <<<<<<<<<<< ERROR
    12.                 var dir = Random.onUnitSphere;
    13.                 dir.z = 0.0f;
    14.                 return dir.normalized;
    15.             }
    16.         }
    17.     }
     
  14. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    That's a syntax error, what you wrote does not make sens. GetRandomDirection2D is a function:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class BallClickScript : MonoBehaviour
    4. {
    5.     public float thrust = 20.0f;
    6.  
    7.     Vector3 GetRandomDirection2D() // This is a function...
    8.     {
    9.         var dir = Random.onUnitSphere;
    10.         dir.z = 0.0f;
    11.         return dir.normalized;
    12.     }
    13.  
    14.     void OnMouseDown()
    15.     {
    16.         var impulse = GetRandomDirection2D() * thrust; // ... that is used here.
    17.         GetComponent<Rigidbody>().AddForce(impulse, ForceMode.Impulse);
    18.     }
    19.  
    20. }
     
  15. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    Thank you I'm a whole step closer to my game.
    I just have some small things I wanna know.

    - Because my ball is 3d he bounce to the right and left and sometimes he rotates and then he falls of the stage, how can I fix that?

    - Here and there I click on the ball, but the speed is kind of random and it looks like I also need to wait before I click again is there a way I can precise this?
     
  16. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    On the Rigidbody, you can freeze position and/or rotation component-wise.

    What do you mean by "kind of random", what do you want as movement?
    For waiting, you could add a timer in the Update function that would start when the ball is kicked and allow kicking the ball again only when this timer is elapsed.
     
  17. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    I got want I want I think what I did wrong was adding x and y dir, in the code, I removed that and I played around with the drag function and mass of the sphere to make the clicking and falling a little stable so you have enough time to click the ball again before it hits the ground.

    One thing I wanna say is that when I click on the ball he goes also down realy fast, wich makes it hard to keep the ball in the air like you can see in this game.

    Here is a link of the game I tried to remake: http://spele.nl/hooghouden-spel/

    Thank you for your help.
     
  18. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    You can change the gravity setting in Edit > Project Settings > Physics.

    In this game the direction of the force applied to kick the ball is not random! The ball move randomly because, well, the players movements are random... that's what makes this game fun.

    So the force direction depends on where you click on the ball. You can use Rigidbody.AddForceAtPosition to indicate where the ball is kicked. For that you would need to compute the mouse position (Input.mousePosition) from screenspace to worldspace. You can do that by either use Physics.Raycast and Camera.ScreenToRayPoint or directly compute the mouse position using Camera.ScreenToWorldPoint.