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 MrZalib · Jan 25, 2014 at 11:52 PM · c#ontriggerenteroncollisionenter

onCollisionEnter vs onTriggerEnter

I'm finding it difficult to decide which one to use and what triggers such events. I have looked at the API reference for both methods extensively and I've done some testing and I've got some confusing results.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerShot : MonoBehaviour 
 {
     public int speed;
 
     void Start () 
     {
         speed = 15;
 
     }
 
     void Update () 
     {
         transform.Translate (0, 1 * speed * Time.deltaTime, 0);
     }
 
     public void onCollisionEnter()
     {
         Debug.Log("onCollisionEnter() called");
         
         Destroy(gameObject);
     }
 }


This is my script that is attached to the "bullet" that gets fired. As I understand it, you only need a rigidbody attached to both objects involved yes? I have a empty game object that is off screen with a box collider attached to it. When the "bullet" passes through the collider it should trigger the method correct? Mine just pass through. Is there something I'm doing wrong here? Or should I be using onTriggerEnter instead?

Comment
Add comment · Show 1
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 getyour411 · Jan 25, 2014 at 11:57 PM 0
Share

Your rigidbody have is$$anonymous$$inesmatic checked?

1 Reply

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

Answer by superluigi · Jan 26, 2014 at 12:20 AM

From my understanding Translate means that it'll ignore collisions. That is your problem. To give you an example if you Translated your character down he would go through the floor. So

 function FixedUpdate()
 {
  rigidbody.velocity = Vector2(0, speed, 0);
 }

If it has a rigidbody you should put it in FixedUpdate. You don't need to multiply by Time.deltaTime because it runs at the same time step as FixedUpdate. However, when working with rigidbodies usually you want realistic physics and manipulating the velocity directly like I just did will lead to unrealistic physics. It will work exactly like translate did though and if you were happy with that then this will work for you. If any part of my answer confused you (maybe some of the terms) let me know in a comment and I will explain it.

Comment
Add comment · Show 8 · 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 superluigi · Jan 26, 2014 at 12:23 AM 0
Share

Also no you only need ONE rigidbody attached to one of the objects. If both have it a rigidbody that'll work too, the general rule is that at least one of the objects should have a rigidbody that is NOT kinematic.

avatar image MrZalib · Jan 26, 2014 at 01:32 PM 0
Share

You have been very helpful and I'll try some stuff out today. I'm doing a Galaga style game so I'm just concerned about how fast the "bullet" is traveling. The collision comes into play because I'm destroying the object the moment it goes off screen and hits the box collider. Perhaps there's a better way to implement this? I have yet to add in the condition of what happens if the bullet hits another ship but I would assume its very similar. Thank you.

avatar image superluigi · Jan 26, 2014 at 10:50 PM 0
Share

Are you by any chance using the 2Drigidbodies that Unity just added. If you are then there is another reason the collisions aren't being detected but it's an easy fix. If you are using regular rigidbodies then everythings ok. If you are doing a galaga type game the best thing in my limited experience is to have your ship stay in one spot and have the background move down or to the sides to give off the appearance that the ship is moving. In these situations a collider/trigger is definitely a good idea because everything is working in a confined and controlled space. Also if my answer is correct please mark it as correct. I've only had 2 answers marked as correct and I want more.

avatar image MrZalib · Jan 27, 2014 at 03:19 PM 0
Share

Your code worked somewhat. I think I had to tweek it a little to get the effect I was looking for but overall it steered me in the correct direction. As far as movement goes well that's a little tricky because I want it set up like a game called Gryus (I think that's how you spell it) If you google it you can see that the movement and the way the game is set up is a little bit tricky.

avatar image MrZalib · Jan 27, 2014 at 04:49 PM 0
Share

Would the code that you have posted for moving a rigidbody work on the 2d version? I don't have 2d rigidbodies on my objects. Is there a difference?

Show more comments

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

19 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

Related Questions

How to make gameObject make only one collision 1 Answer

Nav Mesh Agent para perseguir a un clon 1 Answer

OnCollisionEnter() not getting called between two rigid bodies 3 Answers

Collider doesn't transfer to function 1 Answer

On Trigger Enter, Collide with object, specific collision 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