Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Raycast in 2d not working

Discussion in 'Scripting' started by that_me, Feb 9, 2023.

  1. that_me

    that_me

    Joined:
    Apr 2, 2022
    Posts:
    23
    I'm working on a game where a enemy shoots at the player and this is the enemy script. When I click there goes a timer and the animation starts, after 2,5 sec the raycast gets shot. But when the Raycast happens the line is working the right way but at "Debug.Log(hitInfo.transform.tag);" the output is "Map"(even when it hits the player) which is the tag of the map, but the output should be "Player" when it hits the player, or when missed it should be "Missed" what is wrong? pls help me.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. public class Enemy: MonoBehaviour
    7. {
    8.     [SerializeField] Transform firePoint;
    9.     [SerializeField] GameObject player;
    10.     [SerializeField]Vector3 playerPos;
    11.     [SerializeField] LineRenderer lineRenderer;
    12.     float timer;
    13.     Animator animator;
    14.     float distance;
    15.     [SerializeField]bool hasClicked;
    16.  
    17.     void Start()
    18.     {
    19.         animator = GetComponent<Animator>();
    20.         timer = 2.5f;
    21.         player = GameObject.FindGameObjectWithTag("Player");
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.         if (Input.GetKeyDown(KeyCode.Mouse0)){
    27.             hasClicked = true;
    28.             playerPos= player.transform.position;
    29.             animator.SetBool("IsAttacking", true);
    30.         }
    31.  
    32.         if (timer <= 0){
    33.             Shoot();
    34.         }
    35.  
    36.         if (hasClicked){
    37.             timer -= Time.deltaTime;
    38.         }
    39.  
    40.         void Shoot()
    41.         {  
    42.             hasClicked = false;
    43.             RaycastHit2D hitInfo = Physics2D.Raycast(firePoint.position, playerPos, distance);
    44.  
    45.             if (hitInfo){
    46.                 Debug.Log(hitInfo.transform.tag);
    47.                 lineRenderer.SetPosition(0, firePoint.position);
    48.                 lineRenderer.SetPosition(1, playerPos);
    49.             }else{
    50.                 Debug.Log("missed");
    51.             }
    52.             timer = 2.5f;
    53.             animator.SetBool("IsAttacking", false);
    54.         }
    55.     }
    56. }
     
  2. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
  3. that_me

    that_me

    Joined:
    Apr 2, 2022
    Posts:
    23
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,646
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    You should review the docs for the above method.

    It does NOT accept two positions as the first two arguments.

    Go see what it does require.

    If you have two positions, you may subtract two vectors to get a direction vector.
     
    MelvMay likes this.
  6. that_me

    that_me

    Joined:
    Apr 2, 2022
    Posts:
    23
    Thank you all so much. Using "Debug.Log(hitInfo.point);" I found out that the raycast was hitting the firepoint. So I changed a setting and that wasn't happening anymore. And as Kurt-Dekker said I subtracted the firepoint position from the player position for the raycast so the raycast is now hitting the player and working as it should thank you all so much!!!
     
    Kurt-Dekker likes this.
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    Note that if you have two positions, you can and perhaps should use Physics2D.Linecast