1 Dynamite 2 Telescope 3 SuperJump

Hey everyone. I have a few questions today:
1.) I want to make a game object like a cube or something blow up, the game object is my dynamite. I want me to trigger something (ex. pressing a key, then the dynamite starts a countdown timer and explodes in thirty seconds) which also explodes all game Objects around it. In my game a made a fence with turrets behind it and I want to be able to go get some dynamite, then place it by the fence, press a button, wait thirty seconds and then it and part of the fence explodes.

2.) Also in this game I want to be able to scope out the fence and turrets from very far away, I already have a zoom script on my sniper rifle:

private var baseFOV : float;

function Start () {
    baseFOV = Camera.main.fieldOfView;
}

function Update () {
    if (Input.GetMouseButton(1))
        Camera.main.fieldOfView = 10;
    else 
        Camera.main.fieldOfView = baseFOV;
}

and that happens when I right click, but I want a telescope like object, which when I go near it says “Press “Z” To Zoom in” and then when you press z it zooms in 5-10 times.

3.) I want to make a cube or something and when you step over it, it says “Press “P” to pick up superjump” and if you press “P” then the next time you press spacebar (my jump) you jump say five times higher.

Thanks so much for any answers

–Bruno

PS: if you think this is a good questions please vote up (:

EDIT: please help, if you have any answers or comments or questions any any three please answer… :slight_smile:

You’ll need colliders with a scripr implementing OnTriggerEnter for each question to detect the player’s proximity.

  1. For the visual explosion, check out the detonator package. For the physic explosion, AddExplosionForce. If you want to break objects into pieces, that’s much harder.

  2. Use the trigger for proximity, then input and fieldOfView modifications. If an increment isn’t accurate enough, try an array (10, 25, 60, 90 etc)

  3. Trigger, input and then a var that is used during the jump (probably rigidbody.velocity += (jumpConstForce + jumpBonusForce) * Vector3.up)

Hi, I’m not going to write the code to do anything, but here are a few suggestions on how to achieve what you want:

  1. For the dynamite object. The player creates the dynamite (instantiate it or make it active if you’re pooling) and whatever script you’re using to handle input will call a “StartDetonation()” function inside the dynamite when the player presses whatever button. Inside this function you can do all sorts of things you want to happen when the timer starts but, more important, it calls something like “Invoke(“Detonate”, Timer)”. Invoke calls the detonate() function after the “Timer” amount of time.

And what about the detonate() function? Well, one way to do it: This dynamite object should have a collider trigger with the radius you want it to blast things. Every object that can be exploded should have a function to explode itself, like explodeMe(), or something. So, create a boolean variable like “explode” that detonate() sets to true. Then, if explode is true, call the explodeMe() function on all objects inside the dynamite collider from the ontriggerstay() function inside dynamite. After setting this explode variable, the dynamite should also explode (basically: animations, particles and finally removing the object).

The explodeMe() function can do whatever you want, from just simply smoke-and-vanish to dynamically being pushed in the opposite direction (giving this function a parameter with the dynamite position would do the trick) etc.

You may find working with triggers tricky, though. I certainly do. The functions that handle the intersections of colliders (ontriggerenter, ontriggerstay etc) will ignore objects that are inside the collider if it was already there when the dynamite was instantiated. This is one example of the difficulties. So you may need to think this through a little more.

  1. Again, triggers. This time it’s easier because the telescope is already there and it’s expecting just the player to enter its collider.

A simple, if not a bit crude, way to do this: Telescope has a trigger collider and a function onTriggerStay() that just informs the player (a text on GUI for example) and listens for input on some key. This function is called every frame, not an elegant way to do things. If whatever key is pressed, a function inside the player is called. This function in the player will simply move the camera to some specific position (where the telescope lens is) and the rest you know.

  1. Triggers, triggers and triggers. It’s the same thing as the above.

Seems like everything you want to do revolves around learning triggers. So do that! Here’s a nice article on the subject.