Search Unity

On buttonClick, move character left/right

Discussion in 'Scripting' started by SaamBell, Aug 4, 2015.

  1. SaamBell

    SaamBell

    Joined:
    Mar 28, 2014
    Posts:
    128
    Okay so guys, this is what I've gotten so far. What I've got above is the code the allows the vehicle to be moved left and right using the arrow keys. However since I'm also porting to mobile i also need to have the ability to move left and right using the buttons in my canvas. Now I've setup the functions in my buttons as you can see, but no matter what code I try in these functions I can't get my vehicle to move at all. Any ideas?

    Click.png
     
  2. SaamBell

    SaamBell

    Joined:
    Mar 28, 2014
    Posts:
    128
    Or. If it's possible to easily implement. Could i scrap the buttons and make it so that the car is moved left and right by the input of the players finger?? What should I look at if i want to do this?
     
    igdeveloper likes this.
  3. oOHicksyOo

    oOHicksyOo

    Joined:
    Feb 22, 2015
    Posts:
    17
    Are you doing nothing a lane based system so there is 3 lanes and you switch on the press or gently move across each way. If the second one look into the Ipointerdata interfaces to get when you press down. You could also use the mobile input features to handle swiping if you're are doing lane based

    Edit: here's the link to the interface , just write a script using this interface to get if a button is pressed instead of clicked link unites default

    http://docs.unity3d.com/462/Documen...ystems.IPointerDownHandler.OnPointerDown.html
     
  4. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    when you REALESE button function MoveLeft will be called IF IT IS PUBLIC. its hard to say here what isnt working

    if want for player fingers you can check player input position on screen and if it is lower than screen.width/2 then move left otherwise move right (you can put that in any code so no canvas or other things)
     
  5. SaamBell

    SaamBell

    Joined:
    Mar 28, 2014
    Posts:
    128
    So along the lines of if the player input is < that screen.width/2 then move left, else move right? Sounds easy enough but what would i put into the code to seperate left and right movement. The way I'm currently doing it whilst running the game on windows is check if the player is using the left or right arrow key on the horizontal axis, and this determines whether they move left or right. How could i do the same by using the input - screen.width/2 method??
     
  6. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    you have unity specific sections like
    Code (CSharp):
    1. #if UNITY_WINDOWS  //or something like that
    2. #if UNITY_ANDROID
    3. #end
    goes something like this so if its for windows set controls with arrows else on android check input and if is left and right and move in right direction (its same as input.getkey(keycode.leftarrow) but you check if(inputposition < screen.width/2))
     
  7. SaamBell

    SaamBell

    Joined:
    Mar 28, 2014
    Posts:
    128
    But for the windows one I don't use input.getkey(keycode.leftarrow) i use

    position.x += Input.GetAxis ("Horizontal") * carSpeed * Time.deltaTime;

    position.x = Mathf.Clamp (position.x,-maxPos,maxPos);

    transform.position = position;

    Leaving me quite confused to to put after the if(inputposition < screen.width/2)) regarding to moving my vehicle left and right
     
  8. Cnc96

    Cnc96

    Joined:
    Dec 17, 2013
    Posts:
    57
    Have you actually added the code to the MoveLeft() and MoveRight() functions? I know it's a stupid question, but in your screenshot, you didnt have any functionality in the methods, hence why nothing was happening.
     
  9. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    input get axis returns between -1 and 1 (on joystick and if keys are not set to instant can return floats like 0.5)
    so you can keep this for windows but for android you can do
    Code (CSharp):
    1. int axis;
    2.  
    3. if (fingerPos < screen.width/2)
    4. {
    5.    axis = -1;
    6. } else if (bigger than)
    7.    axis = 1;
    8. }else
    9.   axis = 0;
    10. }
    11.  
    12. after that copy code and replace that horizontal axis part with just axis.
     
  10. SaamBell

    SaamBell

    Joined:
    Mar 28, 2014
    Posts:
    128
    I didn't know what to add to them which was the problem :')
     
  11. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    Well if you use left right side of screen you dont need buttons. Easiest way would be again with flags. Make moving in update function same as before but whole part of get axis horizontal replace with int axis. Int axis you set to -1 or 1 based on controls, in android case i already explained
     
  12. SaamBell

    SaamBell

    Joined:
    Mar 28, 2014
    Posts:
    128
    Okay got this so far

    if(fingerPos < Screen.width/2)
    {
    axis = -1;
    }
    else if (fingerPos > Screen.width/2)
    {
    axis = 1;
    }
    else
    axis = 0;

    However fingerPos doesn't exist for me and I'm not sure what it's supposed to be exactly?
     
  13. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384

    heh :p that is variable of finger position. i can just say what to do but seriously you would still have alot of questions. first do scavanger (the 2d tutorial of unity) and MANY questions will be answered.
     
  14. SaamBell

    SaamBell

    Joined:
    Mar 28, 2014
    Posts:
    128
    I tried what I thought might be a simpler method of adding this code to my two UI buttons


    public void MoveRight()
    {
    //Move player right
    car.transform.Translate(Vector3.right * carSpeed * Time.deltaTime);

    }

    public void MoveLeft()
    {
    //Move player left
    car.transform.Translate(Vector3.left * carSpeed * Time.deltaTime);

    }

    car is a GameObject and has been set in the inspector. Unfortunately this hasn't been working either? Any simple way to get this method working?
     
  15. Annas010

    Annas010

    Joined:
    Oct 5, 2018
    Posts:
    3
    are you already find the Solution??? atless i can tell you.., last comment on aug 5, 2015.., its already pass three year lol.., im late in this page.