A help with this issue is much appreciated.
so my mechanics work as follows, I instantiate boxes (inventory01 in current code) wherever i click on a layer called “PlacingLayer”.
How can in code to check if the object i’m placing is colliding with other objects even of the same type, if there’s no collision then instantiate otherwise don’t instantiate and print “Select an empty space” .
using UnityEngine;
using System.Collections;
public class PlayerInput : MonoBehaviour {
public LayerMask BoxLayer; //Put the boxes from the inventory on that layer
public Transform Inventory01;
void Update () {
if(Input.GetMouseButtonDown(0)){
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
Debug.DrawRay (ray.origin, ray.direction * 20, Color.yellow, Time.deltaTime);
if (Physics.Raycast (ray, out hit, 100, BoxLayer)) {
Debug.DrawLine (ray.origin, hit.point);
print ("Check Point 1 | " + gameObject.tag);
if ( hit.transform.tag == "PlacingLayer" )
{
Vector3 point = ray.origin + (ray.direction * 15);
Instantiate(Inventory01, new Vector3(point.x, point.y, 0 ), Quaternion.identity);
}
}
}
}
}
What I want to happen is to check if the object i’m placing is colliding with other objects even of the same type, if there’s no collision then instantiate otherwise don’t instantiate and print “Select an empty space” .
I’ve been trying to implement it but kind of failing, still new in programming, how would you implement it onto this code?
Like how to tell the code if there’s nothing within the radius then do Instantiate(Inventory01, new Vector3(point.x, point.y, 0 ), Quaternion.identity);
If that still gives you no ideas then you probably should go here:
If you familiarize your self with this two resources and still have trouble handling the task then you should at least be able to tell what specific problem you are having with implementing this.
Pirs01 I went through these links before you wrote them and then came here because I’m stuck, and my question was on how to implement it on this current code, I didn’t ask for links, I tried and cannot get it to work that’s why I want to see how would someone else would tackle and solve the problem through this code.
That information should be sufficient though. Try writing some tests to see how OverlapSphere works. Once you’re confident in that, try it out. If you’ve got specific code that should work and isn’t, then post it and someone may be able to tell where the problem is. But just copying code without understanding the components of it won’t get you very far.
You cant just say “this is what I want my program to do” and expect someone to code it. If you tried to implement this then you should be able to tell what is it specifically you are having problem with? Don’t you understand the documentation for OverlapSphere? Don’t know how to calculate the radius for the sphere so it matches more less what you are instantiating? What is it? For your general question how to go about implementing this provided links are sufficient answer. If you want help to be more specific you need to be more specific about your problem.