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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2.5D laser question C#

Discussion in '2D' started by IgorVermeulen, Dec 18, 2015.

  1. IgorVermeulen

    IgorVermeulen

    Joined:
    Dec 18, 2015
    Posts:
    2
    Hey, I'm trying to create a laser that goes from the player to the mouseposition.

    After reading tons of threads and watching some video tutorials on 3D lasers I've come to this:

    Code (CSharp):
    1. line.enabled = true;
    2.         Vector3 mousepos = Input.mousePosition;
    3.         //mousepos.z = 0.0f;
    4.         Vector3 mouseposWorld = Camera.main.ScreenToWorldPoint(mousepos);
    5.         Vector3 direction = mousepos - transform.position;
    6.  
    7.  
    8.         while (Input.GetButtonDown("Fire1"))
    9.         {
    10.             Ray2D ray = new Ray2D(transform.position, direction) ;
    11.             line.SetPosition(0, ray.origin);
    12.             line.SetPosition(1, ray.GetPoint(10));
    13.  
    14.             yield return null;
    15.         }
    16.         line.enabled = false;
    17.     }
    Result is that I can shoot a laser, but it always shoots in the direction of the middle of my screen.
     
  2. IgorVermeulen

    IgorVermeulen

    Joined:
    Dec 18, 2015
    Posts:
    2
    ok i found it, seems like i was already close, but didnt completetly get how the screentoworldpoint worked