I want the gun to simulate physics but not affect my character controller. I also want the player to be prompted to pick up the gun when the character controller collides with it.
I have tried putting the player and guns on different layers, letting them both collide with the default layer but not with each other. The problem is that this disables collision detection all together and makes it so I can’t detect if the player can pick up the gun.
Any help appreciated as to how I should approach this, I am very new to unity. Thanks!
You’re on the right track, here. You should create a seperate trigger volume that ‘floats’ above the physics-controlled gun prefab, and manages the ‘pick-up’ behaviour, but doesn’t affect physics. Don’t make it a transform child of the gun, exactly, but make it stick to the gun’s position using
transform.position = gun.transform.position
every frame (in FixedUpdate). Then, when the player touches it and the correct conditions are met, do something like this
Usually you should use a trigger to pick up things: create a cube, adjust its dimensions to the volume you want, set Is Trigger in the Collider component, child the weapon to it, then disable the Mesh Renderer component to make the trigger invisible. When the player touches the trigger volume, a OnTriggerEnter event is called in both, the player and the trigger scripts. You can destroy the picked object using the trigger script, and enable the picked weapon in the player script:
// in the trigger script:
function OnTriggerEnter(col: Collider){
if (col.tag == "Player"){
Destroy(gameObject); // destroy the picked item
}
}
// in the player script:
function OnTriggerEnter(col: Collider){
if (col.name == "PickMachineGun"){
// enable weapon "machine gun"
}
else
if (col.name == "PickRocketLauncher"){
// enable weapon "rocket launcher"
}
...
}