- Home /
,How do I make projectile weapons stick to enemies or targets?
Hi there. Im new to Unity and I could use some help. I would like my projectile to hit and stick to its target. The target is moving, and I would like the projectile to stick into. Sort of like an arrow sticking into a target or a shuriken sticking into a target.
Answer by calpolican · Jan 16, 2018 at 12:16 AM
Well, the best way to do this is probably parenting the object. imagine you have the following elements:
Transform myProjectile; //your sticky projectile
Transform target; //the character or thing to wich the projectile is gonna stick.
once you know that the projectile collided with something you can do:
miProjectileObject.transform.parent = targetedObject;
Now the projectile will follow the position and rotation of the parent object!
You probably want to do it whenever the projectile touches your object:
Collider yourCollider;
OnCollisionEnter(Collision collision) //whenver this object collides
{
this.parent = collision.transform; // make the object collision object it's parent
YourCollider.enabled = false; //disable your collider, otherwise it may stick to something else if it touches it.
//depending on how you're animating it you may one to disable the rigidbody too to freeze it in the position.
}
I've tried this but perhaps I'm still uncertain which order to write the code in. Could you walk me through the code step by step? Thank you so much and I'm sorry if I'm being dumb.
Oh, no worries. It would be useful to know how you're trhowing your projectile. You have a guy with a collider, right? and you have a projectile with a collider, ok? You want to know when both colliders are colliding. To know this you use the function "OnCollisionEnter()", that function will run whenever a collision takes place. You also pass a parameter (Collision collision), the function will store there the collision information. You'll be able to know what object collided and several other things.
So you put in your projectile a script that has the function:
OnCollisionEnter(Collision collision){}
Now once the knife hits another collider, either it's a wall, a player, or a moving car, you want it to stick to it. To do this you have to do two things inside the OnColllisionEnter function: 1)freeze the projectile (stop any animations), 2)and parent the projectile to the colliding object. +You have to freeze it (1) because now it's stuck in the surface. You want it to stick in the position it had when it hit the surface. +You have to parent it (2) because you want it to inherit the movement form the object it's stuck into. Parenting is just like puting a file in a Windows folder: if you move the folder, the file with move along. The knife will inherit any of your character's movements, it will be stuck to it.
Now, to freeze the projectile I'd have to know what method you're using to animate it. Say that you're using a rigidbody. You'd then use something like:
Rigidbody myProjectileRigidbody = this.GetComponent<RigidBody>(); //to store the rigidbody of you projectile inside a variable
myProjectileRigidbody.velocity = new Vector3.zero; //so that the rigidbody has no velocity (stops)
myProjectileRigidbody.is$$anonymous$$inematic = true //that basically disables the Rigidbody alltogether.
Now, to parent the projectile with your object:
this.transform.parent = colision.transform; //looks for your collider object and sets it as the knife parent. So basically now the projectile will be moved by the object (stucked).
Here you close the OnCollisionEnter() function with the closing bracket.
A NOTE: $$anonymous$$eep in $$anonymous$$d that to the world of the game your object is not your model but your collider. The aftermentioned method should work well, BUT, the knife will stuck to your collider, not to your model. The object could never stick to the game object. you can try to stick it to a bone or other hacks, but that's way more advance. If you throw it to a car it will work very well since the collider can be similar to the car shape, but if you throw it to a character and the collider is a capsule collider, the knife will stick to the capsule collider, while the character may have some idle, walking or running animation. To overcome this you can place your collider pretty tight following your model torso and head. Still it won't be perefect. To make it perfect it will require a whole bunch of extra expanations, but I hope this sends you in the right direction.
Answer by RedDeCipher · Jan 16, 2018 at 12:03 AM
private Vector3 refPos;
private GameObject target;
private bool sticked = false;
void OnCollisionEnter(Collision collision)
{
ContactPoint contact = collision.contacts[0];
target = collision.gameObject;
Vector3 refPos = contact.point - target.transform.position;
sticked = true;
//.....Other stuff you wana do when collided
}
void Update(){
//... Other Update Stuff
if(sticked)
transform.position = target.transform.position + refPos;
}
This works although the arrow doesn't stick to where it hits, it automatically places the arrow in the middle of the target, it also increases the speed once the arrow hits the moving target and carries it away twice as fast.