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!
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; } } }
Please use the code tags. https://forum.unity.com/threads/using-code-tags-properly.143875/ Also, this is quickly turning into a thread that should be in scripting, not general discussion.
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); } }
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.
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.
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.
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.
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.
I raytrace from current position to next position on fixed update(rb.velocity * fixedUpdate), eliminating the need for continuous collision detection
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.
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.
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); { } } } }
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.
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.
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?
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!