Small Giveaway Scripts

Hello.

I guess I have receved so much from this forum that it would be nice to give back some. This are very simple scripts whcih I reuse a lot and might come in handy. One is an aspect correction script and another is a action to make a guilayer fade in and out when a certain button is pressed.

//Aspect Correction Script for Unity - Zerofractal 2006
//This script can be placed on any GUI element to make its aspect correct to 1:1.  
//The scale is based on the height of the element.  
//It works with textures of equal or unequal width and height, such as 1024x512 maps.  
//Unlike other scripts, this one takes the aspect from the actual screen making it useful for 4:3 16:9 etc etc.

function Awake() {
	RefreshAspect();
}

function RefreshAspect() {
	var aspect: float = 1/Camera.main.aspect;
	transform.localScale.x = transform.localScale.y * aspect * guiElement.texture.width/guiElement.texture.height;
	transform.position = transform.localPosition;
}

The other is a FadeIn Script:

var buttonName="Jump";
var fadeDuration:float=0.5;
var initialDelay:float=5;
private var timeLeft:float=0.5;

function Awake () {
	timeLeft = fadeDuration;
}

function Update () {
	if (initialDelay > 0){
		initialDelay = initialDelay-Time.deltaTime;
	} else {
		if (Input.GetButton (buttonName))
			fade(true);	
		else
			fade(false);
	}
}

function fade(direction:boolean){
	var alpha;
	if (direction){
		if (guiElement.color.a < 0.5){
			timeLeft = timeLeft - Time.deltaTime;
			alpha = (timeLeft/fadeDuration);
			guiElement.color.a=0.5-(alpha/2);
		} else {
			timeLeft = fadeDuration;
		}
	} else {
		if (guiElement.color.a > 0){
			timeLeft = timeLeft - Time.deltaTime;
			alpha = (timeLeft/fadeDuration);
			guiElement.color.a=alpha/2;
		} else {
			timeLeft = fadeDuration;
		}
	}
}

Neat. Is there any reason these can’t go on the wiki? It would be nice to have a single place to access this stuff, so people can appreciate it and modify it more easily :).

Good Idea! I’ll post on the wiki later.

Done.

Thanks a bunch, zero!

I don’t suppose that while you’re in the sharing mood, that you could share your secrets to the rotating select menu that you’re using in your Prism Menu?

Pretty please? :lol:

If not, that’s totally cool.

Well the rotating menu is not that much of a secret. In fact is very simple.

A water plane, a noise script to give some nice mood to the camera, and a script that rotates the menu. Beware, I’m not that much of a programmer. But it basically adds a state variable (rotating), a current angle (ang), a ang Interval, which is derived from 360 degrees/the number of items.

It was a pain in the ass to put together, the script goes something like this…:

function Update () {
	if (not rotating) {
		if (Input.GetAxis ("MenuNav") > 0.25){
			direction =  true;
			if ((ang + angInterval) == 360){
				ang = 0;
			} else {
				ang = ang + angInterval;
			}
			rotating = true;
		} 
		if (Input.GetAxis ("MenuNav") < -0.25) {
			direction =  false;
			if ((ang - angInterval) == angInterval*-1){
				ang = 360-angInterval;
			} else {
				ang = ang - angInterval;
			}
			rotating = true;
		}
		if (Input.GetButton ("MenuChoose")) {
			Application.LoadLevel ((ang/angInterval)+2);
		}
	}

	if ((rotating)  (itemTextures.Length > 1)){	
		var newAng;
		if (direction){
			newAng = zfMenu.transform.localEulerAngles.y + Time.deltaTime*speed;
			if ((newAng < ang) || (ang == 0  newAng >(360-angInterval))){
				zfMenu.transform.Rotate(0, Time.deltaTime*speed, 0);
			} else {
				rotating = false;	
				zfMenu.transform.localEulerAngles.y = ang;
			}
		} else {
			newAng = zfMenu.transform.localEulerAngles.y  - Time.deltaTime*speed;
			if ((newAng < 0)  (ang != 0)) {
				newAng = 360 -Time.deltaTime*speed;
			}
			if ((newAng > ang)) {
				zfMenu.transform.Rotate(0, Time.deltaTime*speed*-1, 0);
			} else {
				rotating = false;	
				zfMenu.transform.localEulerAngles.y = ang;
			}
		}
	} 
}

:wink:

Cool! Thank you very much.

Dont forget to play and have fun with different skyboxes and water materials!

here are some images of what can be achieved: