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 GuyFawkes · Aug 19, 2011 at 09:31 PM · characterinvisible

How do you make it when a character hits an object it turns invisible?

I've tried looking it up, but I haven't found anything that works. Please help! (Basically I just need the way to detect if a character has hit the object, and the object turning invisible would be nice for example :D)

EDIT I tried both of your scripts, and nothing happened when I touched the cube. Here's what I did by Aldonaletto. (I put it in the object named cube):

 function OnTriggerEnter(Hit : Collider)
 {
    if(Hit.tag == "Player") // you can compare tags instead: if (Hit.tag = "Player")
    {
        renderer.enabled = false; // renderer alone is the trigger renderer
    }
 }

Here's the one by DGArtistsInc, I put it in my character:

 function OnTriggerEnter(Hit : Collider)
 {
    if(Hit.gameObject.Find("Cube"))
    {
        gameObject.Find("Cube").renderer.enabled = false;       
    }
 }

I think the problem might be that possibly there's a character controller in the player. Also, I imported the mesh from Blender, and it has armatures in it. What's my problem here?

EDIT What's wrong with this script?

 function OnControllerColliderHit()
 {
   if(Hit.gameObject.Find("Cube"))
   {
     gameObject.Find("Cube").renderer.enabled = false;       
   }
 }

I know that it's probably because of there's no parameters in the function.

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 Joshua · Aug 22, 2011 at 01:45 AM 0
Share

your script is missing a parameter and the Find stuff makes no sense.

 function OnControllerColliderHit( hit : ControllerColliderHit)
 {
     if( hit.gameObject.CompareTag( "Turns invisible" ) )//some test here, so not every thing will always turn invisible
         hit.gameObject.renderer.enabled = false;
 }
avatar image GuyFawkes · Aug 22, 2011 at 02:06 AM 0
Share

Thanks Joshua, that worked! Thanks so much!

4 Replies

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

Answer by DGArtistsInc · Aug 21, 2011 at 01:52 AM

I guess you could try instead of OnTriggerEnter you could try OnControllerColliderHit.

 function OnControllerColliderHit()
 {
   if(Hit.gameObject.Find("name of your gameobject here"))
   {
      gameObject.Find("name of your gameobject here").renderer.enabled = false;       
   }
 }

put this on your character. If that doesnt work i dont know whats wrong.

PS you dont have to make the cube isTrigger if you use this.

Comment
Add comment · Show 6 · 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 GuyFawkes · Aug 22, 2011 at 01:25 AM 0
Share

I think yours may work, but it gives the error: Assets/Scripts/Onhit.js(3,12): BCE0005: $$anonymous$$ identifier: 'Hit'.

I know it's because there's no perameters in the function, but I'm not sure what to add. Thanks in advance to everyone.

Oh yeah this should be in the cube right?

avatar image DGArtistsInc · Aug 22, 2011 at 01:36 AM 0
Share

Oh man my bad i forgot in the parameters put Hit so it should look like this

function OnControllerColliderHit(Hit)

avatar image Joshua · Aug 22, 2011 at 01:37 AM 0
Share

his first line should read

 function OnControllerColliderHit( hit : ControllerColliderHit )
avatar image DGArtistsInc · Aug 22, 2011 at 01:40 AM 0
Share

yes i just corrected my self in the last comment thanks :)

avatar image GuyFawkes · Aug 22, 2011 at 01:49 AM 0
Share

I know this code's format is going to be messed up, but I used this code: function OnControllerColliderHit( Hit : ControllerColliderHit ) { if(Hit.gameObject.Find("Cube")) { gameObject.Find("Cube").renderer.enabled = false;
} }

I put that code in the character, and as soon as the player took one step forward the cube disappeared. Why is this?

Show more comments
avatar image
4

Answer by Ziad · Aug 20, 2011 at 11:50 PM

u have to put rigidbody component on ur player and toggle Use Gravity to false and Is Kinematic to true and make the collider of the other object trigger ... and put the script on ur player

 function OnTriggerEnter(Hit : Collider)

{ if(Hit.tag == "the name of the gameObject") {

    renderer.enabled = false;
 

}

}

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 GuyFawkes · Aug 21, 2011 at 12:04 AM 0
Share

I did your way, using the script: function OnTriggerEnter(Hit : Collider) { if(Hit.gameObject.Find("Cube")) { gameObject.Find("Cube").renderer.enabled = false;
} }

How would I do it with a script in the cube? What would I need to change? The script in the character doesn't seem efficient.

avatar image
1

Answer by aldonaletto · Aug 20, 2011 at 02:46 PM

The first two lines are completely unnecessary. The parameter Hit is a Collider, and allows direct access to most important object variables (properties), like name, tag, transform, renderer etc. If the cube is the trigger, and you want it to become invisible when touched by the player, attach this to the cube:

function OnTriggerEnter(Hit : Collider)
{
   if(Hit.name == "Player") // you can compare tags instead: if (Hit.tag = "Player")
   {
       renderer.enabled = false; // renderer alone is the trigger renderer
   }
}
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 GuyFawkes · Aug 20, 2011 at 11:32 PM 0
Share

I tried it, and it didn't work. Look in the question on what I thought the problem might be.

avatar image aldonaletto · Aug 21, 2011 at 05:50 AM 0
Share

The CharacterController is detected by triggers - but you must use $$anonymous$$ove or Simple$$anonymous$$ove to move it: translate or position modification sabotages its detection. Another possibility is some name mismatch: verify if your character is actually named "Player" (names and tags are case sensitive). You can also omit the if clause and add some debug instruction like print("trigger entered"); inside OnTriggerEnter to help finding the error.

avatar image
-1

Answer by DGArtistsInc · Aug 19, 2011 at 10:27 PM

 function OnTriggerEnter(Hit : Collider)
 {
    if(Hit.gameObject.Find("name of your gameobject here"))
    {
        gameObject.Find("name of your gameobject here").renderer.enabled = false;       
    }
 }

Try this then. I have no idea why the last one didnt work but this should work.

Comment
Add comment · Show 7 · 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 GuyFawkes · Aug 20, 2011 at 01:01 AM 0
Share

Tried the code in that I edited in to my original question.

Got the error "Assets/Scripts/onhit.js(7,11): BCE0019: 'object' is not a member of 'UnityEngine.Collider'."

avatar image DGArtistsInc · Aug 20, 2011 at 02:32 PM 0
Share

i just edited it. Tell me if there are any errors

avatar image GuyFawkes · Aug 20, 2011 at 11:32 PM 0
Share

Yeah, unfortunately it didn't work. Please look at my latest edit in my question.

avatar image Joshua · Aug 22, 2011 at 01:39 AM 0
Share

The if statement makes no sense whatsoever.

avatar image DGArtistsInc · Aug 22, 2011 at 01:42 AM 0
Share

Joshua could you explain?

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

7 People are following this question.

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

Related Questions

Renderer on object disabled after level reload 1 Answer

Character turns invisable on build 0 Answers

3rd person shooter randomly goes invisible 0 Answers

Character Rotation 1 Answer

Character-Only Shadows 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