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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question First Bullet Spawned is sometimes Faster then later bullets

Discussion in 'Scripting' started by lonekinght_ak, May 6, 2022.

  1. lonekinght_ak

    lonekinght_ak

    Joined:
    Oct 30, 2016
    Posts:
    3
    I am following the AI tutorial and am on the acceleration chapter of the tanks, The first bullet spawned sometimes moves faster then the latter bullets. I have the following code for the shells

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Shell : MonoBehaviour
    6. {
    7.     public GameObject explosion;
    8.  
    9.     public float acceleration;
    10.     public float gravity = -9.8f;
    11.     public float zSpeed;
    12.     public float mass = 100f;
    13.     public float force = 1000f;
    14.     public float gravityAcceleration;
    15.     public float ySpeed;
    16.  
    17.     void OnCollisionEnter(Collision col)
    18.     {
    19.         if (col.gameObject.tag == "tank")
    20.         {
    21.             GameObject exp = Instantiate(explosion, this.transform.position, Quaternion.identity);
    22.             Destroy(exp, 0.5f);
    23.             Destroy(this.gameObject);
    24.         }
    25.     }
    26.  
    27.     // Start is called before the first frame update
    28.     void Start()
    29.     {
    30.        
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void Update()
    35.     {
    36.  
    37.      
    38.         acceleration = force / mass;
    39.      
    40.         zSpeed += acceleration * Time.deltaTime;
    41.         this.transform.Translate(0.0f, 0.0f, zSpeed);
    42.      
    43.         force = 0.0f;
    44.  
    45.  
    46.  
    47.  
    48.     }
    49. }
    50.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    You're doing this every frame.
    Code (CSharp):
    1.         acceleration = force / mass;
    2.         zSpeed += acceleration * Time.deltaTime;
    3.         this.transform.Translate(0.0f, 0.0f, zSpeed);
    4.         force = 0.0f;
    But because of the
    force = 0
    part, only the first frame of acceleration actually happens. After that you're stuck with whatever your zSpeed was after one frame. Now the amount you accelerated after one frame was dependent on Time.deltaTime, because you did
    acceleration * Time.deltaTime
    .

    In other words - your code is framerate-dependent. it will behave differently at different framerates. Your objects will get a bigger final speed when the frame took a long time to render. The first frame of a scene often takes longer than others because there is a lot of initialization going on.

    TL;DR you're doing some weird stuff with your speed/acceleration variables etc. and you're ending up with a framerate-dependent final speed for your bullets.
     
  3. lonekinght_ak

    lonekinght_ak

    Joined:
    Oct 30, 2016
    Posts:
    3
    How do i make each bullet framerate independent
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Just give it a constant speed that doesn't depend on deltaTime. Don't overcomplicate things:
    Code (CSharp):
    1. public float speed = 10f;
    2.  
    3. // Update is called once per frame
    4. void Update()
    5. {
    6.     transform.Translate(0.0f, 0.0f, speed * Time.deltaTime);
    7. }
    That being said you should really be moving this thing via a Rigidbody if you're trying to get collisions working...
     
    Deleted User likes this.
  5. Deleted User

    Deleted User

    Guest