Placing Object When in Certain Area?
I'm creating a Plinko styled game and I have already set up the board and now I'm trying to figure out how to setup the disc that it going to be dropped from the top. I was thinking of using a raycast to find the mouse position to position the disc where the mouse is, but I can't figure out how to make it to where the player can only place it in a certain area (the top part of the board). I was thinking about sticking a quad and tagging it with "Placeable" then using rayHit.transform.CompareTag("Placeable"), but it doesn't seem to work how I was hoping? Here is the code I have so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomPos : MonoBehaviour
{
bool isPlaced;
void Start()
{
//this.gameObject.transform.position = new Vector3 (Random.Range(-3.5f, 3.5f), 7, -1);
}
void FixedUpdate ()
{
RaycastHit rayHit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out rayHit))
{
if (rayHit.transform.CompareTag("Placeable"))
{
if (Input.GetMouseButtonDown(0) && !isPlaced)
{
this.gameObject.transform.position = rayHit.point;
}
}
}
}
}
In general this approach should work. $$anonymous$$aybe the ray hits another object? If yes, which one? Try putting the "Placeable" object into a separate layer and limit the ray to that layer.
How would I go about limiting the ray to only one layer?
The Raycast method takes a "layer$$anonymous$$ask" parameter.
https://docs.unity3d.com/540/Documentation/ScriptReference/Physics.Raycast.html