- Home /
Raycast shooting script
Hi, im having a problem with my raycast shooting script, i have this script set up on an empty gameobject at the barrel of the gun. For some reason the script make it spawn bullet holes in really wierd places, got an idea?
#pragma strict
var bulletHole : GameObject;
function Update () {
var fwd = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
Debug.DrawRay(transform.position, fwd*10, Color.green);
if(Input.GetButtonDown("Fire1") && Physics.Raycast(transform.position, fwd, hit, 10)){
Instantiate(bulletHole) hit.point Quaternion.FromToRotation(Vector3.up, hit.normal);
}
}
This code will not compile. Your Instantiate() line is messed up. If this line was fixed, this code should work, but I have to wonder if we are looking at your real code (and therefore your real problem).
Sorry i posted wrong code, i was looking at someones simular code, my main problem is that bullethole texture wont rotate the right way, it keeps the same rotation if i hit a wall and if i hit a floor
here is my code
#pragma strict
var bulletHole : GameObject;
function Update () {
var fwd = transform.TransformDirection(Vector3.forward)*10;
var hit : RaycastHit;
Debug.DrawRay(transform.position, fwd, Color.green);
var hitRotation = Quaternion.FromToRotation(Vector3.forward, hit.normal);
if(Input.GetButtonDown("Fire1") && Physics.Raycast(transform.position, fwd, hit, 1000)){
Instantiate(bulletHole, hit.point, hitRotation);
}
Destroy(bulletHole, 5);
}
Answer by robertbu · Mar 12, 2014 at 05:15 PM
Wrong place and wrong rotation are two different questions. This part of the code you originally posted takes care of the alignment:
Quaternion.FromToRotation(Vector3.up, hit.normal)
This code assumes the bullet hole was on a plane facing up when the rotation is (0,0,0). If you are using a Quad, then substitute Vector3.back.
You can also do it after the instantiate.
var go = Instantiate(bulletHole, hit.point, Quaternion.identity);
go.transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * go.transform.rotation;
You need to substitute for 'transform.up' whatever side of the object is the face. But the prefab can have an arbitrary rotation, and this code will do the alignment.
Like this?
pragma strict
var bulletHole : GameObject; function Update () { var fwd = transform.TransformDirection(Vector3.forward)*100; var hit : RaycastHit; Debug.DrawRay(transform.position, fwd, Color.green); var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal); var go = Instantiate(bulletHole, hit.point, Quaternion.identity);
if(Input.GetButtonDown("Fire1") && Physics.Raycast(transform.position, fwd, hit, 100)){
Instantiate(bulletHole, hit.point, hitRotation);
go.transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * go.transform.rotation;
}
Destroy(bulletHole, 5);
}
That should work if the natural rotation of your object has the visible side 'up', but there is no reason to pass 'hitRotation' in and then do line 11 as well. Either should work without the other.
im sorry but still it just keeps the "up" position, it does not rotate sideways
What is the natural orientation of your object? That is, if your bullet hole prefab has a rotation of (0,0,0), what side of the object has the image? Typically it is either back (Quad), or up (Plane). That is what you will need to align with the hit.normal. So in your code above if it is the back side, you will use either Vector3.back or -transform.forward depending on what code above you are using for alignment.
also with this script it keeps spawning infinitive with bullethole clones