Search Unity

Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Enemy

Discussion in 'Scripting' started by cruising, Oct 9, 2019.

  1. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    312
    Hello!

    I trying to make only objects that has layer "Enemy" to be raycasted by the mouse to add targets to aim at.
    Everything seams to work fine except from that all objects with a collider gets raycasted even if they dont have layer "Enemy"

    What have i done wrong?

    CODE
    Code (csharp):
    1.  
    2. namespace TurretDemo
    3. {
    4.    public class TurretTester : MonoBehaviour
    5.    {
    6.         public TurretRotation[] turret;
    7.         public Vector3 targetPos;
    8.         public Transform targetTransform;
    9.         public InputField enemyName;
    10.  
    11.         [Space]
    12.         public bool turretsIdle = false;
    13.  
    14.         [SerializeField]
    15.         private LayerMask Enemy;
    16.  
    17.         public void Shoot()
    18.         {
    19.             RaycastHit hit;
    20.             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit,Enemy))
    21.             {
    22.                 Debug.Log(hit.transform.name);
    23.                 targetTransform = hit.transform;
    24.                 enemyName.text = hit.transform.name;
    25.             }
    26.         }
    27.    
    28.     private void Update()
    29.         {
    30.  
    31.             if (Input.GetMouseButtonDown(0))
    32.             {
    33.                 Shoot();
    34.             }
    35.             // Toggle turret idle.
    36.             if (Input.GetKeyDown(KeyCode.E))
    37.                 turretsIdle = !turretsIdle;
    38.  
    39.             // When a transform is assigned, pass that to the turret. If not,
    40.             // just pass in whatever this is looking at.
    41.             targetPos = transform.TransformPoint(Vector3.forward * 200.0f);
    42.             foreach (TurretRotation tur in turret)
    43.             {
    44.                 if (targetTransform == null)
    45.                 tur.SetAimpoint(targetPos);
    46.                 else
    47.                 tur.SetAimpoint(targetTransform.position);
    48.  
    49.                 tur.SetIdle(turretsIdle);
    50.             }
    51.          }
    52.  
    53.             public void IdleWeapons()
    54.             {
    55.                 Input.GetKeyDown(KeyCode.E);
    56.             }
    57.  
    58.             public void EnemyClicked()
    59.             {
    60.                 Debug.Log("Hello:");
    61.             }
    62.  
    63.             private void OnDrawGizmos()
    64.             {
    65.                 Gizmos.DrawWireSphere(targetPos, 1.0f);
    66.             }
    67.         }
    68.     }
    69.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    7,338
    Can you show a screenshot of the inspector (and what the "Enemy" LayerMask looks like there)?
     
  3. Zei_SSB

    Zei_SSB

    Joined:
    Apr 7, 2019
    Posts:
    7
    Enemy is just the declaration of the LayerMask - you're not assigning any value to it. Make sure in your inspector you're actually assigning the "Enemy" layer or you can do
    Code (CSHARP):
    1.  
    2. private LayerMask Enemy = LayerMask.GetMask("Enemy");
    3.  
    to assign it directly in code.

    Per here:
    https://docs.unity3d.com/ScriptReference/LayerMask.html
     
  4. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    312
    Thats just a list of layers that you can "check" for what you are able to click on.

    With the layermask you can shose what layer you are able to raycast whith the mouse pointer as i guess you know, so what ever layer i "check" in the list should only be raycasted.
    But it does raycast every collider even if i have checked "Enemy" in the inspector and given the enemy object with a collider the layer "enemy".
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    7,338
    Yes, I know, I'm asking for a screenshot of yours to confirm that its value is properly set up.
     
  6. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    312
    Everthing is fine in the inspector, and the ship you see has "enemy" as layer
    http://imgbox.com/CEs17EKL
    Looks like i cant link images so it shows in the forums -_-

     
    Last edited: Oct 9, 2019
unityunity