Search Unity

Moving sphere along 2D 'gutter' using rb force (first 2D game)

Discussion in '2D' started by Shin_Toasty, Sep 26, 2018.

  1. Shin_Toasty

    Shin_Toasty

    Joined:
    Jun 15, 2017
    Posts:
    48
    Hail Unitarians

    I wouldn't be asking this question if I were working in 3D: I'd have no need to. I'm in a mix-up of 3 and 2D here.

    I'm making my first 2D game and I need to move a sphere down a gutter/road with bends in it. Like you might see in a pinball table. No way round using a 3D sphere I think, it needs to look like a real (patterned) ball. Gutters can look 2D, they're less important.

    I don't want to draw a motion path, I want to add force to the rb and let physics do their job, guiding the ball.

    So I've made a sphere, which is 3D of course and a (necessary) patterned texture for it and I've also made a multilayered 2D sprite where the upper layer is the gutter/road I want to the ball to follow - and then I thought I'd come here and ask the best way of doing this.

    I don't want to use anything 3D where 2 will do: don't want any RAM wasted.

    Any help would be greet.
     
  2. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    Well I'm still a noob but I would suggest lining the gutter with 2d triggers w/rigid bodies then attach an invisible 2d gameobject that follows your 3d ball with a target follow like;
    Code (CSharp):
    1. public class Follow_Target : MonoBehaviour {
    2.     public GameObject target;
    3.  
    4.     void Update()
    5.     {
    6.         transform.position = new Vector3(target.transform.position.x, target.transform.position.y, 0);
    7.        
    8.     }
    9. }
    then just add an OnTriggerEnter2D to the 2d object that will detect when you hit a collider. Last just access a variable from the collider then send that to the 2d ball as a Force.
     
    Shin_Toasty likes this.
  3. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    I think you're asking for advice on mixing 2D and 3D graphics/physics, so I'm going to focus on that.

    You probably aren't going to notice any difference in performance between 2D and 3D physics for something like a pinball table. It's not super taxing to add a dimension to the physics calculations. If there were a very high number of objects, the difference could be more noticeable, and trial and error will be helpful in determining performance targets.

    The difference in impact to performance between 2D and 3D graphics, however, is pretty huge as a general rule. I think you're already aware of this. Naturally, mixing 2D and 3D graphics is part of almost every project. For your constraint where the ball must be patterned, using a 3D object for the ball is a good call.

    In Unity, graphics and physics have little to do with each other. Exceptions include when colliders are generated based off of models or sprites and that sort of thing. You can have 2D graphics and 3D physics, for example. Since you have a ball that needs to look like it's rolling, I would use 3D physics to save time. If you used 2D physics, you'll need to do a lot of calculations to realistically rotate the ball.

    It's fine if you still want to visually represent the gutters with 2D graphics, but I think using 3D colliders would save a lot of headaches. This will be easiest to pull off visually if the camera is either fixed or orthographic, or you'll probably have issues with graphics not lining up with colliders when the camera is in certain spots.

    After saying all that, I'll point out that if you don't care whether the rotation of the ball is realistic and you're ok with having to determine the ball rotation yourself, then 2D physics is still a good option. Sometimes less realistic physics is more satisfying, and that can be more straightforward to adjust when using 2D physics.
     
    Shin_Toasty likes this.
  4. Shin_Toasty

    Shin_Toasty

    Joined:
    Jun 15, 2017
    Posts:
    48
    I've only just seen these: never got notified this thread got any replies!

    I've learned a lot since last September. The balls rolled along flat surfaces in the end - much simpler.
     
    Hyblademin likes this.