- Home /
Rigidbody 2D loosing velocity
Hello! I'm working on my mobile game and I have some trouble with moving the player. The game mechanics are simple: The player is running upwards and your target is to click the screen to jump between walls to avoid obstacles. I have a problem with moving the player. Here's the player script:
public class Player : MonoBehaviour
{
public float speed = 3.0f;
public float maxSpeed = 10f;
public Sprite[] sprite;
public Sprite[] skins;
public int[] alias;
private Animator animator;
private Rigidbody2D rb;
private bool canJump = true;
public bool canMove = true;
public bool scorePoints = true;
public int direction;
private float cooldown;
private float immortal = 0;
private Game game;
private Rect touchArea;
private GameObject shield;
private bool shieldIsActive;
public GameObject shieldBtn;
private bool cantUseShield;
private AudioSource jumpSound;
void Start ()
{
int skinId = PlayerPrefs.GetInt ("SelectedSkin", 0);
Debug.Log (skinId);
touchArea = new Rect(0, Screen.height * 0.15f, Screen.width, Screen.height * 0.70f);
if(PlayerPrefs.HasKey("SoundMuted"))
GetComponent<AudioSource>().mute = true;
else
GetComponent<AudioSource>().mute = false;
jumpSound = GetComponent<AudioSource>();
sprite [0] = skins [alias[skinId]];
sprite [1] = skins [alias[skinId] + 1];
game = Camera.main.GetComponent<Game>();
animator = GetComponent<Animator> ();
rb = GetComponent<Rigidbody2D> ();
shieldBtn = GameObject.FindGameObjectWithTag("ShieldBtn");
animator.speed = 1.5f;
if (Mathf.FloorToInt (Random.Range (-1, 1)) == 0)
direction = 1;
else
direction = -1;
InvokeRepeating("Acc", 0.25f, 0.25f);
Jump (10);
Flip ();
}
void Update ()
{
Debug.Log(rb.velocity);
if (Input.anyKeyDown || Input.touchCount > 0) {
Touch touch = Input.touches [0];
if (touch.phase == TouchPhase.Began && touchArea.Contains(touch.position)) {
if (canJump == true && Time.time > cooldown && game.pause == false) {
jumpSound.Play();
cooldown = Time.time + 0.05f;
Jump (20);
Flip ();
}
}
}
if (Time.time >= immortal)
{
immortal = 0;
animator.Play("Move");
}
}
void Acc()
{
if(speed < maxSpeed)
speed += 1f;
else
CancelInvoke("Acc");
rb.velocity = new Vector2 (rb.velocity.x, speed);
}
void Jump (int strength)
{
canJump = false;
rb.isKinematic = false;
rb.AddForce (new Vector2 (direction*strength, 0), ForceMode2D.Impulse);
}
void OnCollisionStay2D(Collision2D collision)
{
canJump = true;
//rb.isKinematic = true;
}
void OnTriggerEnter2D (Collider2D collider)
{
if (game.gameOver == false)
{
if (collider.tag == "Count Points" && scorePoints)
game.score++;
//if (collider.tag == "Obstacle" && immortal == 0)
// Camera.main.SendMessage ("Over");
//if(collider.tag == "Meteor" && immortal == 0 && shieldIsActive == false)
// Camera.main.SendMessage ("Over");
}
}
void Flip ()
{
if (direction == 1) {
GetComponent<SpriteRenderer> ().sprite = sprite [1];
direction = -1;
} else {
GetComponent<SpriteRenderer> ().sprite = sprite [0];
direction = 1;
}
}
void Immortal ()
{
immortal = Time.time + 3;
animator.Play("Immortal");
}
public void ActivateShield ()
{
if (cantUseShield == false)
{
if(Social.localUser.authenticated)
GooglePlayGames.PlayGamesPlatform.Instance.IncrementAchievement(GPGSIds.achievement_meteors_pff, 1, (bool success)=>{});
transform.FindChild ("Shield").gameObject.SetActive (true);
shieldIsActive = true;
Invoke ("DeactivateShield", 1f);
CooldownShield ();
}
}
public void DeactivateShield ()
{
transform.FindChild("Shield").gameObject.SetActive(false);
shieldIsActive = false;
}
void CooldownShield ()
{
//shieldBtn.GetComponent<Animator>().Play("Cooldown");
shieldBtn.transform.FindChild("Progress").gameObject.SetActive(true);
cantUseShield = true;
Invoke("CooldownShieldEnd", 4f);
}
void CooldownShieldEnd ()
{
//shieldBtn.GetComponent<Animator>().Stop();
shieldBtn.transform.FindChild("Progress").gameObject.SetActive(false);
cantUseShield = false;
}
}
The problem is that sometines the player velocity is turning to zero and the player is stuck in place. I have no idea how to fix it. Here are some screenshots for you to better understand how the game works:
17532356-8060652.jpg
(14.8 kB)
17532356-8060658.jpg
(14.3 kB)
Comment