- Home /
Problem With Bullet Prefab
Hello Everyone I Got Problem, that my Bullet Ignore The Gameobject and go throw thim, without any effect
Shooting Code :
if(Input.GetButton("Fire1") && Time.time > nextFire ){
nextFire = Time.time + CurrentWeapon.firerate;
Rigidbody clone;
clone = Instantiate(CurrentWeapon.Bullet, CurrentWeapon.SpawnPoint.position, CurrentWeapon.SpawnPoint.rotation) as Rigidbody;
bulletprefab = clone.GetComponent<Bullet>();
bulletprefab.Range = CurrentWeapon.Range;
clone.velocity = transform.TransformDirection(Vector3.forward * CurrentWeapon.BulletSpeed );
}
And in Bullet Prefab i have this Code :
//In Update
Vector3 Directoion = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position,Directoion,out hit,Range * 100)){
if(hit.rigidbody){
hit.rigidbody.AddForceAtPosition(transform.TransformDirection(Vector3.forward) * Range,hit.point);
}
//OnTriggerEnter(Collider Col){
Vector3 hitUpDir = hit.normal;
Vector3 hitPoint = hit.point + hit.normal * -0.02f;
try{
switch(Col.tag)
{
case "terrain":
Destroy(myTransform.gameObject);
networkView.RPC("DirtParticle",RPCMode.All,hitPoint,(Quaternion.FromToRotation(Vector3.up,hitUpDir)));
break;
Now When i Shoot the Terrain, sometimes it work and show up the DirtParticle and many other time the bullet just go throw the terrain without any effect
There is a chance that the bullet is moving too fast for the collision to be entered. Try setting the rigidbody Collision Detection setting to Continuous Dynamic
thanks ,, but still the same
when i shoot terrain some hit show the dirt particle and other just go throw the terrain
From my experience the collision detection doesn't help at all.
You could solve this problem by brute force. Use Physics.Linecast ever fixed frame to check if something was between the new and the old position of your bullet. This might impact performance if you have many bullets. There is even a script on the wiki which does this. It is not very well coded but you can learn from it and it will likely work for something spere shaped.
Answer by Raydaq · Apr 20, 2013 at 02:20 AM
This is probably due to the speed of the bullet as alucardj mentioned.
You could try implementing this script from the wiki (I haven't tried it myself but others have had success with it) DontGoThroughThings Script
Answer by CG-DJ · Apr 20, 2013 at 07:29 AM
You could also use raycasts to simulate bullets. A raycast is a line spawned from a point and shot out in one direction until it hits something. When it hits something, you can use that data to do all sorts of things!
//The place where the bullet spawns from, probably an empty
var spawnPoint : Transform;
//If you want your want your bullet to have a maximum distance
var maxDist : float;
//Your particles for dust
var dust : GameObject;
//Your particles for blood
var blood : GameObject;
function Update()
{
if(Input.GetButton("Fire1") && Time.time > nextFire )
{
nextFire = Time.time + CurrentWeapon.firerate;
//variable to store RaycastHit info
var raycastHitInfo : RaycastHit;
//variable to store which direction is forward
var fwd : Vector3;
fwd = spawnPoint.transform.TransformDirection(Vector3.forward);
//Make the raycast
if(Physics.Raycast(spawnPoint, fwd, raycastHitInfo, maxDist)
{
//In here is where you put what you want to bullet to do
//If it hits your terrain, then it will instantiate dust
if(raycastHitInfo.collider.name == "terrain")
{
Instantiate(dust, raycastHitInfo.transform.position, raycastHitInfo.transform.rotation);
}
//Or, if you bullet hits an enemy
if(raycastHitInfo.collider.tag == "enemy")
{
// prepare rotation to instantiate blood or sparks
var rot = Quaternion.FromToRotation(Vector3.up, raycastHitInfo.normal);
Instantiate(blood, raycastHitInfo.transform.position, rot);
}
}
}
}
Here are the wiki links too! http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit.html
I really hope this helps! I worked hard on the code above. Plus me up if it helps!
Raycasts are a good way good doing this as well, however, you cant really have guns that fire bullets at different velocities. (the bullet will be instant).
thank u, but i dont need that
because i want a bullet prefab to show up too
and when that bullet prefab hit the terrain, the Dust show up
Your answer
Follow this Question
Related Questions
How to Sync bullet spawn with shoot animation 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
bullet damage problem 1 Answer
Bullets Shot Stay Still 1 Answer