Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Platform movement stop not normal

Discussion in '2D' started by Flechenoir, Dec 13, 2014.

  1. Flechenoir

    Flechenoir

    Joined:
    Sep 30, 2014
    Posts:
    6
    Hi,

    I'm total new on unity, sorry if I don't ask this question correctly or if it's a double post....

    I have an strange problem with my character and his movement.

    When my character with a box collider 2D and rigidbody 2D move, sometime, he stop... Without reason ! The ground is a simple sprite of 60px with a box collider 2D.

    Here my script for control my character (if you have a suggestion for this script, say me :D)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class playerControlScript : MonoBehaviour {
    5.  
    6.     public float speedMax  = 10;
    7.     public float speedUp   = 5;
    8.     public float speedDown = 8;
    9.  
    10.     private Vector2 movement;
    11.     private float   currentSpeed;
    12.     private bool    isRight;
    13.  
    14.     void Update () {
    15.         float inputX    = Input.GetAxis("Horizontal");
    16.  
    17.         Debug.Log (inputX);
    18.  
    19.         movement = new Vector2(
    20.             inputX * getSpeed(inputX),
    21.             0);
    22.  
    23.         if (inputX > 0 && isRight == true) {
    24.             Flip();
    25.             isRight = false;
    26.         }
    27.  
    28.         if (inputX < 0 && isRight == false) {
    29.             Flip();
    30.             isRight = true;
    31.         }
    32.     }
    33.  
    34.     float getSpeed(float input){
    35.         if(input != 0)
    36.             currentSpeed += speedUp;
    37.         if (input == 0)
    38.             currentSpeed -= speedDown;
    39.  
    40.         if (currentSpeed > speedMax)
    41.             currentSpeed = speedMax;
    42.         if (currentSpeed < 0)
    43.             currentSpeed = 0;
    44.  
    45.         return currentSpeed;
    46.     }
    47.  
    48.     void FixedUpdate()
    49.     {
    50.         rigidbody2D.velocity = movement;
    51.     }
    52.  
    53.     void Flip(){
    54.         Vector3 theScale = transform.localScale;
    55.         theScale.x *= -1;
    56.         transform.localScale = theScale;
    57.     }
    But I don't understand why the character stop without reason sometime. It's not always at the same position, that total random and the move work ( tested with Debug.log() ).



    Thank you for your help and sorry for my bad English ;).

    Flechenoir.
     
  2. Flechenoir

    Flechenoir

    Joined:
    Sep 30, 2014
    Posts:
    6
    I have found the problem, I explain for the future user with that problem, that a logical problem, with two screen-shoot you will understand !

    OLD:

    NEW: