Raycast is not working, oncliking on the one player, the rest of the players also firing bullets, and collision not working too
My player manager file is this ***
public class PlayerManager : MonoBehaviour {
private IndividualPlayerHandler mPHandler;
public GameObject _Player;
// public GameObject[] _arrRefer;
public const int maxHealth = 100;
public int currentHealth = maxHealth;
public RectTransform healthBar;
// private bool destroyOnDeath;
private Vector3[] currPlayerHandler = { new Vector3(-3f, 0.85f, -4f), new Vector3(-3f,0.85f, -1f), new Vector3(-3f, 0.85f, 2f)};
public void Start()
{
mPHandler = GameObject.Find("Player").GetComponent<IndividualPlayerHandler>();
for (int i = 0; i<currPlayerHandler.Length; i++)
{
Instantiate(_Player, currPlayerHandler[i], transform.rotation);
Debug.Log("Player checking here" + _Player);
}
}
//List<IndividualPlayerHandler> _arrIndividualPlayer = new List<IndividualPlayerHandler>();
//private IndividualPlayerHandler mcurrActiveIndividual = null;
//public void OnSelectedPlayer()
//{
// for(int i=0; i<_arrIndividualPlayer.Count; i++)
// {
// mcurrActiveIndividual = ((GameObject)Instantiate(_Player)).GetComponent<IndividualPlayerHandler>();
//}
//}
public void Update()
{
OnMouseDown();
}
public void OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Transform objectHit = hit.transform;
mPHandler.PlayerBulletFire();
Debug.Log("ColliderName" + hit.collider.name);
}
}
}
private void OnBulletCollision(int amount)
{
currentHealth -= amount;
if (currentHealth <= 0 || currentHealth >= 4)
{
currentHealth = 0;
Debug.Log("Dead!");
}
}
}
INDIVIDUAL PLAYER MANAGER *** using System.Collections; using System.Collections.Generic; using UnityEngine;
public class IndividualPlayerHandler : MonoBehaviour {
// public PlayerManager pBullet;
public GameObject bulletPrefab;
public Transform BulletSpawn;
public Transform BulletSpawn1;
public Transform BulletSpawn2;
public Vector3[] BulletSpawningPoints = { new Vector3(-2.85f, 0.85f, -4f), new Vector3(-2.85f, 0.85f, -1f), new Vector3(-2.85f, 0.85f, 2f) };
public void PlayerBulletFire()
{
var bullet = Instantiate(bulletPrefab, BulletSpawn.transform.position, BulletSpawn.transform.rotation);
var bullet1 = Instantiate(bulletPrefab, BulletSpawn1.transform.position, BulletSpawn1.transform.rotation);
//var bullet2 = Instantiate(bulletPrefab, BulletSpawn2.transform.position, BulletSpawn2.transform.rotation);
// Add velocity to the bullet
bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 6;
bullet1.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 6;
// bullet2.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 6;
}
// public void PlayerBulletFire()
// {
// //for (int i = 0; i<BulletSpawningPoints.Length; i++)
// {
// var bullet = (Instantiate(bulletPrefab, BulletSpawn.transform.position, BulletSpawn.transform.rotation));
// bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 6;
// var bullet1 = Instantiate(bulletPrefab, BulletSpawn1.transform.position, BulletSpawn1.transform.rotation);
// bullet1.GetComponent<Rigidbody>().velocity = bullet1.transform.forward * 6;
// }
// }
// //if (gameObject.tag == "BulletSpawn1")
// //{
// //}
// }
// // var bullet2 = Instantiate(bulletPrefab, BulletSpawn2.transform.position, BulletSpawn2.transform.rotation);
// //// Add velocity to the bullet
// //bullet2.GetComponent<Rigidbody>().velocity = bullet2.transform.forward * 6;
//}
} ,
Answer by DrkProdigy09 · Mar 27, 2017 at 02:36 PM
Not sure about your issue with all of the players firing, but with your collision detection, have you made sure that both GameObjects have colliders and at least one of them has a rigidbody? Otherwise they won't detect the collision.
Well, thanks for your response.. Am completely new to unity, the problem is, for players i have used Raycast method so that they should fire the bullet on clicking on them individually, whereas it is firing the bullets from every player... And collision part, even though if i add Rigidbody to wither player or enemy it is not working.. collider is there
Your answer
![](https://koobas.hobune.stream/wayback/20220612114046im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
RayCast not working , why ??? 0 Answers
RayCast not working with MeshCollider - Unity's roller ball collision works 1 Answer
Raycast ignores objects with colliders 1 Answer
raycast or collision? 1 Answer
What is wrong with the code I have written for the camera to avoid it from going through object 1 Answer