Alright, I've tried everything. Many times when I fire, it spawns the bullet but just falls right to the floor. This is the only time it doesn't pass through the walls. All the other times it fires in some random direction, passing right through box colliders. So its a bit frustrating, no matter what I look up, it doesn't work, so I'm just going to ask for someone to help me out. May I have a script that works with raycasting? So all it does is fire a ray from an gameobject's position, and instantiate the bullet so It looks like bullet shells flying through the air. Here is my best script so far, though it still was a complete failure. Code (csharp): var projectile : Transform; var bulletSpeed : float = 20; var shootForce : float = 20; function Update () { if (Input.GetButtonDown("Fire1")) { var clone : Transform; clone = Instantiate(projectile, transform.position, transform.rotation); projectile.rigidbody.AddForce(new Vector3(0,20000,0)); } } So there doesn't even need to be a bullet flying, also, how would I make it like an automatic weapon? Sorry for the demands, I've just gotten so frustrated with this I feel like punching a wall, but basic reasoning and logic tell me not to. Most of all, how do I make a decal? I don't really get how. Thanks,
did you try this: Code (csharp): var projectile : Rigidbody; function Update () { if (Input.GetButtonDown("Fire1")) { var clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody; clone.AddForce(0,20,0); } }
Great .. But I hope someone tell me what is the reason of using keyword as in previous line before Rigidbody ?
It stills goes about a foot in front of the weapon, then falls. I put mass at .1 and nothing changes, why?
The projectile variable is typed as a Rigidbody. The Instantiate method only returns Object typed objects which you must then cast yourself. Since we know that the type of "projectile" is Rigidbody, we can safely assume that whatever is instantiated would also be able to be cast to Rigidbody. The "as Rigidbody" merely tells the compiler that it can treat the result object as a Rigidbody.
you could have kept your original code, the fix actually came from the second line... you had.. Code (csharp): clone = Instantiate(projectile, transform.position, transform.rotation); projectile.rigidbody.AddForce(new Vector3(0,20000,0)); The problem with this code, is you are adding force to your prefab, not to the new bullet you just created... Code (csharp): clone = Instantiate(projectile, transform.position, transform.rotation); clone.rigidbody.AddForce(new Vector3(0,20000,0)); The above change means it will apply the force to your new bullet, not the one you are making copies of.
Just in case you get this: In Unity you can use Raycasts which start from one point and travel in one direction until it collides with something it's not ignoring. Then you just have to check if the raycast hit an enemy, and if so, subtract from it's health.
This. Also, the force is added on the Y axis(up), i think you probably want the Z axis(forward). And make AddForce into AddRelativeForce so it wont be in relation to global space but in local space. Unless this is what you want, but for a gun thats what I think. Finally, unless your firing projectiles in the likes of missiles, mortar bombs, etc. you probably wont even see the object, maybe for a frame or two, real bullet are tiny and fast af. Unless you can actually see the bullets just use a line renderer as a trace for the path