Get All Game Objects Between 2 Points
Hi, I'm getting started making a farming/harvesting type game and I'm trying to figure out how to harvest something without actually hitting it directly.
So far, I've got it so that if you click on the thing to harvest (A simple cube at the moment), if you're within a reasonable range of it, I can act on it.
However, what I'd like to do is when I hold the mouse button down, the longer I hold it, the further the hit becomes in a straight line. i.e. less than 1 second is a click, so least powerful and must be immediately adjacent to it. However for 2 seconds for example, I'd like to harvest in a line from where I'm facing, x distance in front of me.
I'm fine with the bit about guaging how long the button has been held down, as well as how to identify a point x distance in front of the character. The bit I'm stuck on is what is a sensible way to "harvest" everything in a line if I'm not directly clicking on it?
I was thinking "get me all game objects between these 2 points and I'll harvest the ones which are "plant" objects (ignoring things like random, non harvest-able game objects)", but I'm not sure how to do that.
Also, this method has the problem that if the harvest-able plants are in front of the character, but doesn't quite intersect the line between the 2 vectors? I guess it would be better to draw a box in front of the character, but still not something I'm sure how to do.
Any suggestions?
Answer by steviebob · Jun 12, 2016 at 05:48 PM
I've just found the answer. It looks like Physics.RaycastAll is exactly what I need.
In order to get around the issue of "what if the ray jut misses it", I guess I'll just cast several at various heights and points to be sure, then filter out the duplicates.
I assume this is a reasonably efficient approach? (Should have been a comment but my reputation isn't yet high enough).
Your answer
Follow this Question
Related Questions
Check if 2 or more objects of same type is near object. 1 Answer
My collectable objects don't respond like they should after calling a coroutine for the first time? 0 Answers
creating 3d melee cone splash attack 1 Answer
InverseLerp for vector3 1 Answer
Searching a function to get the rotation of one Vector3 in relation to another 1 Answer