Search Unity

2D control? help me please

Discussion in 'Scripting' started by OndrejStolar, Mar 1, 2015.

  1. OndrejStolar

    OndrejStolar

    Joined:
    Mar 12, 2014
    Posts:
    2
    Hi all,
    can somebody help me please to create script to control my ''dot''?
    Basically I need to make controller - first tap on the mobile screen - dot will start moving to left side, second tap - will move it to right and again 3rd tap left,4th tap right etc. so basically dot will fly in space and I need to control it (right and left) only with one touch or eventually mouse click.


    Thank you for your time and please help me :)
     
  2. OndrejStolar

    OndrejStolar

    Joined:
    Mar 12, 2014
    Posts:
    2
    any one?
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,409
  4. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    Code (CSharp):
    1. //we create variable to see where the dot is going currently
    2. public bool goingRight = null;
    3.  
    4. void Update()
    5. {
    6. //we'll start off by saying that the dot will move forward by default even if it goes to right or left
    7. transform.Translate(Vector3.forward * Time.deltaTime * 4f);
    8.  
    9. //now we'll check for inputs from user (left mouse click)
    10. if(Input.GetMouseButtonDown(0))
    11. {
    12. //we'll set by default that first direction will be that it'll go towards right and each click will 'tick' the boolean on and off, meaning it'll turn right then left on each click
    13. goingRight = !goingRight;
    14. }
    15.  
    16. //now let's set the rules for it going to each direction
    17. if(goingRight)
    18. {
    19. transform.Translate(Vector3.right * Time.deltaTime * 3f);
    20. } else {
    21. transform.Translate(Vector3.left * Time.deltaTime * 3f);
    22. }
    23.  
    24. }
     
  5. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    You should remove line 7....
     
  6. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    Oh he does not want the dot to move by default forward all the time?
     
  7. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    My bad, didn't read the code correctly.... What happens when you run the code...
     
  8. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    I did not test it but, @OndrejStolar how does the code work for you?