Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  3. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

how to get input position of mouse int ut?

Discussion in 'Project Tiny' started by apoorv_baked, Feb 1, 2019.

  1. apoorv_baked

    apoorv_baked

    Joined:
    Feb 1, 2019
    Posts:
    5
    i tried doing it the unity way but it didn't work , please help ,urgent.
     
  2. FutureWang

    FutureWang

    Joined:
    May 24, 2018
    Posts:
    22
    Code (JavaScript):
    1. let touch: ut.Core2D.Touch = ut.Core2D.Input.getTouch(0);
    2.  
    3.                     let touchPos = new Vector2(touch.x, touch.y);
     
  3. Rupture13

    Rupture13

    Joined:
    Apr 12, 2016
    Posts:
    131
    Code (CSharp):
    1. let mousePosRaw = getPointerWorldPosition(this.world, this.world.getEntityByName("Camera"));
    2. let mousePos = new Vector2(mousePosRaw.x, mousePosRaw.y);
    3.  
    4. ------------------------- using static method  below ------------------------------------
    5.  
    6. /** Returns the point of clicking in world space while considering frame size */
    7. static getPointerWorldPosition(world: ut.World, cameraEntity: ut.Entity): Vector3 {
    8.             let displayInfo = world.getConfigData(ut.Core2D.DisplayInfo);
    9.             let displaySize = new Vector2(displayInfo.width, displayInfo.height);
    10.             let inputPosition = ut.Runtime.Input.getInputPosition();
    11.             return ut.Core2D.TransformService.windowToWorld(world, cameraEntity, inputPosition, displaySize);
    12. }
     
  4. surfuay

    surfuay

    Joined:
    Sep 4, 2018
    Posts:
    12
    does that touch method allow for mouse input as well, just as standard desktops?
     
  5. surfuay

    surfuay

    Joined:
    Sep 4, 2018
    Posts:
    12
    I'm trying to do your method but anytime I try to type in the "getPointerWorldPosition" it gives me an error
     
  6. Rupture13

    Rupture13

    Joined:
    Apr 12, 2016
    Posts:
    131
    You might want to read the entire code sample I provided.
    The function "getPointerWorldPosition" must be made yourself, the function is at the bottom of the code sample. You can just copy paste that :)
     
    surfuay likes this.
  7. surfuay

    surfuay

    Joined:
    Sep 4, 2018
    Posts:
    12
    oh I'm sorry I didn't realize that the code below it is what allowed for the use of it. Thanks and sorry. this ECS is really messing with me.

    so would I need to make a component for each tag, also would this cause the player entity with a sprite component to move to these places?

    if (ut.Core2D.Input.getMouseButtonDown(0))
    localPosition = mousePos;
     
    Last edited: May 24, 2019
  8. Rupture13

    Rupture13

    Joined:
    Apr 12, 2016
    Posts:
    131
    The code sample doesn't use any non-standard Tiny components. If you want just your player entity to move by this code, you would indeed need to make a new component (like "PlayerMovement" or something) and then use the code in a system that runs only on entities with at least that new component.

    Setting the localPosition of the player entity in the system to the mouseposition would indeed move the player to the mousePosition. However, there are a few things to note here:
    • the localPosition is a Vector3, so you could just use the "mousePosRaw" from the code (which is a Vector3) and not bother making the "mousePos" (which is a Vector2)

    • setting the localPosition to mousePos(Raw) will instantly teleport the player entity to that position. If you want the player to incrementally move towards that position, you should make additional code to lerp the player entity position towards the mousePos(Raw) over time.

    I hope this helps :)
     
  9. surfuay

    surfuay

    Joined:
    Sep 4, 2018
    Posts:
    12
    THAT HELPS SO MUCH. I had just learned standard C# : Monobehavior about a year ago (as my first language) so when I started trying to do this it was a mental tough mudder. It was just not making sense to me with the tutorials I was watching and the other manuals I was reading.
     
    Rupture13 likes this.
  10. surfuay

    surfuay

    Joined:
    Sep 4, 2018
    Posts:
    12
    so when I'm putting in the static method, does that need to be another script altogether or can I put it in the OnUpdate: void of my player input system script. I'm trying it before, in and after the OnUpdate but it's throwing an error in each place specifically

    "declaration or statement expected" but I was able to understand what your tags and references are (not that they're difficult, just new to me) so I at least know how to access them if I could get rid of the primary error being thrown in reference to the word "static"

    SO I STOP ASKING YOU, where are the libraries for unity tiny that I can read through.
     
  11. Rupture13

    Rupture13

    Joined:
    Apr 12, 2016
    Posts:
    131
    The static method should not be placed inside the OnUpdate method. Putting it before or after the OnUpdate, but within the System class should work though. When put there, you should be able to use it by calling it on the name of the system and then the method. So if your system is called "ExampleSystem", then you should be able to use the method like
    ExampleSystem.getPointerWorldPosition(...);

    The whole of that system would look something like this then:
    Code (CSharp):
    1. namespace game {
    2.     /** New System */
    3.     export class ExampleSystem extends ut.ComponentSystem {
    4.  
    5.         OnUpdate(): void {
    6.             [...]
    7.             let mousePosRaw = ExampleSystem.getPointerWorldPosition(this.world, this.world.getEntityByName("Camera"));
    8.             [...]
    9.         }
    10.  
    11.         /** Returns the point of clicking in world space while considering frame size */
    12.         static getPointerWorldPosition(world: ut.World, cameraEntity: ut.Entity): Vector3 {
    13.             let displayInfo = world.getConfigData(ut.Core2D.DisplayInfo);
    14.             let displaySize = new Vector2(displayInfo.width, displayInfo.height);
    15.             let inputPosition = ut.Runtime.Input.getInputPosition();
    16.             return ut.Core2D.TransformService.windowToWorld(world, cameraEntity, inputPosition, displaySize);
    17.         }
    18.     }
    19. }

    I myself put the static method in another file, called "ClickingUtils". I can then use the method from the ExampleSystem using
    ClickingUtils.getPointerWorldPosition(...);
    .
    That ClickingUtils class looks like this (I have a few more static methods in there, but I removed them from this snippet for example purposes):
    Code (CSharp):
    1. namespace game {
    2.     /** Utility class for various clicking, pressing and swiping related functions */
    3.     export class ClickingUtils {
    4.         /** Returns the point of clicking in world space while considering frame size */
    5.         static getPointerWorldPosition(world: ut.World, cameraEntity: ut.Entity): Vector3 {
    6.             let displayInfo = world.getConfigData(ut.Core2D.DisplayInfo);
    7.             let displaySize = new Vector2(displayInfo.width, displayInfo.height);
    8.             let inputPosition = ut.Runtime.Input.getInputPosition();
    9.             return ut.Core2D.TransformService.windowToWorld(world, cameraEntity, inputPosition, displaySize);
    10.         }
    11.     }
    12. }
    ----------------------------------------------------

    As for your other question, about the Unity Tiny libraries. There isn't very much extensive documentation at the moment, but there are two places where you can find information.
    For things that aren't in those two links, you should definitely feel free to ask around on this forum.
    You could also look if something like your question has already been asked (and answered) on this forum by typing
    "Project Tiny" <what you want to search for>
    into Google (with the
    ""
    symbols!).

    Hope this helps :)
     
    surfuay and reallyhexln like this.
  12. surfuay

    surfuay

    Joined:
    Sep 4, 2018
    Posts:
    12
    super helpful, i'll read through the libraries. good to know i'm not just dense when looking for references to what I'm looking for all the time. Thanks!