- Home /
How can i instantiate a mesh at a hit point?
Very new to both scripting and unity and I can't figure out how to spawn something as simple as a sphere at a location, even though the script I'm using manages to spawn particles there. Here's the script that spawns the particles:
function FireOneShot () { var direction = transform.TransformDirection(Vector3.forward); var hit : RaycastHit; // Did we hit anything? if (Physics.Raycast (transform.position, direction, hit, range)) { // Apply a force to the rigidbody we hit if (hit.rigidbody) hit.rigidbody.AddForceAtPosition(force * direction, hit.point); // Place the particle system for spawing out of place where we hit the surface! // And spawn a couple of particles if (hitParticles) { hitParticles.transform.position = hit.point; hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal); hitParticles.Emit(); }
// Send a damage message to the hit object hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver); } bulletsLeft--;
// Register that we shot this frame, // so that the LateUpdate function enabled the muzzleflash renderer for one frame m_LastFrameShot = Time.frameCount; enabled = true; // Reload gun in reload Time if (bulletsLeft == 0) Reload(); }
I suppose I should be able to get the coordinates from the hit.point, but can't seem to figure out how. I want to spawn an object at the same location as the particles.
Thanks in advance! edit: the script i quoted completely changed from what the preview was, sorry.
Select your post 2. Edit your post. 3. Delete what's there (garbage by now) 4. Hit the 010/101 button 5. Paste
Answer by DaveA · Oct 19, 2012 at 10:16 PM
Instantiate (mySphere, hit.point, Quaternion.identity);
Your answer
Follow this Question
Related Questions
2D enemy movement to target 1 Answer
Code based 3d multiple cube instantiation 1 Answer
Enemy spawn then select target? 1 Answer
Spawned Unit Targeting 3 Answers
Instantiate over time? 1 Answer