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

Question Sudden movement

Discussion in '2D' started by grancanon, May 14, 2023.

  1. grancanon

    grancanon

    Joined:
    Mar 15, 2022
    Posts:
    12
    Hello everyone !,
    I'm making a minigame with a spaceship, rotating seems to rotate quite clean and smooth, but in the attached video you can see how the speed seems to be bumping, it doesn't look very smooth, I would like to know what I'm doing wrong, or what do I need to apply to make the movement look as smooth as possible.

    Link video:
    https://files.fm/u/eaws79q6j

    Attached capturing object and script

    Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Mirror;
    5. using UnityEngine.SceneManagement;
    6. using Mirror.Experimental;
    7.  
    8. public class ScriptShipTesting : MonoBehaviour
    9. {
    10.  
    11.     // Rotaciones
    12.     public float SpeedToRotate = 4.5f;
    13.     public int directionRotate = 0;
    14.  
    15.     // Velocidad
    16.     public bool acelerating = false;
    17.     public float MaxSpeed = 3.5f;
    18.     public float SpeedToAcelerate = 30f;
    19.     public float SpeedToDecelerate = 15f;
    20.  
    21.     // Animaciones
    22.     public GameObject leftFireEngine;
    23.     public GameObject rigthFireEngine;
    24.     public bool fireEngineOn = false;
    25.  
    26.     // Mas
    27.     public GameObject PlayerModelNormal;
    28.     public GameObject PlayerModelShip;
    29.  
    30.     public Rigidbody2D _rigidbodyNormal;
    31.  
    32.     public float verticalInputAcceleration = 1;
    33.     public float horizontalInputAcceleration = 20;
    34.  
    35.     public float maxSpeed = 10;
    36.     public float maxRotationSpeed = 100;
    37.  
    38.     public float velocityDrag = 1;
    39.     public float rotationDrag = 1;
    40.  
    41.     private Vector3 velocity;
    42.     private float zRotationVelocity;
    43.  
    44.     // Nuevas disparos
    45.     public GameObject balaPrefab;
    46.  
    47.     public Transform objetivoLeft;
    48.     public Transform objetivoRight;
    49.  
    50.     public Transform balaSpawnLeft;
    51.     public Transform balaSpawnRight;
    52.  
    53.     public float velocidadBala = 10f;
    54.     public bool lastShootLeftCanon = false;
    55.  
    56.     private void Start()
    57.     {
    58.  
    59.         Application.targetFrameRate = 60;
    60.     }
    61.  
    62.     private void Update()
    63.     {
    64.         // apply forward input
    65.         Vector3 acceleration = Input.GetAxis("Vertical") * verticalInputAcceleration * transform.up;
    66.         velocity += acceleration * Time.deltaTime;
    67.  
    68.         // apply turn input
    69.         float zTurnAcceleration = -1 * Input.GetAxis("Horizontal") * horizontalInputAcceleration;
    70.         zRotationVelocity += zTurnAcceleration * Time.deltaTime;
    71.  
    72.  
    73.         if (Input.GetButtonDown("Fire1"))
    74.         {
    75.  
    76.             Debug.Log("Dispara");
    77.             Disparar();
    78.         }
    79.  
    80.     }
    81.  
    82.  
    83.  
    84.  
    85.  
    86.     private void FixedUpdate()
    87.     {
    88.         // apply velocity drag
    89.         velocity = velocity * (1 - Time.fixedDeltaTime * velocityDrag);
    90.  
    91.         // clamp to maxSpeed
    92.         velocity = Vector3.ClampMagnitude(velocity, maxSpeed);
    93.  
    94.         // apply rotation drag
    95.         zRotationVelocity = zRotationVelocity * (1 - Time.fixedDeltaTime * rotationDrag);
    96.  
    97.         // clamp to maxRotationSpeed
    98.         zRotationVelocity = Mathf.Clamp(zRotationVelocity, -maxRotationSpeed, maxRotationSpeed);
    99.  
    100.         // update transform
    101.         transform.position += velocity * Time.fixedDeltaTime;
    102.  
    103.  
    104.         transform.Rotate(0, 0, zRotationVelocity * Time.fixedDeltaTime);
    105.  
    106.         Debug.Log("velocidad -> " + velocity * Time.fixedDeltaTime);
    107.  
    108.         Debug.Log("velocidad -> " + velocity * Time.fixedDeltaTime);
    109.  
    110.  
    111.     }
    112.  
    113.  
    114.     void Disparar()
    115.     {
    116.         GameObject bala;
    117.         Vector2 direccion;
    118.  
    119.         if (!lastShootLeftCanon)
    120.         {
    121.             bala = Instantiate(balaPrefab, balaSpawnLeft.position, Quaternion.identity);
    122.             direccion = (objetivoLeft.position - balaSpawnLeft.position).normalized;
    123.  
    124.             Debug.Log("1111111111 2");
    125.  
    126.         }
    127.         else
    128.         {
    129.             bala = Instantiate(balaPrefab, balaSpawnRight.position, Quaternion.identity);
    130.             direccion = (objetivoRight.position - balaSpawnRight.position).normalized;
    131.             Debug.Log("1111111111 3");
    132.  
    133.  
    134.         }
    135.  
    136.         bala.GetComponent<Rigidbody2D>().velocity = direccion * velocidadBala;
    137.         bala.transform.right = direccion;
    138.         bala.transform.Rotate(0, 0, -90); // Ajustar la rotación en el eje Z en 180 grados
    139.  
    140.  
    141.         // Condicion para que al siguiente disparo dispare del cañon contrario
    142.         lastShootLeftCanon = !lastShootLeftCanon;
    143.  
    144.     }
    145.  
    146. }
    147.  

    Thanks in advance.
     

    Attached Files:

    Last edited: May 14, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,022
    Nobody wants to download and open files... if you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    You're welcome to sniff around a very straightforward Rigidbody2D spaceship engine in my Proximity Buttons project.

    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/

    Spaceship2D controller:

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpaceShip2D : MonoBehaviour
    6. {
    7.     public float RateOfTurn;
    8.  
    9.     public float MaxSpeed;
    10.  
    11.     public float Acceleration;
    12.     public float Damping;
    13.  
    14.     void Reset()
    15.     {
    16.         RateOfTurn = 180;
    17.         MaxSpeed = 10;
    18.         Acceleration = 10;
    19.         Damping = 0.5f;
    20.     }
    21.  
    22.     Rigidbody2D rb2d;
    23.  
    24.     void Start()
    25.     {
    26.         rb2d = gameObject.AddComponent<Rigidbody2D>();
    27.         // set to zero if you want not gravity
    28.         rb2d.gravityScale = 0.1f;
    29.     }
    30.  
    31.     float inputSteer;
    32.     bool inputThrust;
    33.  
    34.     void UpdateGatherInput()
    35.     {
    36.         inputSteer = 0;
    37.         inputThrust = false;
    38.  
    39.         inputSteer += -Input.GetAxisRaw( "Horizontal");
    40.         if (Mathf.Abs( Input.GetAxisRaw( "Vertical")) > 0.5f)
    41.         {
    42.             inputThrust = true;
    43.         }
    44.  
    45.         // TODO: gather other input here
    46.  
    47.         // gate and normalize steering
    48.         if (Mathf.Abs( inputSteer) < 0.3)
    49.         {
    50.             inputSteer = 0;
    51.         }
    52.         if (inputSteer != 0)
    53.         {
    54.             inputSteer = Mathf.Sign( inputSteer);
    55.         }
    56.     }
    57.  
    58.     void UpdateSteering()
    59.     {
    60.         if (inputSteer != 0)
    61.         {
    62.             float angle = rb2d.rotation;
    63.  
    64.             angle += inputSteer * RateOfTurn * Time.deltaTime;
    65.  
    66.             rb2d.MoveRotation( angle);
    67.         }
    68.     }
    69.  
    70.     void UpdateThrusting()
    71.     {
    72.         Vector2 forward = rb2d.transform.up;
    73.  
    74.         Vector2 velocity = rb2d.velocity;
    75.  
    76.         if (inputThrust)
    77.         {
    78.             velocity += (forward * Acceleration * Time.deltaTime);
    79.  
    80.             if (velocity.magnitude > MaxSpeed)
    81.             {
    82.                 velocity = velocity.normalized * MaxSpeed;
    83.             }
    84.         }
    85.  
    86.         velocity -= velocity * Damping * Time.deltaTime;
    87.  
    88.         rb2d.velocity = velocity;
    89.     }
    90.  
    91.     void FixedUpdate ()
    92.     {
    93.         UpdateGatherInput();
    94.  
    95.         UpdateSteering();
    96.  
    97.         UpdateThrusting();
    98.     }
    99. }
     
  3. grancanon

    grancanon

    Joined:
    Mar 15, 2022
    Posts:
    12

    Thanks for the advice on posting the code.
    As for your code, it works a little more fluently but it continues to make sudden jumps. Which may be due?.
    PS: If I don't limit the fps and it's at 1000fps it looks fluid