Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

detect which ball is going to be hit

Discussion in 'Physics' started by dccoo, Feb 7, 2018.

  1. dccoo

    dccoo

    Joined:
    Oct 3, 2015
    Posts:
    113
    Suppose the white sphere is gonna be thrown in direction of the others like in the image.

    How can I detect which sphere is going to be touched (by the white) first?

    upload_2018-2-7_0-54-34.png
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    747
  3. dccoo

    dccoo

    Joined:
    Oct 3, 2015
    Posts:
    113
  4. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    747
    It works almost exactly like RayCast, with the exception that you supply a radius as well. It will return the closest object that the ball will hit. So, it shouldn't be too heavy to use.
     
  5. dccoo

    dccoo

    Joined:
    Oct 3, 2015
    Posts:
    113
    SphereCast creates a fixed sphere somewhere in the space and if I want to get the nearest ball, I would have to move this sphere forwards (in the white ball direction) until it touches something.

    Detail: I want to know where it is going to hit before the white moves, not during its movement, that's why I'm saying the SphereCast may have to move.
     
  6. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    630
    The white ball doesn't need to be moving for the spherecast to work, but you do need to know which direction it will move. That's the direction you send the spherecast, and see what gets hit.

    However, I'm not convinced that a spherecast is the right choice here, and I believe you'll need to do something a little more complicated. Consider a "real" pool game, where you can hit the cue ball off-center to generate spin. In those cases, the ball would move along an arc, not in a straight line, so a spherecast wouldn't work.

    But if you're not putting spin on the ball, then you can use a spherecast in the direction you're currently "aiming" the cue stick, and that should tell you the first thing the white ball will hit.
     
    dccoo likes this.
  7. dccoo

    dccoo

    Joined:
    Oct 3, 2015
    Posts:
    113
    That's the point, how should I move the spherecast every frame efficiently?
     
  8. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    630
    Well, just to be clear, you don't "move" a spherecast. A spherecast is an instantaneous check you perform in a single frame. You just say how far you want it to try to go. It's the equivalent of saying, "If I were to move this sphere forward up to a certain distance, what would it hit?" or, "What things would I hit if I moved this sphere from A to B?" You don't actually move the sphere, though.

    However, you can perform a new spherecast on each frame, using whatever direction your cue stick is currently aiming. Just put the spherecast call in the Update() method, setting the proper direction each frame. Spherecasting on each frame should be fine from an efficiency standpoint.