Search Unity

Using a Button to move an object

Discussion in 'Editor & General Support' started by Genkidevelopment, Jan 18, 2015.

  1. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    I have both read and viewed the unity tutorials as well as watching numerous user made you-tube videos... But!... Making a GUI button move the position of my main camera is proving a tough nut to crack!

    I understand that the 'On click' part within the default Button script is there to notify whatever object to be affected by the clicking of said button... I am at a loss as to actually drawing this up in code and don't understand which objects need to be dragged into where...

    Help please...

    Thanks

    Peace
     
  2. xxBarginsxx

    xxBarginsxx

    Joined:
    Mar 7, 2014
    Posts:
    68
    If you will provide more info i can pretty much see what you trying to do! just a bit more please! because I'm at loss my self... Do you want to know the GUI.Button code?? Do you want to know how to move object on a key press or a mouse button press?? I'm just a little lost my self :) Just a bit more and I might just be able to help! :) Thanks!
     
  3. sphericPrawn

    sphericPrawn

    Joined:
    Oct 12, 2013
    Posts:
    244
    Create a script and attach it to your camera. In the script, create a public function that sets the transform of the camera to the desired location, ie:

    Code (CSharp):
    1. public void MoveCamera ()
    2. {
    3.         this.transform.position = new Vector3(  // new location );
    4. }
    Then set the camera GameObject in the "On Click ()" event. From the dropdown menu beside it go to your script and then to your function.

    If you're trying to animate it or let the player control it etc, there's some more steps and a few different approaches of course.
     
    Genkidevelopment likes this.
  4. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Thank you and yes, I concluded that the script should be assigned to the camera... Are you saying that the script (once selected in the On Click event) will ONLY be called upon On Click being true? As in, actually coding the button click is not needed, it will simply call the code when clicked? I haven't explained that very well, I may come back to it and re word!

    Thanks

    Peace
     
  5. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    In your example script..

    What is the 'MoveCamera' on Line 1? ... Is this simply the name of the public void typed before it?

    On Line 3... 'this'... is this an actual coding term that is simply referencing the object that the script is assigned to? Or is it Demo type for an event?

    I ask because I have other objects transforming position, but in their scripts, I simply typed... 'transform.position = new Vector3 (x,y,z);'

    Without the need for specifying 'this' object... I assume because the script is only assigned to one particular GameObject.

    Thanks
     
  6. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Ok... So I have created a C# script as follows and attached it to the Main Camera...

    Upon pressing a GUI button I want to transform the position of the Main Camera... I have dragged the Main camera object onto the relevant 'HUD' Button and can see the script name within the function list... However, I do not understand what things are listen within the script (from the button 'On Click' menu).

    The options within my script are things like:

    Bool enabled, string name string tab, etc etc...

    Here is my script... attached to the Camera...


    1 using UnityEngine;
    2 using System.Collections;
    3 public class ButtonCameraMovement : MonoBehaviour
    4 {
    5 public void MoveCameraToHUD ()
    6 {
    7 gameObject.transform.position = new Vector3 (100, 100, 100);
    8 }
    9 }
    10


    Thanks for any help

    Peace
     
  7. sphericPrawn

    sphericPrawn

    Joined:
    Oct 12, 2013
    Posts:
    244
    You should see the names of any public functions from your script in the same menu that has "bool enabled," "string name," etc. In your case select "MoveCameraToHUD" as the function you want ran when the button is clicked.
     
    Genkidevelopment likes this.
  8. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Excellent muchios gracious...

    I have it... The MoveCameraToHuD is a function, that gets called upon OnClick, once it is assigned to any button...

    Thanks you very much..

    I am guessing the sliders follow the same pattern, we shall see...

    Wow - prototype shape is coming together WAY faster than I had imagined :D

    Thanks again...

    Peace
     
  9. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Thanks again for the all the help, greatly appreciated... I'm through another door, and the next stage of development starts... To the GUI - where I can actually set the parameters of the game... and from there I can start scripting the objects

    Excited :D
     
  10. xxBarginsxx

    xxBarginsxx

    Joined:
    Mar 7, 2014
    Posts:
    68

    like he said. and if you also looking for moving an object on a button press. you can do this

    public byte amountToMove;

    void OnGUI()
    {
    if (GUI.Button(0f, 0f, 256, 32), "Move Object Up")
    {
    transform.position += transform.up * amountToMove;
    }
    }

    So easy peasy! :D
     
  11. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Hmmm thanks for the advice xxbarginsxx, I seem to be running into a conflict of Old GUI tips vs New GUI tips...

    It seems like a lot of the code is written and invisible using the new GUI...

    One further problem I am experiencing, upon clicking of button I would like to (in some way) make a GUI canvas appear/disappear... Either by means of making it 'not active' or simply moving it off screen. However I am using on screen overlays so moving it doesn't seem to work (where ever I position it - it gets moved back to fit on the main screen!)..

    Still, another day, another problem to solve, another concept to learn :D

    Peace...