Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity ignore small vector2 values help

Discussion in 'Scripting' started by Prokopis-Petros, Jun 8, 2016.

  1. Prokopis-Petros

    Prokopis-Petros

    Joined:
    Jun 8, 2016
    Posts:
    24
    So ive made a character that can move around by changing the velocity of his rigidbody2D component, and have made it so that his rotation is changed depending on the velocity of that rigidbody2D, meaning that if he is moving left but is still sliding right, he will still be pointing right.

    The problem occurrs when the player hits a wall, and therefore is moving with very small values away from the wall, resulting in the sprite changing directions every frame.

    The way i thought of fixing this is by adding a sort of a deadzone, so that very small values wouldn't be picked up, but does anyone know how to do this

    This is what im using to find the velocity of my rigidbody2D

    Code (CSharp):
    1. transform.GetComponent<Rigidbody2D>().velocity;
    So it would be like

    Code (CSharp):
    1. If (transform.GetComponent<Rigidbody2D>().velocity > "Very low value") {
    2.  
    3. Make the player point towards traveling direction
    4.  
    5. }
    Although the problem i can see in doing this, is that there will be a delay from when the player starts moving, so if anyone else has a better solution to this, please tell me
    Any help is appreciated
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  3. Prokopis-Petros

    Prokopis-Petros

    Joined:
    Jun 8, 2016
    Posts:
    24
    I couldnt get it work well
    It basically worked when colliding with the wall most of the time, but, it brought a ton of bugs, where the player would not change rotation if he was close to the 0,0,0 point, or walking in the opposite direction, although the player would rotate into the right direction after a few seconds

    Code (CSharp):
    1.             //declare the current Magnitude value for ease of use
    2.             float currentMagnitudeValue = transform.position.magnitude;
    3.             //If delta magnitude is larger than 0.005, then change player rotation
    4.             if (currentMagnitudeValue - lastMagnitudeValue > 0.005)
    5.             {
    6.                 //Change rotation to velocity direction
    7.                 Vector2 dir = transform.GetComponent<Rigidbody2D>().velocity;
    8.                 float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    9.                 transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    10.             }
    11.             lastMagnitudeValue = currentMagnitudeValue;
    Although maybe im not using using the magnitude correctly?
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I thought you were trying to suppress rotation if the velocity was very low?
    Code (csharp):
    1.  
    2. if (rb.velocity.magnitude > 0.1)
    3. {
    4.     // do rotation stuff
    5. }
    6.  
     
    Prokopis-Petros likes this.
  5. Prokopis-Petros

    Prokopis-Petros

    Joined:
    Jun 8, 2016
    Posts:
    24
    I thought magnitude was a transform.position specific command since im relativly new to the unity commands
    Your code fixed most of my problems, although there are still ways of changing directions every frame by sliding on a wall, although to fix this problem i will probably have to come up with a better solution than just creating a deadzone, but this will do for now

    Thanks for the help
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Prokopis-Petros likes this.
  7. Prokopis-Petros

    Prokopis-Petros

    Joined:
    Jun 8, 2016
    Posts:
    24
    Seems ive pretty much fixed the problem completely now!
    The second part of my problem was that my hitbox was too complicated which resulted in wierd bugs when using my method of movement and slamming my character against a wall.
    Instead i just assigned a simple square as my hitbox (instead of 2 squares, one for head and one for the body), and the problem was pretty much non-existent

    Thanks for all the help