- Home /
making object visible/invisible
I am making a digging game with a lot of cubes. I want to somehow destroy cubes that are out of certain range of player, and make them again when they are in certain range from player . But the problem appears here: How do I make them appear exacly the same if player dig out some of them. I am doing this because the game will be ported to andriod and iOS someday, and fps is quite low even on my computer. thank you for your time
Answer by clunk47 · Dec 30, 2013 at 08:53 PM
I wouldn't destroy them, I'd just set them active or inactive. Have a look at Vector3.Distance, and GameObject.SetActive.
no, no, no. You were right. :) I attached script to gameObject which set itself inactive. I attached it on empty gameObject and cngae script to
#pragma strict
var player : Transform;
var chunk1 : GameObject;
function Update () {
var dist : float = Vector3.Distance(chunk1.transform.position, transform.position);
if(dist >= 50){
chunk1.SetActive (false);
} else if (dist <= 50){
chunk1.SetActive (true);
}
Debug.Log(dist);
}
great information here! I'dd change something else to your code: Calculating the distance on each update with ALL of your chunks every frame might become heavy by itself if you end up with hundreds of chunks. You might want to calculate with this ins$$anonymous$$d: Physics.OverlapSphere I personally started using this ins$$anonymous$$d of distance and it helps a lot of FPS
@Gr$$anonymous$$I Can you plaese tell me more of how do you use it. #pragma strict
var player : GameObject;
var chunk1 : GameObject;
function Update () {
Physics.OverlapSphere(Vector3(player.transform.position.x, player.transform.position.y, player.transform.position.z), 30.0); // I guess that this will create sphere at players position with radius of 30
if(if there is something in spheres radius){
Debug.Log("there is something in spheres radus");
}
}
I'll try to help, but know first that I am myself a early user in Unity & just learned to code with it in C#. So Ill help as I can in C# - youll have to translate that in .js pragma strict (it is very look alike)
GameObject player; //declare your player here
Collider[] objectsInRange; //all colliders in radius
float radius = 30f; //this is the distance between your player and your chunks that we want to find
void OnCollisionEnter (Collision col)
{
objectsInRange = Physics.OverlapSphere (player.transform.position, radius);
foreach (Collider chunk in objectsInRange )
{
if (chunk.gameObject.tag == "Chunk")
{
debug.log ("I am chunk: " + chunk.gameObject.name);
}
}
}
this implies thus that your chunks must: - have a collider - have a tag "Chunk"
I hope this helps
Answer by azmat786n · Dec 30, 2013 at 08:54 PM
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnBecameVisible.html
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnBecameInvisible.html
these are helpful for you..
Your answer
Follow this Question
Related Questions
how to make player character visible after respawn. Parent with children. 0 Answers
Bullet wont collide for the other player (PHOTON+UNITY) 1 Answer
How can I Instantiate and do it in a random spot in a random spot? 2 Answers
Problem with Random.Range 1 Answer
Detect if player is in range? 0 Answers