Hi I’m bringing in a int value from an external arduino sensor.
The strength of the incoming signal alters the scale of a 3d object in my scene.
The incoming int value is called ‘allscale’ and the code is applied to the 3d object.
This works fine.
I also have a sprite which has a series of animations.
I want the value of my arduino sensor value to alter which animated sequence is played on the sprite.
I have an integer called ‘bubblestate’ applied to the Animator and this value is adjusted in the condition section of each of the transitions.
I presume I need to apply a script to the sprite.
How can I
1.pull in the ‘allscale’ value into the sprite script.
2. How can I adjust the ‘bubblestate’ value based on the value of the ‘allscale’ value ?
An easy way to get the at “allscale” in other scripts is to make it “public static”.
using UnityEngine;
using System.Collections;
using System.IO.Ports;
public class ledscale : MonoBehaviour {
public static int allscale = 0;
SerialPort serial = new SerialPort("COM8",9600);
void Update () {
if (!serial.IsOpen)
serial.Open ();
allscale = 30 *(int.Parse (serial.ReadLine()));
if (allscale <= 30)
{
allscale =30;
}
transform.localScale = new Vector3(allscale,allscale,allscale);
}
}
then you can access it from the other scripts with “ledscale.allscale” from other scripts.
To access your animation you do something like this:
using UnityEngine;
using System.Collections;
public class AnimStuff : MonoBehaviour
{
Animator anim;
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
anim.SetInt("bubblestate", ledscale.allscale);
}
}
Hi - I got that sort of working with one or two changes - thanks for your help.
Unfortunately (while bubbles.cs does seem to pull in the external ‘allstate’ value it is only triggering the first animation and doesn’t play anything further…do you think the issue is with the code or with the properties of my sprite animations ? Is there a control pannel button that needs to be activated in order to switch between the transtioned sprites based on their int value ?
The code looks alright to me. There is no buttons that need to be pressed for the transitions to work. If all the transitions are set us as the one in the picture you included, they should be OK. If you try trigger one of the other animations without the if:s, does that work?