Search Unity

Smoother bullet travel

Discussion in 'Physics' started by truthseeker089, Jun 19, 2019.

  1. truthseeker089

    truthseeker089

    Joined:
    May 6, 2019
    Posts:
    61
    hi guys, I've been experimenting with Is Trigger on my bullet so that I won't hit every box collider there is as it travels through the distance.. However, when it comes to hitting my actual target, the bullet passes through him as well.
    Actually I have an Ienumerator enablebox script for this to temporarily disable the collider but I am just wondering if there is anyway we can use Is Trigger instead for a smoother travel of the bullet?

    Raycast is way too advance for me.

    Thanks!
     
  2. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,162
    If raycasts are too advanced for you, then it's time to learn how to use them. Boxcasts as well.
     
    Ryiah, angrypenguin, Vryken and 2 others like this.
  3. truthseeker089

    truthseeker089

    Joined:
    May 6, 2019
    Posts:
    61
    this is the script I have on my hearthealth, it has 3 hearts, I am using switch for his health...actually it does work, however the hearts decrease right on when the bullet gets instantiated, it does not wait for the bullet to collide with the heart object....

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class hearthealth : MonoBehaviour {
    public GameObject heart1, heart2,heart3, gameover;
    public static int health;

    // Use this for initialization
    void Start () {
    health = 3;
    heart1.gameObject.SetActive (true);
    heart2.gameObject.SetActive (true);
    heart3.gameObject.SetActive (true);
    gameover.gameObject.SetActive (false);



    }

    // Update is called once per frame
    void Update () {
    if (health > 3)
    health = 3;

    switch (health) {
    case 3:
    heart1.gameObject.SetActive (true);
    heart2.gameObject.SetActive (true);
    heart3.gameObject.SetActive (true);

    break;
    case 2:
    heart1.gameObject.SetActive (true);
    heart2.gameObject.SetActive (true);
    heart3.gameObject.SetActive (false);
    break;

    case 1:
    heart1.gameObject.SetActive (true);
    heart2.gameObject.SetActive (false);
    heart3.gameObject.SetActive (false);

    break;
    case 0:
    heart1.gameObject.SetActive (false);
    heart2.gameObject.SetActive (false);
    heart3.gameObject.SetActive (false);
    gameover.gameObject.SetActive (true);

    break;

    }
    }
    }
     
  4. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,162
  5. truthseeker089

    truthseeker089

    Joined:
    May 6, 2019
    Posts:
    61
    the script attached to the bullet is this

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class bulletontrigger : MonoBehaviour {

    void OnTriggerEnter2D (Collider2D col)
    {
    hearthealth.health -= 1;
    Destroy (gameObject, 2f);
    }
    }
     
  6. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    The point of box colliders is they represent things that collide with other things, if they are not meant to be hit by bullets e.g. bullets can go through walls/windows, then they should be on a separate layer.
     
  7. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,162
    This is not even remotely the problem being described.
     
  8. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
     
  9. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,162
    Yes. The bullet is passing through things because it's using a box collider and travelling too fast, which means the physics system can't keep up. The easy solution to this is to use ray/box/spherecasts as necessary.
     
    Ryiah and angrypenguin like this.
  10. Deleted User

    Deleted User

    Guest

    I'm not sure why a lot of people think they must handle things like smooth collisions themselves... with some interpolation/collision modes on your rigidbodies it works just fine even for high velocities.
     
  11. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,162
    Because those can come with greater performance costs that you don't find with box/raycasts.
     
    xVergilx, Ryiah and Billy4184 like this.
  12. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    If a raycast is "way to advanced" i dont think you can do a complete project yet, its not as complicated as you believe, spend some time with it.
     
    Martin_H likes this.
  13. ShilohGames

    ShilohGames

    Joined:
    Mar 24, 2014
    Posts:
    3,023
    What I do on a moving projectile (such as a bullet) in a game is Raycast forward the amount the bullet would travel during the next couple rendering frames.
     
    Ryiah and angrypenguin like this.
  14. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I raytrace from current position to next position on fixed update(rb.velocity * fixedUpdate), eliminating the need for continuous collision detection
     
    angrypenguin likes this.
  15. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    736
    I do what Sparrow does.
    I used it for a golf ball flight/bounce/roll thing and for a breakout game using my terrain system.
     
  16. truthseeker089

    truthseeker089

    Joined:
    May 6, 2019
    Posts:
    61
    I am trying to learn raycast now guys for my bullet thanks alot!
     
  17. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    The approach that works regardless of whether you are using physics or not for movement is to raycast from the bullet's last position to it's current position. Other approaches are guesses and will have to deal with additional logic to compensate for under/over guessing. Unless you want projectiles hitting from meters away or not hitting at all.

    You simply can't reliably predict how far a projectile will travel in an update, whether it's Update or FixedUpdate.
     
  18. truthseeker089

    truthseeker089

    Joined:
    May 6, 2019
    Posts:
    61
    hi guys I hope you all are doing well...anyway, I was about do a raycast research when I stumbled upon a very good tutorial about Object Pooling. It was beginner friendly and I was able to recreate the tutorial on my own. However when I was about to apply it on my project, I couldn't do it, the bullet wouldn't instantiate since I replaced the instantiate with the Object pooler shared Instance. Kindly point out what I did wrong. Thanks all!!!

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class bulletspawn : MonoBehaviour {
    public GameObject bullet;
    public List<GameObject> bulletspawner; //
    public List<GameObject> activePlayerTurrets;

    public float scatterShotTurretReloadTime = 2.0f;

    void Start()
    {

    }

    void Update ()
    {

    if (Input.GetKeyDown ("Fire2")) {
    Shoot ();
    }
    }

    void Shoot() {
    foreach(GameObject turret in activePlayerTurrets) {
    GameObject bullet = objectpooler.SharedInstance.GetPooledObject ();
    if (bullet != null) {
    bullet.transform.position = turret.transform.position;
    bullet.transform.rotation = turret.transform.rotation;
    bullet.SetActive (true);

    }

    }
    }
    IEnumerator Activatebulletspawner ()
    {

    while (true) {
    foreach (GameObject turret in bulletspawner) {
    GameObject bullet = objectpooler.SharedInstance.GetPooledObject ();
    if (bullet != null) {
    bullet.transform.position = turret.transform.position;
    bullet.transform.rotation = turret.transform.rotation;
    bullet.SetActive (true);
    }
    }

    yield return new WaitForSeconds (scatterShotTurretReloadTime);

    {

    }
    }
    }
    }
     
  19. truthseeker089

    truthseeker089

    Joined:
    May 6, 2019
    Posts:
    61
    I was trying to instantiate a bullet from the object pool (20) to save memory however the bullet wouldn't spawn if I press Fire2....the turret isn't supposed to be there, it's part of the tutorial and I don't know how to replace it, it is attached to my bulletspawner as a child, there are 2 of them, in the tutorial there are 8..I'm not sure if Im allowed to post the link here.
     
  20. KouroshX98

    KouroshX98

    Joined:
    Feb 8, 2018
    Posts:
    24
    You can set Collision Matrix in physics setting.
    You give each of your object's group (e.x like players,bullets,walls) an specific layer.then in the setting you choose which layers collide with each other.if you want more help with this let me know. LayerCollisionMatrix.png
     
  21. truthseeker089

    truthseeker089

    Joined:
    May 6, 2019
    Posts:
    61
    thanks kourosh I did but it's kinda limited you can't use tags with it right? only layers and names...I think a scripted one has more flexibility?
     
  22. KouroshX98

    KouroshX98

    Joined:
    Feb 8, 2018
    Posts:
    24
    Sorry for late reply
    Certainly scripting gives more flexibility.
    But with good thinking and method you can easily modify the collision matrix to work the way you want
    Good Luck!