Raycast never gets called
So, I made a script where something should be instantiated where the mouse is positioned. However if (Physics.Raycast(ray)) {} never gets called. I can't seem to figure out why... Here's my current code : (this is nto the full code, but I think this is the only part needed, if not tell me. :) )
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray))
{
if (Input.GetButtonDown("Fire1"))
{
Instantiate(towers[i], Input.mousePosition, transform.rotation);
Debug.Log("Instantiated");
towerRayBools[i] = false;
}
}
Answer by MadDevil · Oct 12, 2015 at 04:47 AM
@nobx101
you have written the if conditions in a wrong way, you are raycasting before pressing the button.
try this,
if(Input.GetMouseButtonDown(0))
{
Ray ry = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ry, out hitinfo, Mathf.Infinity))
{
Instantiate(towers[i], Input.mousePosition, transform.rotation);
Debug.Log("Instantiated");
towerRayBools[i] = false;
}
}
@$$anonymous$$adDevil That didn't work :/ It's not really what I want either, I want the raycast to be "displayed" even though the player didn't press mouse button 0. I just want it to instantiate there when the button is pressed.
@nobx101
if you just want to instantiate anything by pressing a button, why are you using the raycast ??
and if you just want to debug a ray then use Debug.Drawray, rather than raycasting with help of physics.
@$$anonymous$$adDevil I use the raycast just to check if there's already a placed tower where the player wants to place a new one.
Your answer
Follow this Question
Related Questions
How do you prevent instantiation of objects inside each other with proper orientation? 0 Answers
Raycast doesn't stay in one place 0 Answers
Can You help me Fix This? (Infinite level Generator) 0 Answers
Animating a click to move character 1 Answer
How can I change this script so that each instantiated prefab spawns a set amount faster 0 Answers