Hi! I want to ask a fast question! (Moderator: Please let me get atleast 1 answer, I want to understand
understand these ray things atleast a bit, for exact my question)
So, I made many in things in Unity and C#, but no 3D things so far. I completely concentrated
on 2D-Managing games, and that’s why I have to ask here.
I made a map and some “buildspaces” in it. When you click on these Buildspaces, they should become
houses.
That’s what I made so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Superiour : MonoBehaviour
{
Camera cam;
public Transform buildspace1;
public Transform house1;
public GameObject house1GO;
// Start is called before the first frame update
void Start()
{
cam = Camera.main;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(hit.point, buildspace1))
{
buildspace1.SetActive(false);
house1GO.SetActive(true);
}
}
}
I’m like Jon Snow, I know nothing! Could you please help me?
Well you create the Ray called ray, but then you don’t use it in your raycast. You need to pass it as a parameter:
if (Physics.Raycast(ray, out hit, 100)){
if(hit.gameObject.transform == buildspace1){
buildspace1.SetActive(false);
house1GO.SetActive(true);
}
}
I put 100 units as the distance to shoot out the ray. Should work. The function has been overloaded a ton, so a lot of different combinations work. You can see the overload options in visual studio while you’re typing out the function. Hope that helps.