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

Need help firing projectile towards mouse

Discussion in 'Scripting' started by IanGarrett1234, Mar 5, 2020.

  1. IanGarrett1234

    IanGarrett1234

    Joined:
    Mar 5, 2020
    Posts:
    2
    I am creating a spell based rogue like game (like wizard of legend) and I can’t figure out how to fire a projectile towards the mouse. I am using camera.main.screentoworkdpoint(Input.mouseposition) and it only fires towards the center of the screen. Anyone know how to help or fix this?
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    This is one of those things that I think the docs could use some clarification on, given the number of times that I've seen this kind of issue. The z value of the position you pass into ScreenToWorldPoint denotes the number of units away from the camera you want the point to be. Input.mousePosition, being a Vector2, gets converted to a Vector3 with a z of 0 when being passed in.

    The effect of this is that the resulting point you get out is on the near plane of the camera -- which if I had to guess is probably 0.3 units away. Make sure that the value you pass in for the z will give you a point at the same depth that you want to use it. If you were to take the default unity scene as an example, you would pass in a value of 10 because the camera is at z = -10, placing the resulting point at z = 0.

    My apologies if this text explanation doesn't click for you, I would create a diagram (which would clear this up instantly) but I am not in the position to do so at the moment.
     
  3. IanGarrett1234

    IanGarrett1234

    Joined:
    Mar 5, 2020
    Posts:
    2
    Ok I will try to figure it out tomorrow and if you are able to make a graph that would help a lot. The game is saved on a different computer so I’ll have access to it tomorrow to try it out. Thanks for you help.