Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Need help with a raycast!

Discussion in 'Getting Started' started by Noodle_Meanie, Feb 1, 2023.

  1. Noodle_Meanie

    Noodle_Meanie

    Joined:
    Sep 11, 2021
    Posts:
    110
    I've been trying to make something shoot a raycast, and when it hits the player gameObject, it sets a bool called "go" to true. I can't seem to get it to work. Please help and tell me how I can do this.
     
  2. jbnlwilliams1

    jbnlwilliams1

    Joined:
    May 21, 2019
    Posts:
    267
    Well, Line 3 of your code is incorrect c#. Lines 6-10 are causing an infinite loop, and the rest really does not make any sense at all.
     
  3. Noodle_Meanie

    Noodle_Meanie

    Joined:
    Sep 11, 2021
    Posts:
    110
    What? I think you have the wrong post...
     
  4. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    488
    I think they are getting at the fact that you have provided nothing for us to work from.

    If you want help with a script, then you need to show us that script - put it in a post using code tags (if you don't use code tags, then you may not get many people helping out).

    However for a raycast, it should be relatively simple to do and there should even be examples in the manual for the different overload versions of the command.

    But first, if you already have a script where you are trying to do the raycast, then show us that script.
     
  5. Noodle_Meanie

    Noodle_Meanie

    Joined:
    Sep 11, 2021
    Posts:
    110


    Code (CSharp):
    1. {
    2.     public GameObject player;
    3.  
    4.     public bool facingRight;
    5.     public bool go = false;
    6.     public float energy;
    7.     public Animator animator;
    8.  
    9.     public float speedSet;
    10.     private float speed;
    11.  
    12.     private void Start()
    13.     {
    14.         speed = speedSet;
    15.     }
    16.     void Update()
    17.     {
    18.         if (facingRight == true)
    19.         {
    20.             RaycastHit2D rayInfo = Physics2D.Raycast(transform.position, Vector2.right, 5);
    21.             if (rayInfo.collider != player)
    22.             {
    23.                 go = false;
    24.             }
    25.             else
    26.             {
    27.                 go = true;
    28.             }
    29.         }
    30.         else
    31.         {
    32.             RaycastHit2D rayInfo = Physics2D.Raycast(transform.position, Vector2.left, 5);
    33.             if(rayInfo.collider != player)
    34.             {
    35.                 go = false;
    36.             }
    37.             else
    38.             {
    39.                 go = true;
    40.             }
    41.         }
    42.  
    43.         if (go == true)
    44.         {
    45.             Debug.Log("hitP");
    46.             animator.SetBool("SeesPlayer", true);
    47.  
    48.             if(energy >= 0)
    49.             {
    50.                 energy -= Time.deltaTime;
    51.  
    52.                 if(facingRight == true)
    53.                 {
    54.                     transform.Translate(Vector2.right * speed * Time.deltaTime);
    55.                 }
    56.                 else
    57.                 {
    58.                     transform.Translate(Vector2.left * speed * Time.deltaTime);
    59.                 }
    60.             }
    61.             else
    62.             {
    63.                 Destroy(gameObject);
    64.             }
    65.  
    66.         }
    67.         else
    68.         {
    69.             animator.SetBool("SeesPlayer", false);
    70.         }
    71.     }
    72. }