- Home /
Detect gameobjects via radius [PROBLEM]
Hello, thank you for taking time. I've got a maybe not that simple question. It's about the radius and these damn Physics.OverlapSphere things. I'm just not able to do it.
Short Introduction: I'm making a "Settlers of Catan" and "Civilization" inspired Hex-Game with creating custom worlds and so on. I got to the units now. I'm fine with how you can send them to positions, you can select them, send them somewhere (needs still improvement, but that project is more a test than a try to make a game). But I came to a problem now. Of course I don't want the player to be able to send his units anywhere, like to the end of the map, in one turn. My idea of making the player able to send his unit only to neighbouring province was to create a radius around the unit. He is only able to be set somewere within a distance of say 10f. I actually don't care for the number now, I want to know how to implement it. My script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BasicUnitScript : MonoBehaviour
{
public Camera cam;
Vector3 newPosition;
public LayerMask seaTileMask;
public HexagonWorldCreation worldScript;
public bool unitSelected;
public bool unitSelectable;
// Start is called before the first frame update
void Start()
{
unitSelected = false;
unitSelectable = true;
newPosition = transform.position;
}
// Update is called once per frame
void Update()
{
int layerMask = 1 << 9;
layerMask = ~layerMask;
if (Input.GetMouseButtonDown(0))
{
Ray unitSelectionRay = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(unitSelectionRay, out hit))
{
if ((hit.transform == this.gameObject.transform) && (unitSelectable == true))
{
unitSelected = true;
}
}
}
if (Input.GetMouseButtonDown(1))
{
Ray unitMovementRay = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit2;
if (Physics.Raycast(unitMovementRay, out hit2)) // If something doens't work, check the 100!
{
if ((unitSelected == true) && (hit2.transform.tag != "SeaTile"))
{
unitSelected = false;
unitSelectable = false;
newPosition = hit2.point;
transform.position = newPosition;
this.gameObject.transform.position = new Vector3(hit2.point.x, 18.7f, hit2.point.z);
}
}
Debug.Log("hit2.point: " + hit2.point + " transform.position:" + transform.position + " hit tag: " + hit2.transform.tag);
}
}
}
Could anyone please implement it for me, 'cause I got no idea. I looked the doc and the other threads up, but it didn't worked. If you could, thank you!
Note: Please no layerMasks!
You want:
Vector3 origin
Vector3 target (mouse point)
Ray ray (from origin to target)
Float distance (10.0f)
ray.GetPoint(distance);
This should give you a vector3 location at the distance you require.
But I still don't know how to implement it
What have you tried so far?
Personally I'd probably do a ray to screenpoint at the max distance, and if that ray collides with a selectable hex-tile, highlight it. Then as you move the mouse around you'll highlight just the tiles that are within range.
Answer by InfixionGames · Apr 22, 2019 at 01:46 PM
I hope this can help you
//The distance between my target (player) and my transform (enemy)
float distance = Vector2.Distance(target.position, transform.position);
//If the distance is less than the detectionRange(detectionRange is a float)
if (distance < detectionRange)
{
//Do something
Movement();
}
Thank you, but could you please explain to me how to use it with a ray? And where is detectionRange defined? But still, thank you for helping me! So, you know, it's about unit movement in Hexagon-Provinces. I want that the unit can only go to bordering provinces. So, the target.position is the position where the ray shot too, isn't it? And detectionRange is the Range in within the provinces are detected? I'm going to try it!
You define the detectionRange at the top of your script (this checks for nearby provinces), inside the if statement you would do your raycasting. If you want to move the units to a hexagon you would need to do some casting to find which hexagon was click and move the units to the center of that hexagon. So here is some more clear code.
float detectionRange = 10f;
//The distance between my target (this would be your hexagon) and my transform (this would be your unit)
float distance = Vector2.Distance(target.position, transform.position);
//If the distance is less than the detectionRange
if (distance < detectionRange)
{
//Here you would do the raycasting
}
Yeah, I could copy and paste it now, but I want to understand this, and I didn't, and I will ask till I understood it, if I ask you or any other person. I'm still bothering with when
if (distance < detectionRange)
has to do its job. It makes no sense for me. Why does it checks / detect it even before the ray is shot? I'm not saying that your code is wrong and bad and not logical, but I need some explaination. I would say that the
if (distance < detectionRane)
should be in the clause of the ray, in here:
if (Physics.Raycast(unit$$anonymous$$ovementRay, out hit2)) // Check the y if not working
{
if ((unitSelected == true) && (hit2.transform.tag != "SeaTile"))
{
unitSelected = false;
unitSelectable = false;
newPosition = hit2.point;
transform.position = newPosition;
this.gameObject.transform.position = new Vector3(hit2.point.x, 18.7f, hit2.point.z);
}
}
Your answer
Follow this Question
Related Questions
Shoot off multiple raycasts from 1 object? 2 Answers
How to detect if a raycast ray stop hitting an object 1 Answer
How to damage gameobject via raycast 1 Answer
click on a game object that script isn't attached to 1 Answer
Ray cast collider recognition and execution of a script attached to hit object 1 Answer