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
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][1] which does this. It is not very well coded but you can learn from it and it will likely work for something spere shaped. [1]: http://wiki.unity3d.com/index.php/DontGoThroughThings
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!
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).
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
– AlucardJaythanks ,, but still the same when i shoot terrain some hit show the dirt particle and other just go throw the terrain
– D3m0nEFrom 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][1] which does this. It is not very well coded but you can learn from it and it will likely work for something spere shaped. [1]: http://wiki.unity3d.com/index.php/DontGoThroughThings
– Lockstep