- Home /
Invisable Projectile; Instantiate at Collisions
Hi, is there any way that I can instantiate something only at game objects that have a collider attached to it? I don't want to see the prefab travel, just instantiate at a collider (a wall).
For example, if the player is facing toward a wall that is far away and I click the fire key (Input.GetAxis("Fire")) the prefab would instantiate at the collision (the wall)?
public class InstPrefabs : MonoBehaviour {
private void InstThePreFab() {
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, fwd, Mathf.Infinity)) {
Instantiate(prefab, transform.position, Quaternion.Identity);
}
}
}
This is kinda funky.
What I have, is when anything (both raycasts and projectiles) collide with something then it plays the collision sound (which is the sound attached to the prefab in the audiosource).
Answer by Jessy · Jan 12, 2011 at 12:13 AM
Raycast, and instantiate what you like at RaycastHit.point.
can OnCollisionEnter be invoked when this raycast collides with a collider?
No. I could see how you might think so, because a wheel collider utilizes raycasting, and it works for that, but no.
No, but you'll get the same sort of behaviour if you read my answer in the link I posted.
Answer by Statement · Jan 12, 2011 at 12:32 AM
Check out this similar question. It contain full code in C# to make a RayCollider. With minor modifications you can ask it to test ray when pressing button. You can then have code such as
void OnRayCollision(RayCollision collision)
{
// spawn prefab at collision.hit.point
}
Did you even bother to follow the link or read what I wrote?
That's what you get for giving the men fish after I taught them how to make a rod and raise worms! :-D
Well, I see no big difference. Unity contain code. $$anonymous$$y answer contain code. If unity provide a method to check for collisions and I provide a method to check for collisions, what does it matter? Productivity should matter. But hey, it's nice to see you're describing yourself a $$anonymous$$cher and me a fish dealer.
I don't think you understood what I said. What I'm saying, is that you keep giving more elaborate answers than I do (and in one case edited my answer to be so), and while I think your answers have looked more informative, that doesn't mean the asker is going to appreciate them more. $$anonymous$$y approach is to provide the smallest amount of direction I think would have helped me understand the solution to the question. I hardly know most askers here, so I can't really tailor the results to them, and there are other things I need to do with my time.
Your answer
Follow this Question
Related Questions
Spawn Prefab or FBX model after import? 1 Answer
How can I instantiate two prefabs at different times and positions ? 1 Answer
How to detect a single collision when 2 identical gameobjects collide. 1 Answer
Instantiate Pre-fab clone altered on Instantiate 2 Answers
What is the best way to instantiate inside parent objects? 2 Answers