- Home /
show different textures when trigged entered thrice
i have a player ball and a enemy ball, which moves randomly around & when it hits the player the player should loose one of his live....and my prob is can we display three different GUITextures when the enemy ball hits the player for 3 times. Like my below score GUI Texture
#pragma strict
var score1 : GUITexture;
var score2 : GUITexture;
function Start()
{
score1.enabled = true;
score2.enabled = false;
}
function OnTriggerEnter (other : Collider) {
if(other.gameObject.tag == "Player")
{
score1.enabled = false;
score2.enabled = true;
}
}
yep, just add a counter to keep track of the currently used material, then cycle through them with a switch-case.
if you don't know about switch-case I can post an answer for you =]
I'd have just used an array, but a switch can be used to implement more complex behaviour, so that's good too!
below is the script :
var GUITex : GUITexture; // This is where you drop the main GUITexture component!
var images : Texture2D[]; // This is an array of images!
var currentImage : int = 0; // The index of the current texture. //Now, whenever the player gets hit, do something like this:
function OnTriggerEnter (other : Collider) {
if(other.gameObject.tag == "Player") { currentImage = $$anonymous$$athf.Clamp(currentImage++, 0, images.Length); // Advances the index! GUITex.texture = images[currentImage]; // Changes the image! }
}
@alucardj: thanks for the script its working....since i have two balls in the scene how can i tag them.....i mean now its like when one of the ball hits the player its displaying the data under case 1 & if the other ball hits the player its also displaying the data under case 1? how can i combine the both balls like if one ball hits the player it displays data under case 1 & even if other ball hits the player then it displays the data under case 2.
sry, all fixed now. (just wanted to follow up on my switch comment, also maybe wasn't confident enough to post as answer alongside one of the pro's!)
Answer by syclamoth · Jun 26, 2012 at 09:38 AM
What you have now is a setup where you can only have 2 things- for more, it will have to be a little more complex.
For starters, instead of hardcoding exactly two 'score' images, try keeping a collection of possible textures to switch your GUITexture between:
var GUITex : GUITexture; // This is where you drop the main GUITexture component!
var images : Texture2D[]; // This is an array of images!
private var currentImage : int = 0; // The index of the current texture.
Now, whenever the player gets hit, do something like this:
currentImage = Mathf.Clamp(currentImage++, 0, images.Length); // Advances the index!
GUITex.texture = images[currentImage]; // Changes the image!
This will move the image forward every time the player enters the trigger, if you use it in the same way you were using your old code.
$$anonymous$$ieran Wallace: thanks for the script....but its updating only once i mean even though we have added three textures its displaying only one.
Below is the code that i have used #pragma strict
var GUITex : GUITexture; // This is where you drop the main GUITexture component!
var images : Texture2D[]; // This is an array of images!
var currentImage : int = 0; // The index of the current texture. //Now, whenever the player gets hit, do something like this:
function OnTriggerEnter (other : Collider) {
if(other.gameObject.tag == "Player") { currentImage = $$anonymous$$athf.Clamp(currentImage++, 0, images.Length); // Advances the index! GUITex.texture = images[currentImage]; // Changes the image! }
}
can you pls suggest me the same script using switch case.
Thanks!!!
Answer by AlucardJay · Jun 28, 2012 at 06:42 PM
this is just an example of a switch-case, I am still beginning (and syclamoth's answer is very logical and compact).
private var currentImage : int = 0;
function OnTriggerEnter (other : Collider) {
if(other.gameObject.tag == "Something")
{
currentImage++;
switch(currentImage)
{
case 1 :
// do stuff for condition 1, eg tex = myTex1;
// and/or play a sound, eg audio.Play();
// or call a function, more than one thing can be added here, and it's better than clustered if's
break;
case 2 :
// do stuff for condition 2
break;
case 3 :
// do stuff for condition 3
// * end of the count of 3 is reached
// make image stay on case 3 (currentImage++; at start of trigger event makes 3),
// or just let the switch fall to default
currentImage = 2;
// or, cycle through again with
// currentImage = 0;
break;
default :
// have default settings if currentImage counter is under 1 or over 3, or see checks in case 3
// or do nothing and leave this part empty with just //
break;
}
}
}
ghostreddy : @alucardj: thanks for the script its working....since i have two balls in the scene how can i tag them.....i mean now its like when one of the ball hits the player its displaying the data under case 1 & if the other ball hits the player its also displaying the data under case 1? how can i combine the both balls like if one ball hits the player it displays data under case 1 & even if other ball hits the player then it displays the data under case 2.
you can get all sorts of information from the collider. my last example used :
if (other.gameObject.tag == "Something")
this can also be done with the GameObject's name :
if (other.gameObject.name == "Ball_1")
So each if statement and switch-case needs a counter, something like this :
private var counter1 : int = 0;
private var counter2 : int = 0;
function OnTriggerEnter (other : Collider) {
if (other.gameObject.name == "Ball_1")
{
counter1++;
switch(counter1)
{
case 1 :
//
break;
....
}
}
if (other.gameObject.name == "Ball_2")
{
counter2++;
switch(counter2)
{
case 1 :
//
break;
....
}
}
}
@$$anonymous$$ $$anonymous$$ay @alucardj: If we have applied below script even then it leads us to the same prob i guess....even though we are classifying the balls as 1 and 2 but if player hits ball1 code under case 1 will be loaded & when the player hits ball2 then also the code under case 1 will be loaded. here we need to tag both the balls like if player hits ball 1 then code under case1 should be executed and when the player hits ball2 or ball1 then case2 should be executed.
to make the player affected by either ball, you can have both Ball_1 and Ball_2 tagged as 'Ball', then just use :
if (other.gameObject.tag == "Ball")
or check for each object name in the conditional :
if (other.gameObject.name == "Ball_1" || other.gameObject.name == "Ball_2")
@alucardj: its working thanks.......is it possible to do something like this:
Since the enemy ball move randomly around the scene if enemy balls co$$anonymous$$g closure to player ball then we need to suggest the safe direction to the player ball by displaying the guitexture arrows like left,right,up,down like that......Eg: if enemy balls are moving closure to player ball in +z axis then the arrow should suggest the safe direction either left or right.