- Home /
triggering 3DText by collision
Hello Y'all
here is a Text Game:
I have 3DText sandwiched between 2 Planes (1&2)
I made it into a prefab and will line a few of them up like dominos for a Camera to move through on a path ...So words appear as you scroll forward along the Z Axis Triggered by colliding with the Planes (fade in & out the Text based on a boolean)
but I am having problems with the semantics of the .js code that I am attaching to the Prefab
#pragma strict
// var to access Text mesh
var myText : TextMesh;
//switch text Mesh on and off
var mySwitch : boolean = false;
function Start () {
}
// Fade in myText
function fadeIn () {
Fade.use.Alpha(myText.renderer.material, 0.0, 1.0, 1.0, EaseType.In);
}
// Fade out myText
function fadeOut () {
Fade.use.Alpha(myText.renderer.material, 1.0, 0.0, 1.0, EaseType.Out);
}
//when Camera(or FPC) collides with Plane_1
// Fade in or out depending on switch
function onCollision ("plane_1") {
if mySwitch = false {
fadeIn;
mySwitch = true;
}
else mySwitch = true {
fadeOut;
mySwitch = false;
}
}
//when Camera(or FPC) collides with Plane_2
// Fade in or out depending on switch
function onCollision ("plane_2") {
if mySwitch = false {
fadeIn;
mySwitch = true;
}
else mySwitch = true {
fadeOut;
mySwitch = false;
}
}
Any help is greatly appreciated !!
~be
Answer by Bentoon · Jun 20, 2014 at 09:54 PM
Hey Landem Thank you So much for your time, my original way of thinking was problematic
I found and manipulated a script that allows me only to work with the text based on Position
See below:
#pragma strict
var fadeIn : boolean;
var fadeOut : boolean;
var fadeSpeed : float = 0.01;
var minAlpha : float = 0.0;
var maxAlpha : float = 1.0;
var minDistance = 3;
var target : Transform;
var myTransform : Transform;
function Awake()
{
myTransform = transform;
}
function Start()
{
target = GameObject.FindWithTag("Player").transform;
}
function Update()
{
if(fadeIn && !fadeOut)
FadeIn ();
if(fadeOut && !fadeIn)
FadeOut ();
if(renderer.material.color.a <= minAlpha)
{
fadeOut = false;
if(Vector3.Distance(myTransform.position, target.position) < minDistance)
{
fadeIn = true;
}
}
if(renderer.material.color.a >= maxAlpha)
{
fadeIn = false;
if(Vector3.Distance(myTransform.position, target.position) > minDistance)
{
fadeOut = true;
}
}
}
function FadeIn()
{
renderer.material.color.a += fadeSpeed;
}
function FadeOut()
{
renderer.material.color.a -= fadeSpeed;
}
Answer by Landern · Jun 20, 2014 at 12:27 PM
Add a collider to the 3d text.
Check for collision, in the case below it's OnCollisionEnter, but you can check for other states.
#pragma strict
// var to access Text mesh
var myText : TextMesh;
//switch text Mesh on and off
var mySwitch : boolean = false;
function Start () {
}
// Fade in myText
function fadeIn () {
Fade.use.Alpha(myText.renderer.material, 0.0, 1.0, 1.0, EaseType.In);
}
// Fade out myText
function fadeOut () {
Fade.use.Alpha(myText.renderer.material, 1.0, 0.0, 1.0, EaseType.Out);
}
function OnCollisionEnter(collision : Collision) {
if (collision.name = "plane_2" || collision.name == "plane_1") {
if (mySwitch == false) { // Could also evaluate !mySwitch
fadeIn();
mySwitch = true;
} else {
fadeOut();
mySwitch = false;
}
}
}
Thank you, i like the compressed code, but I can't run it - it generates a bunch of errors to console!
: o
Ahh, i didn't catch you wrote your if statements incorrectly, fixed, damnit and forgot your parenthesis when calling fadeIn and fadeOut...
Lander Thank you
It's getting closer, but there are still problems with line 22 that's not letting me compile:
if (collision.name = "plane_2" || collision.name == "plane_1") {
Thanks so much for your time!
~be
Your answer
