- Home /
Please help in triggering an animation through another object.
I have a button script and an "isPressed" boolean inside it. Also I have a camera animation. Who can help me with an if statement I will add to the button that will activate the camera animation when isPressed is true? Thanks in advance!
Comment
Answer by Spinnernicholas · Dec 05, 2013 at 04:48 PM
//Add in the body of the button script class
public GameObject camera;
void Update()
{
if(isPressed)
{
camera.animation.Play("animation name");
}
}
void OnGUI()
{
if(GUI.button(args))
{
isPressed = true;
}
else
{
isPressed = false;
}
}
$$anonymous$$y scrpt is in javascript. Can you convert it please?)
//Add in the body of the button script class
public var camera : GameObject;
function Update()
{
if(isPressed)
{
camera.animation.Play("animation name");
}
}
function OnGUI()
{
//Replace "args" with your button arguments
if(GUI.button(args))
{
isPressed = true;
}
else
{
isPressed = false;
}
}
Why doesn't it work? Just nothing happens...
public var myCamera : GameObject;
function Update ()
{
if (Input.touchCount > 0)
{
var ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit))
{
Debug.Log(" You just hit " + hit.collider.gameObject.name);
if(hit.collider.tag == "Credits")
{
myCamera.animation.Play("Credits");
}
}
}
}
Is the animation clip correctly attached to the Animation component? You need to add it to the animation list in the component using the inspector, or so animation.play() can find it.
The problem is before, because... Debug.Log(" You just hit " + hit.collider.gameObject.name); ...doesn't return anything. The debug log remains empty.