i’m developing a fps game for my own educational use and entertainment , currently i’m trying to figure out how i’m going to develop the c4 (explosive).
I know how to detonate it or put that in script but my main problem is trying to figure out how to make this object to stick to things such as walls and stuff.
this object is a rigid body, so far i don’t have a code for it yet i’m just planning out the scripting before i lay everything out. i’m trying to learn how to script and improve from it.
one of the ideas would be trying to get the c4 to get parented to the contact normals of the objects. but i what i don’t know is how to parent the c4 once it has touched a contact normal in scripting.
I would assume that if you are building a fps then you would want to use a physics.raycast from the camera to the first wall intersection, then the C4 is put on that wall in the normal of the geometry. (I say this but I don’t really know how to get the normal from the geometry) If you used the normal from the raycast, you will probably not be square on the wall. This would give you the best “fit”. If it were a 3rd person, or something where you are using a mouse, you would use the same theory but from the perspective of the mouse.
Also, when you do this you could also give it a slight, or completely random local y rotation. Then it is never really squared to everything and looks a bit more realistic.
The actual explosion is simple. You broadcast to the device and tell it to explode. So in your code you would store the last C4 placed.
Well, you may try to use a texture, similar to a bullet hole. There are good numbers of scripts that apply bullet hole to static object, such as wall.
I would have it the same as any grenade or arrow style bullet…well more an arrow. Shoot your c4 like a bullet with not very much velocity…More like a toss then a shot. Have it parent it self to whatever it hits.
Here’s a small code example: ( in C#)… I use arrays to send my projectiles info so change what you need
My Array is declared at the top of my class since i use it in multiple functions
public float damage = 20.0f; // give it some damage
public float projectileSpeed = 50.0f; // how fast does the projectile travel
private float[] projectileInfo = new float[2]; // all of the info sent to a fired projectile
inside my fire projectile function i have:
// i have an empty game object set up as a muzzlepoint to spawn projectiles
Vector3 position = muzzlePoint.position; // position to spawn rocket
projectileInfo[0] = damage;
projectileInfo[1] = projectileSpeed;
GameObject newProjectile = Instantiate(YOUR_PROJECTILE_PREFAB, position, transform.parent.rotation) as GameObject;
newProjectile.SendMessageUpwards("SetUp", projectileInfo);
then on my projectile script:
public void SetUp(float[] info)
{
damage = info[0];
speed = info[1];
direction = transform.TransformDirection(0, 0, 1); // well,mine is a bit different here, but this should shoot straight ahead
velocity = speed * transform.forward;
rigidbody.velocity = velocity + direction; // my projectile is a rigidbody and affected by gravity
// schedule for destruction if bullet never hits anything
Destroy(gameObject, lifetime);
}
void OnCollisionEnter(Collision enterObject)
{
ContactPoint contact = enterObject.contacts[0];
Quaternion rotation = gameObject.rigidbody.rotation;
// and to make your c4 stick to whatever it touches i would do this
transform.parent = enterObject.transform; // should parent the transform this script is attached to whatever it collides with
}
then do whatever you want to detonate and what not… I had to edit the code a bit on the fly from mine so hopefully there are no errors but this should work good
thanks for the advice being given later on i will come back with more questions about it
i will do some test on it. hopefully it works. in some weeks i might be able to come out with some videos of my project.