• 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 /
  • Help Room /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Chaithra · Mar 27, 2017 at 09:42 AM · collisionraycast

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;
 //}


} ,

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · 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 Chaithra · Mar 28, 2017 at 04:13 AM 0
Share

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

148 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast not detecting hit 4 Answers

Raycast not consistently detecting collision. 1 Answer

Ray casting collision detection, not working for moving right. 0 Answers

Raycasting not detecting collider 0 Answers

Raycast on moving object? 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