Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Aim and Fire

Discussion in '2D' started by Reymus, Nov 14, 2015.

  1. Reymus

    Reymus

    Joined:
    Apr 27, 2015
    Posts:
    44
    Hi all.

    I'm trying to figure out how to do something and can't find anything in the tutorials to help.

    I have a player character, a bunny, that I want to make jump in a 2D sidescroller for iPhone and Android. I want to have the user tap and hold on the screen. The direction the bunny hops will be towards the user's finger when the user releases, and the power put into the jump will be based on how long the user holds his finger down.

    Are there any tutorials I've missed, on the unity3d website, or elsewhere?
     
  2. codeedward

    codeedward

    Joined:
    Dec 19, 2014
    Posts:
    93
    For direction - you could detect touch events, below you have script from my game as a sample:

    Code (CSharp):
    1. Touch touch = Input.GetTouch(0);
    2. if((touch.phase == TouchPhase.Began))
    3. {
    4.     IsMouseButtonDown = true;
    5. }
    6. else if(touch.phase == TouchPhase.Moved)
    7. {
    8.     // ScreenToWorldPoint is for translate screen(touch) position to your game position
    9.     mousePositionInWorldPoint = Camera.main.ScreenToWorldPoint (touch.position);
    10. }
    11. else if(touch.phase == TouchPhase.Stationary)
    12. {
    13.     mousePositionInWorldPoint = Camera.main.ScreenToWorldPoint (touch.position);
    14. }
    15. if(touch.phase == TouchPhase.Ended)
    16. {
    17.     IsMouseButtonDown = false;
    18. }

    For power
    - simply count time and multiply by some constant. On 'began' start counting and on the 'ended' finish. Then calculate and add power to your character.
     
    theANMATOR2b likes this.