- Home /
Copy/Clone collider to other gameobject at runtime
I have two Rigid bodies a firearm and a mag. When the mag is attached I do this
public void AttachMagazine (Magazine magazine)
{
this.Magazine = magazine;
this.Magazine.CanInteract = false;
this.Magazine.Rigidbody.isKinematic = true;
this.Magazine.Rigidbody.useGravity = false;
this.Magazine.Rigidbody.detectCollisions = false;
PlayClip (MagazineIn);
}
I also update the position of the mag each frame so that it appears to be attached to the firearm. Only problem is that firearm cant go through walls but the attached magazine can. If i dont set this.Magazine.Rigidbody.isKinematic and this.Magazine.Rigidbody.detectCollisions it will go complete crazy.
So my idea is somehow to clone the colliders from the magazine to the firearm so that it firearm get teh correct shape with the attached magazine., But cant firere out how todo it, any ideas?
Answer by JoshDangIt · May 23, 2016 at 09:38 PM
How to copy components: http://answers.unity3d.com/questions/458207/copy-a-component-at-runtime.html
Also, if you're making an FPS you probably shouldn't have a collider and rigidbody on the gun. Just put a capsule collider over the player and the gun to prevent it from going through walls.
Saw that, but there must be a more built in way than using reflection? It's a VR game so physics is alot more demanding
Unfortunately I don't think there is. I was doing research into this recently and I haven't found a better way. If you do make sure to post it here.
2020 and still no solution to copy a component. I just wonder why because you can do it from the inspector so why can't you just call the same method at runtime ? ¯_(ツ)_/¯