Hi!
I’m trying to create a 2d platform scene and i need to place a script that animates multiple copies of the same gameObject. The problems that the script attached to it makes animating all gameObject with this script. How can i separate the behavior of every singular gameObject? Here’s the script
using UnityEngine;
using System.Collections;
class GrassAnimationScript : MonoBehaviour
{
private bool onGrass;
public float grassRadius = 1;
public Transform groundCheck;
public LayerMask Grasses;
public Sprite sprite0;
public Sprite sprite1;
public float timer = 1;
public float timerSpeed= 1;
private bool wichSprite;
private SpriteRenderer myRenderer;
// Use this for initialization
void Start ()
{
myRenderer = gameObject.GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update()
{
onGrass = Physics2D.OverlapCircle (groundCheck.position, grassRadius, Grasses);
if(onGrass == true && Input.GetAxis("Horizontal") != 0)
{
if (wichSprite == true)
{
myRenderer.sprite = sprite0;
timer -= Time.deltaTime*timerSpeed;
if (timer <= 0)
wichSprite = false;
}
else if (wichSprite == false)
{
myRenderer.sprite= sprite1;
timer += Time.deltaTime*timerSpeed;
if (timer >= 1)
wichSprite= true;
}
}
else
{
myRenderer.sprite = sprite0;
timer = 1;
}
}
}
Same result using simple animator controller.