- Home /
The question is answered, right answer was accepted
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)
{
}
}
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.
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);
}
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
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
Follow this Question
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