So I used the Unity FPS (First person shooter) tutorial to make a grenade launcher, and I want to know 2 things: (1) How to set the grenade launch to the left mousebutton, and (2) How to make the grenades stick to any object that it collides with. (and then explode in .50 seconds after collision.
if (Input.GetMouseButtonDown(0)) {
// Launch grenade.
}
You could make a sticky grenade effect by attaching a joint between the grenade and the target at the point of contact, or by making the grenade a child of the target. The advantage of the joint is that you could arrange for it to have a bit of springiness so it would vibrate slightly after the collision. Either way, you would add an OnCollisionEnter function to the grenade’s script. The Collision object passed to this function has a list of contact points between the two colliders. You could add a joint at a contact point to stick the grenade to the target.
Oops, I meant to say right mouse button… So I changed the (0)) to (1)). It still did not work (it did not work with the original script either. here is the script that I have for my first launcher:
I want the new script to have the exact same action as the scipt above,(with the sticky script of course) but is executed with the RIGHT mouse button. Where would I put the new script? I even played around with it a little and it still did not help. Actually, one time I got it to launch, but I could not control it, and it kept launching automatically on it’s own without my input.
In Unity meny, go to Edit / Project settings / Input and define which key or mouse button should be connected to each Input type. Make sure there is one called Fire1 and connect it to the right mouse button.
Ok, I have gotten the right-click for grenade to work, but I still do not understand how to make it stick to an object after being launched. Do I have to have pre-determined targets first?
Do you only want it to stick to certain types of objects? If not…
function OnCollisionEnter () {
rigidbody.isKinematic = true;
}
this makes the rigidbody Kinematic if it collides with anything. Kinematic rigidbodys aren’t affected by forces or other rigidbodys, making it lose all velocity immediately.
I have also been trying to get a sticky projectile working, but with only partial success. Can anyone get this code functioning?
I have tried out many different scripts using OnCollisionEnter, Fixed joints and contact points, but most of them will only cause the projectile to stick to the world, not to the object collided with (i.e. moving enemies). I am comparatively noob at scripting, and would appreciate if anyone can point out some errors or even get the code working.
This is the latest script I wrote – trial and error just trying to get the fixed joint to stick to the contact point object. Just like all the others, the fixed joint sticks to the world. The first part is ofcourse projectile life expectancy.
var timeOut = 5;
function Start ()
{
Destroy(gameObject,timeOut);
}
function OnCollisionEnter (collision : Collision)
{
for (var contact : ContactPoint in collision.contacts)
{
var fixedJoint = gameObject.AddComponent(FixedJoint);
anchor = contact;
connectedBody = collision.collider.rigidbody;
}
}
An easy way to test the script out is open up the FPS tutorial and put the script onto the Rocket prefab.
i’m not really a good coder i’m a newbie too but if you look at the fps tutorial by when the grenades explode the explosion is using the normals of the level why not try sticking to the normals of any object insead?? like try sticking your grenades into the wall and stuff. i got an idea bt i won’t be able to show you in code but you could try reverse engeneering the code for explosion from the fps tutorial.
That’s not a question. That’s a … … declaration of intent with a question mark pasted on the end.
Do you mean, “How do I add a Rigidbody component to an object?” - click the Component menu, then Physics, then Rigidbody.
If you mean, “How do I add a Rigidbody component to an object in script?” - gameObject.AddComponent( Rigidbody ).
Don’t forget to go through the official tutorials (especially Lerpz); they’ll teach you all of this stuff.