Search Unity

Two questions

Discussion in 'Getting Started' started by Smitty3264, Oct 2, 2020.

  1. Smitty3264

    Smitty3264

    Joined:
    Sep 12, 2020
    Posts:
    38
    Hello all. I'm making a pool game in 2D format with 3D colliders. My first question is how to actually get the balls to go sink into the pocket along the z axis then move behind the table to be destroyed. I thought about trigger colliders but wouldn't every tagged ball move the same way once one ball entered the trigger? Is there some way to use triggers in a way that moves one ball at a time? Second problem is using the UI button.
    When the player touches the button cue ball should move in the direction of last clicked/touched coordinates. I know forceMode.impulse is what I want but an extensive search turned up nothing for answers. I was using a script but sorry, I can't get to it now as my computer is down. I guess I'm asking, how to record last touched coordinates with raycast then send an object to that direction with forcemode impulse when the UI button is touched? Thank you in advance.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    A trigger collider could work fine. But in OnTriggerEnter you just have it affect the specific ball which entered the trigger, not all balls. See how OnTriggerEnter is implemented. It automatically provides the Collider instance which entered the trigger. That would be the Collider you have attached to the specific ball. I'd probably have a script attached to the ball which handles having the ball sink into the pocket by just calling a function.
    https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html

    So in your OnTriggerEnter the first thing you'd do is check that the GameObject the collider is attached to is in fact a ball. I'd probably just give them all the tag "Ball" which you can check for with CompareTag. If true, you can then GetComponent for your ball script attached to the ball, and then tell it to sink (and probably some scoring mechanic gets handled as well).

    Might look something like this
    Code (csharp):
    1. void OnTriggerEnter(Collider ballCollider)
    2. {
    3.     if (ballCollider.gameObject.CompareTag("Ball")
    4.     {
    5.         BallScript ballScript = ballCollider.gameObject.GetComponent<BallScript>();
    6.         if (ballScript != null)
    7.         {
    8.             ballScript.FallInHole();
    9.         }
    10.     }
    11. }
    I'm a bit rusty on exactly how this is best handled. Last time I did this I think I just calculated the angle relative to the player (in this case the cue ball) rather than use a specific point. But if no one else answers later, I'll try to remember to open up the project I did 4 years ago and see how I did it. Good luck to you, and have fun.
     
  3. Smitty3264

    Smitty3264

    Joined:
    Sep 12, 2020
    Posts:
    38
    Thank you for your answer but unfortunately i'm getting error message cs 1547 keyword void cannot be used in this context. I used your advise precisely. I must be missing curly brackets somewhere. can you look at your code again and tell me where to add them I have already tried adding them in several places but to no avail. thank you
     
  4. Smitty3264

    Smitty3264

    Joined:
    Sep 12, 2020
    Posts:
    38
    sorry I forgot this script

    public class triggger1 : MonoBehaviour,
    void OnTriggerEnter(Collider ballCollider)
    {
    if (ballCollider.gameObject.CompareTag("Ball")
    {
    BallScript ballScript = ballCollider.gameObject.GetComponent<BallScript>();
    if (ballScript != null)
    {
    ballScript.FallInHole();
    }
    }
    }
     
  5. Smitty3264

    Smitty3264

    Joined:
    Sep 12, 2020
    Posts:
    38
    I'm also new to the forum. how do I properly load a script
     
  6. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    See how to use code tags here:
    https://forum.unity.com/threads/using-code-tags-properly.143875/

    Two things wrong:
    • You have a comma after MonoBehaviour, which is a syntax error. Classes should be enclosed between curly-braces
      {}
    • You're missing an extra closing parenthesis where you have
      if(ballCollider.gameObject.CompareTag("Ball")
    Code (CSharp):
    1. public class triggger1 : MonoBehaviour {
    2.     void OnTriggerEnter(Collider ballCollider) {
    3.         if(ballCollider.gameObject.CompareTag("Ball"))
    4.         {
    5.             BallScript ballScript = ballCollider.gameObject.GetComponent<BallScript>();
    6.             if(ballScript != null) {
    7.                 ballScript.FallInHole();
    8.             }
    9.         }
    10.     }
    11. }
    On an extra note, class names typically start with a capital letter by convention.
    I.E: "trigger1" would be "Trigger1".
    This won't break your script though, and you can feel free to keep using lowercase-starting letters if you wish.
     
  7. Smitty3264

    Smitty3264

    Joined:
    Sep 12, 2020
    Posts:
    38
    Using the script you gave now gives me an error cs0246 (10,14) BallScript could not be found. Yes, I know learning something new sucks especially something like scripts. Please have patience with my learning curve lol.
     
  8. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    I'm guessing you're following a tutorial then? I had assumed you already made a
    BallScript
    class.

    Anyway, that's what the error means; there's no such class or other type named "BallScript" in your project.
    If you are following a tutorial, perhaps you accidentally missed the part where
    BallScript
    was created?

    Edit:
    Nevermind, I just realized this is the example script @Joe-Censored posted above.
    "BallScript" in this context is just pseudo-code. The script was just meant to be a general guide on how you could implement what you're trying to do, not an exact solution.
     
  9. Smitty3264

    Smitty3264

    Joined:
    Sep 12, 2020
    Posts:
    38
    I take it it's a similar deal with FallInHole. I must need a script for both
     
  10. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Nah, "FallInHole" would be a method of the "BallScript" class, in the same way that
    GetComponent
    is a method of the
    GameObject
    class.

    This brief tutorial covers classes & methods in C#:
    https://www.tutorialsteacher.com/csharp/csharp-class
     
  11. Smitty3264

    Smitty3264

    Joined:
    Sep 12, 2020
    Posts:
    38
    Code (CSharp):
    1. Transform.Translate(vector3.forward * Time.deltaTime);
    2.  
    That's my ball script so basically I need a definition for FallInHole somewhere in this script, Right?
     
  12. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Sure. What that method does is up to you.
     
  13. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    When I wrote "BallScript", that was a placeholder for whatever script you planned on attaching to the ball GameObject itself. I just gave it a name which described what it was a placeholder for, not a literal class name to be used verbatim. I have no idea what the code for sinking into a pocket is supposed to look like in the context of your game, which is why I didn't attempt to write an example. Just that you'd have some script attached to the ball, you get a reference to that script when it enters the trigger collider, and you then tell that script to run whatever code it has to fall into the pocket.

    Maybe falling into the pocket runs an animation, maybe it literally "falls" in 3D, maybe you just shrink it for a faked 2D effect of moving away from the camera, maybe you just destroy the ball object and do some cool particle effect, maybe you do some earthquake effect and a slot machine dumping a pile of quarters sound. No idea what you want that code to really do. :)
     
    Vryken likes this.
  14. Smitty3264

    Smitty3264

    Joined:
    Sep 12, 2020
    Posts:
    38
    Can't get anything to happen with this in my ball script
    Code (CSharp):
    1. Public void FallInHole()
    2. transform.Translate(Vector3.forward * Time.deltaTime);
    I attached this to my cue ball to test it, tagged it as "Ball". I have a 3d cylinder over the pocket as the trigger attached your code to the cylinder.
    What I want to happen is upon entering the trigger the tagged ball moves down on the z axis ( remember this is a 2d game) then move towards the center of the table. I did my research on methods and moving objects. What is wrong with this script?
     
  15. Smitty3264

    Smitty3264

    Joined:
    Sep 12, 2020
    Posts:
    38
    Hey thanks for the help. I used this in my ball script for my method

    Code (CSharp):
    1. transform.position += new Vector3( 0f, 0f, 0.7f);
    Works like a charm. Balls sink. I'll be adding to this script so I'll keep you posted maybe ask for more advice if another extensive research turns up nothing. Thanks joe-censored