Search code examples
c#unity-game-enginetouchgame-physics

Smooth touch movement on axis only while going up?


I'm new to Unity and c#. I'm creating a project for mobile phones. I want to move the jet Axis position only by touching left and right while the Jet is going up

if (Input.touchCount > 0)
      {
           Touch touch = Input.GetTouch(0);

                 switch (touch.phase)
                 {
                    case TouchPhase.Began:
          if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
            {
                    //side to side movement
                     if (touch.position.x < Screen.width / 2)
                        rb.velocity = new Vector2(- 2f, transform.position.y);
                     if (touch.position.x > Screen.width / 2)
                        rb.velocity = new Vector2(+ 2f, transform.position.y);
                }
                   break;
                  case TouchPhase.Ended:
                      rb.velocity = new Vector2(0f, 0f);
                      break;
          }

The Jet have Addforce so the jet slows down when ever I touch left and right.

Jet code:

switch (JetOn)
        {
            case true:
             StartCoroutine(BurnFuel());
             rb.AddForce(new Vector2(0f, JumpForce), ForceMode2D.Force);
                break;
            case false:
                rb.AddForce(new Vector2(0f, 0f), ForceMode2D.Force);
                break;
       }

Solution

  • You should not mix AddForce and manually assigning to velocity. Assigning to velocity directly makes AddForce act unpredictably, and often results in it never working.

    Choose one or the other and use it for every change in velocity you need to make that frame.

    Here is an example of your code with only assigning to velocity:

    if (Input.touchCount > 0)
    {
        Touch touch = Input.GetTouch(0);
    
        switch (touch.phase)
        {
            case TouchPhase.Began:
                if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
                {
                    //side to side movement
                    if (touch.position.x < Screen.width / 2)
                        // I think you mean rb.velocity.y here instead of transform.position.y 
                        rb.velocity = new Vector2(- 2f, rb.velocity.y);
                    if (touch.position.x > Screen.width / 2)
                        rb.velocity = new Vector2(+ 2f, rb.velocity.y);
                }
                break;
            case TouchPhase.Ended:
                rb.velocity = new Vector2(0f, 0f);
                break;
        }
    }
    
    ...
    
    switch (JetOn)
    {
        case true:
            StartCoroutine(BurnFuel());
            rb.velocity += new Vector2(0f, JumpForce) / rb.mass;
            break;
        case false:
            // unnecessary but included for example purposes
            // rb.velocity += new Vector2(0f, 0f) / rb.mass;
            break;
    }