- Home /
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.
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;
}
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.
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?
Oh man my bad i forgot in the parameters put Hit so it should look like this
function OnControllerColliderHit(Hit)
his first line should read
function OnControllerColliderHit( hit : ControllerColliderHit )
yes i just corrected my self in the last comment thanks :)
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?
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;
}
}
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.
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 } }
I tried it, and it didn't work. Look in the question on what I thought the problem might be.
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.
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.
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'."
Yeah, unfortunately it didn't work. Please look at my latest edit in my question.
Your answer
Follow this Question
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