Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 bobthejuser · Jun 15, 2012 at 01:08 AM · collisionphysicsbeginnerignorecollision

Need help ignoring collision

Earlier today, I asked for help regarding how to avoid collision between two instantiated gameobjects.

With my limited understanding on programming, perhaps a question on the general use of it was a bit over my head.

Instead, please help me by looking at my code, poor as it may be, and help me with my problem.

I need to make Missile and Enemy ignore collision with each other.

 using UnityEngine;

using System.Collections;

public class EnemyBehaviour : MonoBehaviour {

 public GameObject Missile;
 public GameObject Player;
 public float enemySpeed;
 private int health;
 private float time;
 private int shotFired;
 private float shotTimer;
 
 void OnCollisionEnter(Collision target)
 {
     if(target.transform.name == "Bullet(Clone)")
     {
         health--;
     }
 }
 
 // Use this for initialization
 void Start () {
 health = 3;
     time = Time.deltaTime;
     shotFired = 0;
 }
 
 // Update is called once per frame
 void Update () {
     
     if(PlayerBehaviour.playerHeight - transform.position.y >=0.3)
     {
     transform.Translate(-15f * time,10f * time,0);
     }
     else if(PlayerBehaviour.playerHeight - transform.position.y <=-0.3)
     {
     transform.Translate(-15f * time,-10f * time,0);
     }
     else
     {
     transform.Translate(-15f * time,0,0);
     }
     
     shotTimer += 1 * Time.deltaTime;
     
     if(shotTimer >= 0.4 && shotFired == 0)
     {
         
         shoot();
         
     }
     
     if(transform.position.x < -40)
     {
         Destroy(gameObject);
     }
     
     if(health<=0)
     {
         Destroy (gameObject);
     }
 
 }
 
 void shoot()
 {
 
     if(shotFired == 0)
     {
     
     Instantiate(Missile, new Vector3(transform.position.x+3,transform.position.y,transform.position.z),Quaternion.identity);

         
     shotFired = 1;    
     }
 }

}

That is for the Enemy

 using UnityEngine;

using System.Collections;

public class MissileBehaviour : MonoBehaviour {

 private float heightSpeed;
 private int directionSet;
 private float fireTime;
 
 
 void OnCollisionEnter(Collision other)
 {
     if(other.transform.name == "Ship")
     {
     Application.LoadLevel("Menu");
     }
 }
 
 // Use this for initialization
 void Start () {
     
     directionSet = 0;
     
 }
 
 // Update is called once per frame
 void Update () {
 fireTime += 1 * Time.deltaTime;
     
 if(PlayerBehaviour.playerHeight != 0)
     {
     
     if(directionSet == 0)
     {
         
         heightSpeed = -((PlayerBehaviour.playerHeight - transform.position.y)/(PlayerBehaviour.playerX - transform.position.x-2));
         directionSet = 1;
         
     }
     
     if(fireTime >= 1.0)
         {
             transform.Translate (-100f*Time.deltaTime,heightSpeed*100*Time.deltaTime,0);
         }
         
     }
     
 }

}

That is for the missile

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by rutter · Jun 15, 2012 at 04:57 AM

Unity's physics engine allows you to set a layer for each GameObject. It's also possible to set flags to control which layers will collide with each other. You can read more about this in the Unity manual. If you're spawning with prefabs, you can set the prefab's layer using the inspector. If it's easier in your case, you could also set a bullet's layer right after spawning it, from script.

If you only need to ignore collision between two specific colliders -- say, a bullet and the character who shot it -- you can call `Physics.IgnoreCollision()` to ignore collisions between any two colliders. For this method, you'll have to do it in script after creating the bullet.

Maybe this is still more general than you're looking for. Is there some specific part of this process that's giving you trouble?

Comment
Add comment · Show 2 · 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 bobthejuser · Jun 15, 2012 at 05:13 AM 0
Share

Thank you for answering.

Yes, this is still a bit too general for me.

I understand that Physics.IgnoreCollision() will do what I need, but the problem is that I don't know how to use it.

This might just be me who is a total noob at Unity, but I don't know how to find the collider of the missile I'm shooting.

For instance,

Physics.IgnoreCollision(missile.collider,enemy.collider);

From what I understand, I must first find the GameObject of the missile and the enemy, but since both of them are prefabs, I don't know how to do so.

Up until now, whenever I've had to use another GameObject I would manually find it in the script and then find the specific GameObject in my scene.

This is not possible to do with instantiated GameObjects however, so how do I find these?

TL;DR:

How do I find the GameObjects of something instantiated and use said GameObjects collider in Physics.IgnoreCollision()

avatar image bodec · Jun 15, 2012 at 05:18 AM 0
Share

that is a question for another thread uyour question has been answered for you and should be marked as answered, on the side note to save you time filling out another question you need to set a var to the game object getComponet collider then use it in the Physics.IgnoreCollision function.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Having some trouble with physics when creating a Shufflepuck Cafe clone (slidding and hitting the puck on a plane) 0 Answers

How do I make a collision detector for the parent which effects the children? 1 Answer

Rigidbody Collision Question 1 Answer

Ignore Collision problem 1 Answer

How do I ignore collision between two instantiated objects. 1 Answer


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