Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unity_19borkcdr · May 13, 2019 at 03:34 PM · bulletwallthroughtravel

How can I get a bullet prefab to travel through an invisible wall

Hey, so I am new to unity and right now I am using a simple AI type code for an enemy, I am making a Mario like game, that bounces between two set points(walls) that I have set up with box colliders. I then have to make the colliders trigger to get my character to move through and I know that, however with how I have the bullet code set up the bullet will go right to the colliders and destroy itself, here is the code for the bullet itself

 public class Bullet : MonoBehaviour
 {
     public LayerMask immaterial;
     public float speed = 20f;
     public int damage = 40;
     public Rigidbody2D rb;
     private GameObject LeftWall;
     // Start is called before the first frame update
     void Start()
     {
         // Velocity of the bullet, right instead of forward for 2D.
         rb.velocity = transform.right * speed;                          
     }    
     // If the bullet hits anything destroy the bullet, if it hits enemy then enemy takes damage.
     private void OnTriggerEnter2D(Collider2D hitInfo)
     {
         Enemy enemy = hitInfo.GetComponent<Enemy>();
         if (enemy!= null)
         {
             enemy.TakeDamage(damage);
         }
        
         Destroy(gameObject);
     }
 }

I used the Immaterial to try and fix what I thought would have been a layer issue. It was not. Any insight as to how I could work around this or if I would have to change to a raycast setup for my bullet or what I am supposed to do as I have been looking for about three hours total on this with every search giving me the exact opposite of what I need.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tormentoarmagedoom · May 13, 2019 at 03:43 PM 0
Share

HEllo.

I dont get exactly what you want or need...

By defaut, objects can move through everything. IF not, is because 2 nontriger collider are "impacting"

avatar image DCordoba · May 13, 2019 at 05:17 PM 0
Share

ummm learn about layers and ignoring collision with certain layers I think there is what you need, in performance and easy solution

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by BondoGames · May 13, 2019 at 09:49 PM

@unity_19borkcdr At the start of your OnTriggerEnter2D function, add this:

 if(hitInfo.isTrigger)
         {
             return;
         }

This will make the bullet check if what it touched was a trigger, and if it is it'll ignore it.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Cornelis-de-Jager · May 13, 2019 at 09:55 PM

Disable collision between the wall and bullet

 using UnityEngine;
 using System.Collections;
 
 public class ExampleClass : MonoBehaviour
 {
     public Transform bulletPrefab;
 
     void Start()
     {
         Transform bullet = Instantiate(bulletPrefab) as Transform;
         Physics.IgnoreCollision(bullet.GetComponent<Collider>(), GetComponent<Collider>());
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

108 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Bullet moves fast and makes a late collision.. 3 Answers

Gun through wall. [please read I don't want layering] 1 Answer

Going through wall 2 Answers

object goes through walls(no, dats nnot what r u thinkig about) 0 Answers

Detect if GameObject is Touching an Imported Level 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges