Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Danieo · Jun 05, 2016 at 04:13 PM · 2d gamemobilerigidbody2dvelocityphysics2d

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: alt text

alt text

17532356-8060652.jpg (14.8 kB)
17532356-8060658.jpg (14.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

,Rotate a gameobject around another while being attracted by its gravity 1 Answer

Simple question about unity 5 rigidbody/gravity 1 Answer

Change a specific Gameobjects rigidbody gravity to horizontal (left) 1 Answer

Rigidbody2D returning zero values 2 Answers

Adding a force to RigidBody2d - Velocity returns 0 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges