Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by insomain · Nov 27, 2011 at 03:08 AM · collisionphysicstriggerguncharacter controller

Gun collision problem

I am trying to create a gun prefab.

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by syclamoth · Nov 27, 2011 at 03:30 AM

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

 player.GiveGun(gun);
 Destroy(gun.gameObject);
 Destroy(gameObject);

to clean up the trigger and the associated rigidbody.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by aldonaletto · Nov 27, 2011 at 03:33 AM

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" } ... }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Nov 27, 2011 at 03:43 AM 0
Share

Problem here is that @insomain needs the gun to have physical behaviour, as well. But I guess my answer covers that part of it.

avatar image aldonaletto · Nov 27, 2011 at 03:55 AM 0
Share

I just didn't understand what he intended with this physics thing, thus I ignored it and showed the conventional stuff - but your answer is a good solution for a rigidbody weapon: the weapon can fall to the ground, bounce etc. and still be picked by the player.

avatar image
0

Answer by insomain · Nov 27, 2011 at 03:58 AM

Thank you guys very much, it works perfectly!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

CharacterController jumps when colliding with kinematic rigidbodies 1 Answer

How to design an Archer Board Scoring System 1 Answer

Trigger to some, Collider to others 1 Answer

Script calling onCollisionEnter2D and onTriggerEnter2D while disabled 1 Answer

Optimize hundreds of objects 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges