How can I find object ?
Hİ, This is my code to find the object nearest.Perceives the distance or proximity to the object.But the same object is being copied.When they are more than one, the code is not starting to work properly anymore. As objects multiply, I want the closest object of raycasting to react.All objects are now reacting.I've read most of the similar issues.But I do not have what I need.
public Transform box;
private Ray ray;
RaycastHit hit;
if (Input.GetButton ("Fire2")) {
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray.origin, ray.direction, out hit)) {
Vector3 pos = box.transform.position;
float distance = Vector3.Distance (pos, hit.point);
print ("there" + distance);
if (distance > 1.5) {
print ("it is long distance");
Debug.DrawRay (pos, transform.position, Color.red, 1.0f);
} else {
print ("Near the distance");
Debug.DrawRay (pos, transform.position, Color.green, 1.0f);
}
}
}
It is output like this;
But I want it this way, Whichever is closest, it interacts with the box;
Answer by SohailBukhari · Apr 25, 2017 at 11:28 AM
First of all you are not checking distance from more than one objects, so here you need all the references of the Transforms where you want to calculate the distance. Now you are just checking distance of one object, so you needs to change your code little bit. Make an array of transform and pass the Obstacles which you have in the inspector. After Passing the transforms then in the update method check distance from each object using foreach or you can use linq class.
using UnityEngine;
public class RaycastTest : MonoBehaviour
{
private RaycastHit _hit;
private Ray _ray;
public Transform[] ObstaclesaObjects;
// Update is called once per frame
private void FixedUpdate()
{
if (Input.GetButton("Fire2"))
{
_ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(_ray.origin, _ray.direction, out _hit))
{
foreach (var item in ObstaclesaObjects)
{
var distance = Vector3.Distance(item.position, _hit.point);
print("there" + distance);
Debug.DrawRay(item.position, transform.position, distance > 1.5f ? Color.red : Color.green, 1.0f);
}
}
}
}
}
Thank you for your answer @SohailBukhari so, the black boxes in the picture are added later on the scene.When I click with the mouse the ground is being destroyed and replaced with these black boxes.It looks like I can not do what I want with the list method because it is an Instantiate.I tried many ways about it but it didn't work.In fact I only want to check around the black box.
private Ray ray;
private RaycastHit hit;
private Vector3 endPoint;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetButton ("Fire2")) {
if (Physics.Raycast(ray.origin, ray.direction, out hit, $$anonymous$$athf.Infinity))
{
if (Physics.Raycast (transform.position, Vector3.left, out hit, 1.0f)
|| Physics.Raycast (transform.position, Vector3.right, out hit, 1.0f)
|| Physics.Raycast (transform.position, Vector3.forward, out hit, 1.0f)
|| Physics.Raycast (transform.position, Vector3.back, out hit, 1.0f)) {
if (hit.collider.transform.tag == "blackbox") {
print ("found! :)");
}
}
Debug.DrawRay (hit.transform.position, transform.right * r1, Color.gray);
Debug.DrawRay (hit.transform.position, -transform.right * r1, Color.gray);
Debug.DrawRay (hit.transform.position, transform.forward * r1, Color.gray);
Debug.DrawRay (hit.transform.position, -transform.forward * r1, Color.gray);
Debug.DrawRay (ray.origin, ray.direction * 50, Color.green);
endPoint = hit.point;
} else {
endPoint = ray.origin + ray.direction.normalized*distance;
Debug.DrawRay (ray.origin, ray.direction * 50, Color.black);
}
}
I wrote this code before but it didn't work.I do not know if my method is wrong, but can't it be controlled around Raycast?
i can't understand whats really your problem,What you tried in the list method ?. if you want to detect only black box then simply check whether its tag matched or not. assign a tag to black box and then check.
you can track your instantiated objects by simply saving in array or list
you are casting ray from transform transform.position so tell me one thing whats your transform. If the script is attached on the box then transform will be your box. ins$$anonymous$$d of casting ray detect other boxes by math
Lets Suppose you have i number of rows and j number of columns. then find i by j-1 for left and j+1 for right. Then j by i-1 up box and j+1 down box. If your problem not solved then send me Unity package of your code and i will check.
I've been dealing with it since morning, but I failed. :( I put the link down.Thank you for your help. @SohailBukhari
Script raycast code which i change.
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray.origin, ray.direction, out _hit, $$anonymous$$athf.Infinity))
{
if (Physics.Raycast(_hit.transform.position, Vector3.left, out _hitLeft, 1.0f))
{
if (_hitLeft.collider.gameObject.tag == "plane" && !_left)
{
Debug.Log("tag found");
_left = true;
_found++;
}
}
else if (Physics.Raycast(_hit.transform.position, Vector3.right, out _hitRight, 1.0f))
{
if (_hitRight.collider.gameObject.tag == "plane" && !_right)
{
_right = true;
_found++;
}
}
else if (Physics.Raycast(_hit.transform.position, Vector3.forward, out _hitup, 1.0f))
{
if (_hitup.collider.gameObject.tag == "plane" && !_up)
{
_up = true;
_found++;
}
}
else if (Physics.Raycast(_hit.transform.position, Vector3.back, out _hitDown, 1.0f))
{
if (_hitDown.collider.gameObject.tag == "plane" && !_down)
{
_down = true;
_found++;
}
}
if (_found == 4)
{
Debug.Log("Found");
}
Debug.DrawRay(_hit.transform.position, Vector3.left*_res, Color.yellow);
Debug.DrawRay(_hit.transform.position, Vector3.right*_res, Color.blue);
Debug.DrawRay(_hit.transform.position, Vector3.forward*_res, Color.green);
Debug.DrawRay(_hit.transform.position, Vector3.back*_res, Color.black);
}
}
Download Project and check.
Answer by CassMeck · Apr 27, 2017 at 10:21 AM
Thank you so much @SohailBukhari.I have reviewed the changes.I could not think of using an boolean.I used the same raycast for every direction.I guess I could not do it for this reason. By the way, the reset was super.When each box is placed, the boolean starts from scratch.Very logical.Thank you for your time. :)