- Home /
2D Getting Arrow to stick to what it has collided with
I'm trying to make it so that when my arrow gameobject collides with something - it stops moving (so it looks like it's sticking out from the ground, wall, or any enemy).
Right now it stops velocity, but unless I shoot it down it just rotates in place without moving. Would anyone be able to help me?
Here's my collision function on my arrows:
void OnCollisionEnter2D(Collision2D coll) {
fired = false;
GetComponent<Rigidbody2D> ().velocity = new Vector2(0,0);
GetComponent<Rigidbody2D>().isKinematic = true;
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY;
}
Also, should I attach the arrow as a child to whatever it hits? Would that make it move with whatever it hits? Thanks in advance!
Answer by wornTunic · Sep 20, 2017 at 12:47 PM
Maybe change it to:
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
This freezes motion and rotation for all axes.
Edit: Didn't see the second part of your question. Yes, you should do something like:
this.gameObject.transform.parent = coll.gameObject.transform;
in order to make it move with the thing it hit.
Adjusting the rigidbody restraints still results in the arrow rotating in place - but thank you for child code!
@impojr Just noticed this, but it might be that there isn't a good way to translate constraints to rigidbody2D from general RigidbodyConstraints. So, you can try this:
GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeAll;
I always forget about adding the 2D! It works perfectly now, thank you! :)
Your answer
Follow this Question
Related Questions
How to stop projectile collider (is trigger) from colliding with other colliders (also is trigger) 2 Answers
The tilemap collider is simply not working 0 Answers
Object hits another but doesnt bounce off again. 0 Answers
Is there a way for 2D Box Colliders to ignore another? 4 Answers
Dropping down from 2D platforms: Character gets "sprung back" if collision resumes mid-collider 1 Answer