- Home /
instantiate decal at raycastHit
Hello,
I made a script for a gun that shoots with raycast method. Now I need to Instantiate a plane as a bullet hole effect on the fired position. I tried with raycastHit but I didn't succeed.
Any help?
#pragma strict
var FireSound : AudioClip;
var ReloadSound : AudioClip;
var Muzzle : GameObject;
var MuzzleSpawner : GameObject;
var HoldingWeapon : GameObject;
var Hole : GameObject;
private var attributes : WeaponAttributes;
private var counter : float;
function Awake () {
attributes = HoldingWeapon.GetComponent("WeaponAttributes");
}
function Update () {
counter --;
var fwd = transform.TransformDirection (Vector3.forward);
var newPos = Vector3.forward * 10;
Debug.DrawRay(transform.position, transform.forward * attributes.Range, Color.blue);
if(Input.GetMouseButton(0) && counter <= 0)
{
counter = attributes.RateOfFire;
var muzz = Instantiate(Muzzle, MuzzleSpawner.transform.position, transform.rotation);
muzz.transform.parent = gameObject.transform;
audio.PlayOneShot(FireSound);
if (Physics.Raycast (transform.position, fwd, attributes.Range))
{
print ("We hit something!");
}
}
}
"I tried with raycastHit but I didn't succeed": Where? The script above only prints Hit Something. $$anonymous$$ight be helpful if you ins$$anonymous$$d posted (only?) the part where you try to instantiate the plane at raycastHit.
Also might be helpful if you changed the question title to be more specific. $$anonymous$$aybe "instantiate decal at raycasthit."
That specific Q has also been asked here before, so you might be able to just google and find examples.
I would make it where the ray hited it there you istantiate the new plane for 0.01 float away towards you
OR if you are more skilled you paint the texture there that's where I cannot help you as I'm not there yet, ...
I removed my try from the code 'cause it just made the code harder to understand, it didn't worked at all.
The raycast variable 'point' gives the actual world space point at which the ray hit the collider.
http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit-point.html
Answer by samtperrin · Oct 19, 2013 at 02:10 PM
Use something like:
RaycastHit hit;
if(Physics.Raycast(transform.position, fwd, out rayHit, attributes.Range))
{
Instantiate(texture, hit.point, Quaternion.FromToRotation(Vector3.forward, rayHit.normal));
}
This will Instantiate a texture at the hit point of the Raycast.
Well, almost -- the raycast has to also have the hit
variable in the parens (that's how you tell it "and also please write down extra hit data, in this variable.")
The way it is now, hit.point will always be just 000 (or a "var not assigned" error in C#, which is better.)
You are absolutely correct. Have edited the code. Thanks for pointing that out.
Your answer
Follow this Question
Related Questions
Help With RayCasting 1 Answer
Raycast visible bullets 2 Answers
Unity | Raycast Hit Empty Object which has Objects as Children 1 Answer
Need a script for a gun that shoots bullet using raycast 2 Answers
Vehicle is vibrating on the edge of the ramp/surface when I get the normal of the surface 0 Answers