How Can I make a plane only visible when steped on?
I have an invisible platform that I only want to be visible when the player is standing on it. I then want it to turn invisible again when the player steps off. I prefer C# and am developing a 3d game (you can tell, as the tittle mentions the "plane" object)
I have tried many scripts but: my coding skills < a baby's
Edit: It seems that the FPSController does not work with OnCollisionEnter?
Answer by MeroDN · Oct 31, 2015 at 06:18 PM
I decided to add a trigger colider to the pllanes and used this script I came up with: using UnityEngine; using System.Collections;
public class See_if_touching : MonoBehaviour
{
private bool UnderPlayer = false;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
UnderPlayer = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
UnderPlayer = false;
}
}
void Update()
{
if (UnderPlayer)
{
GetComponent<MeshRenderer>().enabled = true;
}
else
{
GetComponent<MeshRenderer>().enabled = false;
}
}
}
Answer by ElDo · Oct 31, 2015 at 09:06 AM
A script like this could do the trick (not tested):
using UnityEngine;
using System.Collections;
public class InvisiblePlatform : MonoBehaviour {
int _numOccupants=0;
void OnCollisionEnter(Collision collision){
_numOccupants++;
foreach(Transform t in transform){
t.GetComponent<Renderer>().enabled=true;
}
}
void OnCollisionExit(Collision collsion){
_numOccupants--;
if(numOccupants==0){
foreach(Transform t in transform){
t.GetComponent<Renderer>().enabled=false;
}
}
}
}
attach this to the platform. As soon as something collides with it all renderers in all transform will be enabled and as soon as nothing's left on the platform it will be disabled again. It Counts the number of objects that enter the platform to make it back invisible as all have left it so it won't get invisble as still someone stays on it.
try using GetComponentsInChildren<Renderer>()
ins$$anonymous$$d (reference).
using UnityEngine;
public class InvisiblePlatform : $$anonymous$$onoBehaviour
{
int numOccupants = 0;
public void Enter()
{
if (numOccupants++ == 0)
ShowPlatform(true);
}
public void Exit()
{
if (--numOccupants == 0)
ShowPlatform(false);
}
void Awake()
{
ShowPlatform(false);
}
void OnCollisionEnter()
{
Enter();
}
void OnCollisionExit()
{
Exit();
}
void ShowPlatform(bool show)
{
foreach (var renderer in GetComponentsInChildren<Renderer>())
renderer.enabled = show;
}
}
Your answer
Follow this Question
Related Questions
Double Jump using character controller 0 Answers
Wall Jump with Jump Delay 0 Answers
How can I Wall Jump with a Delay 1 Answer
Struggling with bounce pad in platformer game. 1 Answer
How to hide and show level based on player/camera position? 0 Answers