Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

In-Game Computer [Please Help!]

Discussion in 'Scripting' started by Disthron, Sep 15, 2018.

  1. Disthron

    Disthron

    Joined:
    Aug 21, 2015
    Posts:
    6
    Hi everyone, for the past week or so I've been trying to create an in-game computer that the player character can interact with. Programming an interactive screen using keyboard input was really easy but I haven't had much luck when it comes to mouse input.



    I want to be able to have the player move the mouse around on the virtual screen and click on buttons to mimic an old GUI operating system. I've gotten stuck on being able to move the mouse around the screen. I'm using 2 Vector 3 objects, one to hold the current position and one to hold the new position, subtracting them to get the distance the mouse has traveled since the last frame. The pointer seems to move around nicely but I can't seem to clamp the position to the canvas.

    I've tried locking the mouse position, I've tried a number of different ways to try and get the position of the mouse. I've noticed that the numbers that show up in the mouse position don't seem to have much correlation to what is displayed in the Rect Transform section. Also, the place where the boundary square is located seems to be related to where the mouse is on the screen when the program starts. If I move the mouse to a different area of the screen before clicking the play button it will leave the clamped square in a different place.

    So I'm pretty exhausted right now. Does anyone have any suggestions? Is there a better way of doing this? I'm not even sure how I'll get the button functionality working. My current thinking is to have 2D colliders on the pointer and button and call the OnClick method when the mouse has entered the button's collider and the user has pressed the mouse button. But again, I haven't gotten that far.

    Any help would be greatly appreciated.
    Disthron
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    1,674
  3. Disthron

    Disthron

    Joined:
    Aug 21, 2015
    Posts:
    6
    The screen is created using a 2D camera that renders to a render texture. It has a canvas attached to it that is outputting to the 2D camera and has the button and the image with a pointer graphic on it. I believe they are UI elements.

    In the gif that I posted you can see the 3D game world in the left-hand corner on the left panel, the 2D game that's on the fake computer is off on the right. I hope that makes sense, it's 3 am. Do you think this method will work through a render texture?
     
  4. Disthron

    Disthron

    Joined:
    Aug 21, 2015
    Posts:
    6
  5. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    1,674
    What you'd want to do is put an invisible world space canvas over your TV screen, then use that function to convert from the (real) screen to a local point in the world space canvas's rectangle, then use that as a point in your TV game's space which should be 1:1 if the world canvas is positioned properly.
     
  6. Disthron

    Disthron

    Joined:
    Aug 21, 2015
    Posts:
    6
    Ok, so I've tried to implement what you said, unfortunately, the function has an 'out' argument that I don't know what to do with. It's the last one. It's a Vector 2, I tried creating a Vector2 just to put in there but it still had the same issue.

    Also, I did some testing, trying to have the image collide with the button. By placing colliders on them, and activating the button's on click method if they were overlapping and the user clicked the button. I was able to invoke the OnClick method through the code but I've not been able to get a colision to trigger between the button and the mouse pointer. Though if I set the mouse pointer to not be a trigger it will push other objects around.
    I'm starting to think maybe I'm barking up the wrong tree with this method. I've been reading about casting rays through the render texture somehow but I'm still trying to read up on it. It seems not a lot of people are trying to do what I want to do.
     
  7. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    1,674
    If your intention is to perfectly mirror all of your mouse inputs as well, you may be better served having the real game in the centre of the screen and the computer off to the side with a second rendering camera. That way you can leverage all of the built-in tools for mouse clicks and whatnot.