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 stingman · Jan 26, 2012 at 08:43 PM · collisiondestroycharacter controllerbooleans

Help in Javascript destroying Objects using character controllers and conditionals

Ok so I know I'm on the right track... I just know I need some help with some coding or someone to point me in the right direction. Here is the situation: Both players labeled as player 1 and player 2 are using character controllers to control them and when one is active the other is inactive (characters change when collecting a powerup). They are rigidbodies and the enemy is a rigidbody as well. When player one collides with the enemy (who has a trigger attached to him) I want player one to be destroyed and when player 2 collides with the enemy I want the enemy to be destroyed. Here is the code I'm working with... I know it's wrong but that's why I'm reaching out :)

private var enemy : GameObject;

private var player1: GameObject;

private var player2: GameObject;

function OnControllerColliderHit(hit: ControllerColliderHit){

enemy = GameObject.FindWithTag("enemy");

player1 = GameObject.FindWithTag("Player1");

player2 = GameObject.FindWithTag("Player2");

while (player1.active = true) {

if (hit.rigidbody){

Destroy (player1);

}

}

while (player2.active = true) {

if (hit.rigidbody) {

enemy.SetActiveRecursively(false);

} } }

I know the while (player1.active = true) is a boolean and this cannot work but I left it in there to show you what i'm trying to accomplish. I just don't know what to use at this point in the script. Thank you for any help or tips :)

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 FLASHDENMARK · Jan 26, 2012 at 08:46 PM 0
Share

While loops will work with booleans as well, but using while loops in this context is not to efficient why not just use a if(player.active)? ins$$anonymous$$d of a while loop?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FLASHDENMARK · Jan 26, 2012 at 08:52 PM

 private var enemy : GameObject;
 private var player1: GameObject;
 private var player2: GameObject;
 
 function OnControllerColliderHit(hit: ControllerColliderHit){
     if(hit.rigidbody){
         if(player1.active)
             Destroy(player1);
         if(player2.active)
             enemy.SetActiveRecursively(false); 
     }
 }

There is no reason to use a while loop(use simple if statements instead), or check the same condition twice with only different "sub-conditions".

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 stingman · Jan 26, 2012 at 09:17 PM 0
Share

I thought that gameObject.active is a boolean? Therefore it is only used to declare true or false. e.g. player1.active = true;

I declare whether player 1 is true or false in the powerup script however the code you suggested for this script stops the game whenever either character collides with the enemy. Thank you for the tips to get rid of the "while" statement... one more step in the right direction! However if you have any more ideas to get this working I'm very grateful. Thanks!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Trouble with raycast hitting a tagged object 0 Answers

How to only delete one of two collided objects? 1 Answer

Destroying all objects with a certain tag that ray cast if colliding with 0 Answers

Collision between 2 prefabs. 0 Answers

How to destroy bullet on collision! 2 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