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 /
This question was closed Sep 02, 2014 at 12:48 AM by Domo23000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Domo23000 · Aug 27, 2014 at 04:23 AM · collisiongameobjectlistclasstags

Collisions and Lists

I have a list of gameobjects, which are all guns with tags for each. i want to ask if the player is colliding with any of them, and then equip them. I just cant figure out the collision part.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Weapons : MonoBehaviour 
 {
     public List<Weapon>weapons = new List<Weapon>();
 
     [System.Serializable]
     public class Weapon
     {
         public GameObject Item;
         public GameObject Hold;
     }
 
     public GameObject current1 = null;
     public GameObject current2 = null;
 
     void OnCollisionEnter(Collision other)
     {
 
     }
 }
Comment
Add comment · Show 3
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 robertbu · Aug 27, 2014 at 04:46 AM 0
Share

You are going to have to sort through some possibilities and make some decisions before we can give you accurate advice:

  • If you player uses a CharacterController, he will not generate collision events. For a CC, you must use the OnControllerColliderHit() callback in a script on the same game object as the CC.

  • For Collisions to be detected, both objects need colliders and one of the two objects must have a Rigidbody component.

  • There is no way for third-party object to detect a collision. The only way is for a script with OnCollisionEnter() or OnCollisionStay() on the game object what will experience the collision.

  • If your setup allows for the collisions with the guns, you can handle it one of two ways. You can have the weapons sense when they hit the player and do whatever, or you can have the player sense when he hits a weapon and tell the weapon to do whatever.

  • There are functions like Physics.OverlapSphere() that might be good enough in telling you when there is a Player/Weapon intersection.

  • Depending on the geometry, it may be possible for either the weapon or the player to raycast to detect closeness or intersection.

avatar image Domo23000 · Aug 27, 2014 at 10:16 PM 0
Share
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Weapons : $$anonymous$$onoBehaviour 
 {
     public List<Weapon>weapons = new List<Weapon>();
 
     [System.Serializable]
     public class Weapon
     {
         public GameObject Item;
         public GameObject Hold;
     }
 
     public float pickupDistance = 5f;
 
     public GameObject current1 = null;
     public GameObject current2 = null;
 
     void Update()
     {
         Ray ray = Camera.main.ScreenPointToRay(Vector3.forward);
         RaycastHit hit;
 
         if(Physics.Raycast(ray, out hit, pickupDistance))
         {
             for(int i = 0; i < weapons.Count; i ++)
             {
                 if(hit.collider.tag == weapons[i].Item.tag)
                 {    
                     if(current1 == null)
                     {
                         current1 = weapons[i].Hold;
                     }
                     else
                     if(current2 == null)
                     {
                         current2 = weapons[i].Hold;
                     }
                 }
             }
         }
 
         Debug.DrawRay(ray.origin, ray.direction * pickupDistance, Color.yellow);
     }

avatar image Domo23000 · Aug 27, 2014 at 10:19 PM 0
Share

The player uses a rigidbody and a capsule collider, but raycasting would be a better path to take. Also this script above does not work. I think its because the list, but ill try a debug

1 Reply

  • Sort: 
avatar image
0

Answer by 767_2 · Aug 27, 2014 at 05:35 AM

you can give your weapon game objects a tag like weapon and check if player hits it

 void OnCollisionEnter(Collision other)
     {
         if(other.collider.tag=="weapon")
             currentweapon=other.collider.gameObject; //swap weapon
         
     }


hope this helps

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

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

missingGameObject problem 3 Answers

Change gameobject tag when player collides with another gameobject 1 Answer

Using parent tag on whole gameObject 1 Answer

Multi object detection with single ray cast 1 Answer


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