- Home /
Question by
darkal · Jul 27, 2018 at 03:42 AM ·
gameobjectraycastraycasthitdrop
Using a ray to make drop script
So what i want to do is ray to drop objects so the ray will look down from the object and if it hits any object it places object being dropped on top. I've used rays with cameras but not sure how use it with gameobject with out a camera.
Comment
Best Answer
Answer by Carterryan1990 · Jul 27, 2018 at 03:55 AM
float disToObject = 10; //Change the distance your ray will fire accordingly;
public GameObject objectToSpawn;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
RaycastHit hit;
if(Physics.Raycast(transform.position, transform.forward, out hit, disToObject))
{
Instantiate(objectToSpawn, hit.point, Quaternion.identity);
}
}
}
}
float disToObject = 10; //Change the distance your ray will fire accordingly;
public GameObject objectToSpawn;
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
RaycastHit hit;
if(Physics.Raycast(camera.main.transform.position, transform.forward, out hit, disToObject))
{
Instantiate(objectToSpawn, hit.point, Quaternion.identity);
}
}
}
This will fire the ray from the center of your camera.
Your answer
Follow this Question
Related Questions
See what object I hit in raycast 0 Answers
How do I ask if RaycastHit returns null? 3 Answers
Grabbing MonoBehavior object from collision 1 Answer
Some GameObjects can't touch !!! 0 Answers
Raycast Based on GameObject Tag 0 Answers