Hey, I’m really new to unity and coding and I’m just practicing what I’ve learned so far, so I wrote a code to instantiate a projectile prefab in my mouse direction when I click and destroy it after 3 seconds, but the problem is I noticed the first bullet/projectile is always faster than the rest so I checked the velocity of it and the first one almost always is twice as fast as the rest:
void Shoot()
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction = mousePos - transform.position;
direction.Normalize();
projectileIns = Instantiate(projectile, transform.position, Quaternion.identity);
projectileRB = projectileIns.GetComponent<Rigidbody2D>();
projectileRB.AddForce(direction * projectileSpeed * 10 * Time.deltaTime, ForceMode2D.Impulse);
Debug.Log("Velo: " + projectileRB.velocity);
Destroy(projectileIns,2);
}
this method is called in Update(), not FixedUpdate (if that helps).
Velocity of objects in order of instantiation:
- Velo: (19.62, 0.37)
- Velo: (12.17, -1.22)
- Velo: (11.16, -1.11)
- Velo: (13.05, -1.30)
- etc.
also, since I know my code isn’t perfectly clean and there are better ways of doing this, so if you can help with cleaning my code or making it any better, I’ll be thankful.