Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Nov 16, 2017 at 04:07 PM by Crightub for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Crightub · Nov 15, 2017 at 05:47 PM · unity 5raycastunity 2dprogrammingraycasthit2d

Raycasthit2d isnt working.

I'm trying to programm multiplayer 2d game with a shoot function. you can see my shoot script below, but i cant hit something except of my own player. If i select that im also allowed to shoot myslef its working (LayerMask).

However, this line of code -Debug.Log ("Shot: " + _hit.collider.name);- isnt working if i hit the ground;

 using UnityEngine.Networking;
 using UnityEngine;
 
 public class PlayerShoot : NetworkBehaviour {
 
     private const string LOCAL_PLAYER_LAYER = "LocalPlayer";
 
     public PlayerWeapon weapon;
 
     [SerializeField]
     private Transform player;
 
     [SerializeField]
     private LayerMask mask;
 
     void Start() {
 
 
     }
 
     void Update(){
         if(Input.GetKeyDown(KeyCode.F)){
             Shoot ();
         }
     }
         
     [Client]
     public void Shoot(){
         
         RaycastHit2D _hit = Physics2D.Raycast(player.position, player.forward, weapon.range, mask);
         if (_hit.collider != null)
         {
             if (_hit.collider.tag == "Player")
             {
                 //Debug.Log ("We hit " + _hit.collider.name);
                 CmdPlayerShot(_hit.collider.name);
             }
 
             // We hit something, call the OnHit method on the server
             //CmdOnHit(_hit.point, _hit.normal);
             Debug.Log ("Shot: " + _hit.collider.name);
         }
     }
 
     [Command]
     void CmdPlayerShot(string _ID) {
         Debug.Log (_ID + " has been shot.");
     }
 

If an other player is joining he's on the RemotePlayer Layer.

alt text

unbenannt.png (130.2 kB)
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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Crightub · Nov 16, 2017 at 04:07 PM

Fixed it. i used player.forward for the dircetion, but i a 2d game i shoot the raycast INTO the game. So it hits nothing. You have to use player.right or player.up.

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
0

Answer by smkmth · Nov 15, 2017 at 06:07 PM

dont know for sure, literally just guessing here - but i feel you need a hit.collider.gameObject.name to return the game objects name and not the attached collider's name - like i said, litearlly just a guess, i am new at this.

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 Crightub · Nov 15, 2017 at 06:30 PM 0
Share

Nope. Isn't working. Line 31 isn't working, becuase if put a Debug.Log in the if function i wont be triggered. so _hit.collider is always null;

Follow this Question

Answers Answers and Comments

172 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 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

Multiple Cars not working 1 Answer

Raycasting2D to Compare Tag of GameObject 1 Answer

Raycast2D not returning any transform instead keeps returning void. 1 Answer

How to get co-ordinates of end point of Raycast2D ? 1 Answer

Raycast2D/Line Renderer offset on colliders based on mouse position 0 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