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

Is Raycasting the answer? (Noob problem)

Discussion in 'Scripting' started by DigitalPanda, Oct 9, 2015.

  1. DigitalPanda

    DigitalPanda

    Joined:
    Mar 9, 2015
    Posts:
    9
    I apologize if this has been asked before or if I'm asking in the wrong section. Just let me know and a can move or remove this this post.

    Ok so first of all I'm using Playmaker right, I can understand JavaScript. My JS knowledge is weak though.
    I currently working on a Idle cookie clicker kinda game as its the most basic kinda of game i could think of.

    The main function of the gave is to give the player money when he clicks a button. So i have a 3D sphere for the cookie. It currently work by checking to see if its collider has been entered by a game object with a collider that should follow the mouse, if the collider has been entered then the cookie can be clicked. I'm using the action "Get Mouse Button down" to detect a click, but the action doesn't care about where is being clicked (Thats why im using the colliders). I figured by doing this I could also animate the cookie while the mouse is hovering.

    The problem I'm having comes to tracking the mouse. Currently im using actions to get the mouse's X and Y positions and then storing them in a variables called Mouse_X and Mouse_Y. The problem with this is action "Get mouse X/Y" store its as value 0-1, so at most the object the suppose to follow the mouse can move +-1 in any direction. I tried multiplying the X/Y variable based off the aspect ratio i.e 16/9, which sort of works but the object doesn't directly follow the mouse, it always stays closer to the top-right than the mouse does. actually now that i look at it it doesnt even keep a static distance from the mouse.

    I'm not really looking a "heres the script" answer, i more would like advise. I am still a noob so i dont know much about Raycasting, but it sounds like it might be a better solution then what im trying right now. So really what I'd like to know is.... Is what I'm trying now a decent solution or should a try Raycasting? And what ever it is could you give me some suggestions on how to make work? Thank you so much for you help. :)

    Also if you have any suggestions about Playmaker or scripting or anything else really, feel free to let me know.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Forget raycasting. It can be used, but there are simpler solutions. Here is how I would tackle it, starting from an empty scene.
    • Create your cookie
    • Add a sphere collider to the cookie
    • Add a EventTrigger component to the cookie. Add OnClick to the EventTrigger. Add a listener to fire off an appropriate method
    • Add an EventSystem to the scene
    • Add a PhysicsRaycaster component to your camera
    Now your method will automatically be called every time you click the cookie.
     
    DigitalPanda, ChrisSch and McMayhem like this.
  3. DigitalPanda

    DigitalPanda

    Joined:
    Mar 9, 2015
    Posts:
    9
    Thanks, it worked like a charm :). I'm definitely going to have to read up on the EventSystem and EventTrigger more. Feels like something i definitely want to have a firm understanding of. Well i don't have anything else to say, so Thanks Again. :)
     
    Kiwasi likes this.
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    All good. The automatic raycasters are a fairly new feature in the engine. So most tutorials still refer to manually raycasting the old way. But it's so much easier this way.

    And as a side effect this way will play nicely with Unity's UI system.