iamos
iamos

Reputation: 3

Unity android 2d movement issue

I want to create a game in which I can move box left and right to avoid falling boxes and it works, but the problem is when I hold my finger in one place, the player box then is shaking left and right. You can see this at this gif http://gfycat.com/FragrantBrokenEyas. And here's the code of C# script

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {
private Camera m_cam;
private float m_offsetRight = 1.0F;
private float m_offsetLeft = 0.0F;


void Awake() {
    m_cam = Camera.main;
}


void Update () {
    rigidbody2D.velocity = new Vector2(0 * 10, 0);
    Move ();
}


void Move () {
    if (Application.platform == RuntimePlatform.Android) {
        Vector3 player_pos = m_cam.WorldToViewportPoint(rigidbody2D.position);
        Vector3 touch_pos = new Vector3(Input.GetTouch(0).position.x, 0, 0);

        touch_pos = Camera.main.ScreenToViewportPoint(touch_pos);

        if(touch_pos.x > player_pos.x)
            rigidbody2D.velocity = new Vector2(1 * 10, 0);
        else if (touch_pos.x < player_pos.x)
            rigidbody2D.velocity = new Vector2(-1 * 10, 0);
    }
    else{       
        Vector3 player_pos = m_cam.WorldToViewportPoint(rigidbody2D.position);
        float h = Input.GetAxis ("Horizontal");

        if (h > 0 && player_pos.x < m_offsetRight)
            rigidbody2D.velocity = new Vector2(h * 10, 0);
        else if (h < 0 && player_pos.x > m_offsetLeft)
            rigidbody2D.velocity = new Vector2(h * 10, 0);
        else 
            rigidbody2D.velocity = new Vector2(0, rigidbody2D.velocity.y);
    }
}


void OnTriggerEnter2D(Collider2D other) {
    if (other.gameObject.name == "Enemy(Clone)") {
        Destroy(other.gameObject);
        GameSetUp.score -= 2;
    } else if (other.gameObject.name == "Coin(Clone)") {
        GameSetUp.score += 2;
        Destroy(other.gameObject);
    }
}

}

I think that the problem is somewhere over here:

touch_pos = Camera.main.ScreenToViewportPoint(touch_pos);

        if(touch_pos.x > player_pos.x)
            rigidbody2D.velocity = new Vector2(1 * 10, 0);
        else if (touch_pos.x < player_pos.x)
            rigidbody2D.velocity = new Vector2(-1 * 10, 0);

Upvotes: 0

Views: 355

Answers (1)

maZZZu
maZZZu

Reputation: 3615

This "one place" is probably the place where the x coordinate of the touch is about the same than the x coordinate of the player box.

While your finger is pressing that area, on the first frame touch_pos.x > player_pos.x is true and the velocity is set to positive. On the next frame player has moved to positive direction and this time touch_pos.x < player_pos.x is true and speed is set to negative. This leads to the shaking effect.

To avoid it, you can for example increase and decrease the velocity slowly, rather than changing it completely to opposite in one frame. You can do this by changing your code to this:

    touch_pos = Camera.main.ScreenToViewportPoint(touch_pos);

    const float acceleration = 60.0f;
    const float maxSpeed = 10.0f;

    if(touch_pos.x > player_pos.x)
        if(rigidbody2D.velocity.x < maxSpeed)
            rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x + acceleration * Time.deltaTime, 0);
        else
            rigidbody2D.velocity = new Vector2(maxSpeed, 0);
    else if (touch_pos.x < player_pos.x)
        if(rigidbody2D.velocity.x > -maxSpeed)
            rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x - acceleration * Time.deltaTime , 0);
        else
            rigidbody2D.velocity = new Vector2(-maxSpeed, 0);

Just adjust the acceleration to the value that seems right.

Upvotes: 1

Related Questions