Search Unity

Question tight controls for a top down shooter

Discussion in 'Scripting' started by Saw9yer, Nov 26, 2021.

  1. Saw9yer

    Saw9yer

    Joined:
    Sep 19, 2020
    Posts:
    72
    Hello everyone,

    I try to improve the movments of my top down shooter.
    Actually it fe my movements fells robotic.
    This is the code i use :
    Code (CSharp):
    1.  
    2.  
    3.     void FixedUpdate()
    4.     {
    5.         VelocityBasedAxis("horizontal", inputHorizontal, velocity_x);
    6.         VelocityBasedAxis("vertical", inputVertical, velocity_y);
    7.  
    8.         rb.velocity = new Vector2(velocity_x, velocity_y);
    9.     }
    10.  
    11. void VelocityBasedAxis(string axis, int input, float velocity)
    12.     {
    13.         //Accelerate on axis
    14.         if (input == 1 && velocity >= 0)
    15.         {
    16.             velocity += acceleration * Time.deltaTime;
    17.         }
    18.         else if (input == -1 && velocity <= 0)
    19.         {
    20.             velocity -= acceleration * Time.deltaTime;
    21.         }
    22.         //Decelerate when you want to chaneg direction
    23.         else if (input == 1 && velocity <= 0)
    24.         {
    25.             velocity += decelerationHigh * Time.deltaTime;
    26.         }
    27.         else if (input == -1 && velocity >= 0)
    28.         {
    29.             velocity -= decelerationHigh * Time.deltaTime;
    30.         }
    31.         //Decelerate when no input
    32.         else if (input == 0 && velocity >= minValueMovement)
    33.         {
    34.             velocity -= deceleration * Time.deltaTime;
    35.         }
    36.         else if (input == 0 && velocity <= -minValueMovement)
    37.         {
    38.             velocity += deceleration * Time.deltaTime;
    39.         }
    40.         //Stop when velocity near 0
    41.         else if (input == 0 && (velocity < minValueMovement && velocity > -minValueMovement))
    42.         {
    43.             velocity = 0;
    44.         }
    45.  
    46.         //Retrun a clamped value
    47.         velocity = Mathf.Clamp(velocity, -maxVelocity, maxVelocity);
    48.         if (axis == "horizontal")
    49.         {
    50.             velocity_x = velocity;
    51.         }
    52.         else if (axis == "vertical")
    53.         {
    54.             velocity_y = velocity;
    55.         }
    56.     }
    I am a bit lost i want to have precises controls (with i think a non linerar acceleration/deceleration) on my player but what is the best to use? velocity, addForce,translate ...

    Can you help me?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    I have no idea what this means but you're welcome to look at my twinstick shooter example. Look for the
    DemoTwinStickShooter
    scene in my Proximity Buttons project.

    Specifically, this is the code for moving the player:

    https://github.com/kurtdekker/proxi...TwinStickShooter/TwinStickPlayerController.cs

    .., around line 94 in the UpdateMoving() method.

    proximity_buttons is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/proximity_buttons

    https://github.com/kurtdekker/proximity_buttons

    https://gitlab.com/kurtdekker/proximity_buttons

    https://sourceforge.net/projects/proximity-buttons/