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 RexoniuM · Dec 27, 2012 at 01:07 PM · collisionobjecttag

Move object to some random position once it gets in collision

I need my object to move once it gets in collision with another object, denoted and tagged as enemy. I am having this js script, however it won't work, once I get it on my object which I tend to be moving. What may be the cause of this issue? Thanks in advance for your answers.

 private var enemyTag: String = "Enemy";
 
 function OnCollisionEnter(collision : Collision){
      if (collision.gameObject.tag == enemyTag){
         this.transform.position = Vector3(Random.Range(0, 100),0, Random.Range(0, 100));
         
      }
 
 }
Comment
Add comment · Show 5
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 AlucardJay · Dec 27, 2012 at 02:45 PM 0
Share
 function OnCollisionEnter(collision : Collision){
     Debug.Log( "collision.gameObject.tag = " + collision.gameObject.tag );
 // if .... the rest

what does this print out in the console ?

avatar image RexoniuM · Dec 27, 2012 at 02:55 PM 0
Share

I have tried it before, and it doesn't detect collision at all even though I have set up sphere collider and set trigger on true.

avatar image AlucardJay · Dec 27, 2012 at 03:00 PM 0
Share

Well, that would be the problem then. This should work for any collision. As soon as it happens, the script is saying "hey, what did I hit?", so if there is no debug in the console then the colliders are not set up correctly.

Oh, if you are using a trigger, then you want OnTriggerEnter : http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html

Note that trigger events are only sent if one of the colliders also has a rigidbody attached.

For non-trigger collision : http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html

Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached.

You need to work on your objects, get that debug printing in the console!

avatar image RexoniuM · Dec 27, 2012 at 03:06 PM 0
Share

Yes, I know for those scenarios, however this isn't working ...

avatar image AlucardJay · Dec 27, 2012 at 03:12 PM 0
Share

Dude, its simple. You have a collider, and you want to check for collisions. Either you are checking for collision, or trigger. The rules for both are above. For either to work, you need to have a rigidbody attached. It's all there ....

Watch : http://www.unity3dstudent.com/2010/07/beginner-b01-basic-collision-detection/

and : http://www.unity3dstudent.com/2010/07/beginner-b13-trigger-collision-detection/

then watch them all (start at the bottom and work up) : http://www.unity3dstudent.com/category/modules/

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ben Ezard · Dec 27, 2012 at 01:57 PM

You should use the "Equals" method when comparing strings, so private var enemyTag: String = "Enemy";

function OnCollisionEnter(collision : Collision){
if (collision.gameObject.tag.Equals(enemyTag)){
this.transform.position = Vector3(Random.Range(0, 100),0, Random.Range(0, 100));
}
}

Comment
Add comment · Show 1 · 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 RexoniuM · Dec 27, 2012 at 02:35 PM 0
Share

Nah, still the same...

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

10 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

Related Questions

Destroy All of One One object on collision 1 Answer

OnCollision Script... 2 Answers

Turning an object into a RigidBody and applying a force on it... 2 Answers

A node in a childnode? 1 Answer

Fast moving object passing through other objects. 6 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