Search Unity

question about mouse double click and popup command lists.

Discussion in 'Scripting' started by orindoomhammer, Nov 16, 2009.

  1. orindoomhammer

    orindoomhammer

    Joined:
    Oct 6, 2009
    Posts:
    113
    Ok what i would like to know is how do you make a double click command. i have a orbit cam setup but i would like to use a double click to to set where to move to. i dont know how to register a double click.

    also i would like to set up a right click comand that brings up a small window with some options depending on what you click on.

    some of the options would be.

    > Go here
    > Set Hostile
    > Target

    stuff like that. any examples will be great.
     
  2. orindoomhammer

    orindoomhammer

    Joined:
    Oct 6, 2009
    Posts:
    113
    ok i figured out the double click thing i just dont know how to use it.

    the scripting refrance isnt so spacific. any help or ideas would be great.
     
  3. ChrisMann

    ChrisMann

    Joined:
    Oct 21, 2009
    Posts:
    52
    Good question, I think this might work.


    Code (csharp):
    1.  
    2. var DoubleClickTime = 0.5;
    3. var SingleClick = false;
    4.  
    5. function Update()
    6. {
    7.   if(Input.GetButtonDown ("Fire1"))
    8.   {
    9.     SingleClick == true; // A single click has been commited
    10.   }
    11.  
    12.   if(SingleClick == true) // If a single click
    13.   {
    14.     DoubleClickTime -= Time.deltaTime; // Decrement DoubleClickTime by the amount of time since last update
    15.     if(DoubleClickTime > 0  Input.GetButtonDown("Fire1"))// If DoubleClickTime hasn't run out and click is pressed
    16.     {
    17.       GoHere(
    18.        Input.GetAxis("MouseX"),
    19.        Input.GetAxis  ("MouseY")); // Process double click
    20.     }
    21.     else
    22.     {
    23.       DoubleClickTime = 3.0; // Reset DoubleClickTime
    24.       SingleClick = false; // Reset SingleClick
    25.     }
    26.   }
    27. }
    28.  
    29. function GoHere(var X, var Y)
    30. {
    31.   //Move toward X,Y;
    32. }
    33.  
    34.  
    I'm in uni right now, so I haven't tested this. It might need tweaking. I'm positive that you want to countdown from the first click, if the counter is zero by the time of the second click it doesn't count. Altering the value of DoubleClickTime will allow for "lazier" double clicks with longer intervals in between.

    If it doesn't work, let me know and I'll fix it when I get home.
     
  4. orindoomhammer

    orindoomhammer

    Joined:
    Oct 6, 2009
    Posts:
    113
    im getting a few errors though im sure if i could figure out why the first one happens ill get rid of both of them.

    Assets/spaceships/shipmovementscripts/large ship movement script/largeshipmovement.js(80,17): BCE0043: Unexpected token: var.
    and
    Assets/spaceships/shipmovementscripts/large ship movement script/largeshipmovement.js(80,21): BCE0044: expecting EOF, found 'X'.

    being new to scripting i have yet to figure out what these are. so ill tweak it till you can find a fix and if i figure it out then ill post as such.
     
  5. ChrisMann

    ChrisMann

    Joined:
    Oct 21, 2009
    Posts:
    52
    change the function GoHere to this:


    Code (csharp):
    1. function GoHere(X,Y)
    2. {
    3.   //Move toward X,Y;
    4. }
    Sorry, I don't javascript very often.
     
  6. orindoomhammer

    orindoomhammer

    Joined:
    Oct 6, 2009
    Posts:
    113
    now i get this.
    Assets/spaceships/shipmovementscripts/large ship movement script/largeshipmovement.js(60,17): BCE0034: Expressions in statements must only be executed for their side-effects.
     
  7. ChrisMann

    ChrisMann

    Joined:
    Oct 21, 2009
    Posts:
    52
    it could be that there is a comment making it's way onto a line without a comment marker ("//"). Let me look at it when I get home.
     
  8. orindoomhammer

    orindoomhammer

    Joined:
    Oct 6, 2009
    Posts:
    113
    ok now a question on raycasting.

    do i acctualy need to hitsomething with my raycast to move my object to the location or can i just send it out into nowhere?
    and how would i do it.
     
  9. ChrisMann

    ChrisMann

    Joined:
    Oct 21, 2009
    Posts:
    52
    Are you working in 3d or 2d?

    You can use Transform.LookAt to rotate your object towards the position of the mouse, and then use Transform.forward to move it forwards until it arrives there. Because you are translating from the mouse, you will probably need to use ScreenToWorldPoint, or ScreenPointToRay. All this new stuff can be daunting, I understand, but no one will do the work for you. I've spent many hours staring at computer screens, frustrated with video game development. Stick at it, though, and your hard work will be evident.
     
  10. orindoomhammer

    orindoomhammer

    Joined:
    Oct 6, 2009
    Posts:
    113
    its not that i want you guys to do the work for me. if i wanted that id make a post in the collaberation page. when you guys do post code sure i use it but i also try to learn from it. being new to codeing like this but wanting to learn id rather you guys just point me in the right direction or if possible put some code up for example not exactly relateing to my project but just ingeneral. if i can see it i can tweak it. seeing as how the scripting resource doesnt have usage examples for alot of the stuff i want to do it sometimes gets hard to understand how to use it.

    i really appreciate the help you have given me. you have helped me set up a base script for double clicking on something so thats a great start.

    i only have alot of questions cause im new to it. after i start getting the hang of things ill be answering more then i ask. i also post all the code up after its working and give it to the comunity so even though i get help i try to give back as much as i can.
     
  11. ChrisMann

    ChrisMann

    Joined:
    Oct 21, 2009
    Posts:
    52
    I'm the same, game development is a cruel (but rewarding) mistress. I was working on a 3d space flight game, but I abandoned it in the early stages. Maybe we could work together?