- Home /
2d sprite Animation - Fire at frame
Hi guys i am new on this forum i would ask a question about my script problem . i have a ninja that shoot shuriken but my problem is that he must shoot only when the attack animation arrive to 6 image (name of the sixth sprite is 6 lol) how i can do ? Script is this
using UnityEngine; using System.Collections;
[RequireComponent (typeof(Rigidbody2D))] public class AI : MonoBehaviour {
GameObject player,enemy;
public GameObject shuriken;
public bool dis = false;
Rigidbody2D rb;
public Transform shuriken_origin;
public float speed;
public float distance;
LineRenderer range;
public Transform target, eyes;
public float Range_Attack = 17f;
public SpriteRenderer sprite;
Animator anim;
public Vector3 theScale;
public bool touchedleft = false;
public bool touchedright = false;
public Vector2 enemyvel;
public float speedtoleft= -5;
void Start () {
rb = this.GetComponent<Rigidbody2D>();
player = GameObject.FindGameObjectWithTag("Player");
anim = GetComponent<Animator>();
Vector2 enemyvel = rb.velocity;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void Update () {
distance = Vector2.Distance(eyes.transform.position, target.transform.position);
Vector2 enemyvel = rb.velocity;
enemyvel.x = speed;
rb.velocity = enemyvel;
Attack();
}
void FixedUpdate()
{
}
void OnTriggerEnter2D(Collider2D col)
{
Flip();
if (col.gameObject.name == "right")
{
speed = speedtoleft;
enemyvel = new Vector2(rb.velocity.x * speedtoleft, 0);
touchedright = false;
touchedleft = true;
Flip();
}
if (col.gameObject.name == "left")
{
speed = 5;
enemyvel = new Vector2(rb.velocity.x * speed, 0);
touchedleft = false;
touchedright = true;
Flip();
}
}
public void Flip()
{
if (!touchedright && touchedleft)
{
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
if (!touchedleft && touchedright)
{
Vector3 theScale = transform.localScale;
theScale.x *= 1;
transform.localScale = theScale;
}
}
void Attack() // questo metodo è stato un trip
{
Vector2 direction = target.transform.position - transform.position;
bool attack = false;
if (distance < Range_Attack && dis == false)
{
dis = true;
speed = 0;
speedtoleft = 0;
enemyvel = new Vector2(rb.velocity.x * 0, 0);
anim.SetBool("walk enemy", false);
if (touchedleft == true) // andiamo a toccare sinistra
{
attack = true;
Debug.Log("si");
Vector3 Scale = transform.localScale;
Scale.x = 1;
transform.localScale = Scale;
if (attack && sprite.sprite.name == "6") {
GameObject shurikenclone;
shurikenclone = Instantiate(shuriken, shuriken_origin.transform.position, Quaternion.identity) as GameObject;
shurikenclone.GetComponent<Rigidbody2D>().velocity = direction * 2;
}
}
if (touchedright == true) // andiamo a toccare destra
{
attack = true;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
if (attack && sprite.sprite.name == "6") {
GameObject shurikenclone;
shurikenclone = Instantiate(shuriken, shuriken_origin.transform.position, Quaternion.identity) as GameObject;
shurikenclone.GetComponent<Rigidbody2D>().velocity = direction * 2;
}
}
}
else if (distance > Range_Attack && dis == true)
{
attack = false;
dis = false;
anim.SetBool("walk enemy", true);
speed = 5;
speedtoleft = -5;
Vector2 velninja = new Vector2(rb.velocity.x + speed, 0);
enemyvel = velninja;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
touchedleft = false;
touchedright = false;
}
}
}
Answer by Abhiroop-Tandon · Mar 31, 2016 at 09:20 AM
In your animator you can specifically add behavior to one of your states. So if you go in your animator and click on your attack state, go in add behaviour, write a script which says after how long it should instantiate the shuriken and you should be good to go !!
Answer by meat5000 · Mar 31, 2016 at 10:09 AM
'FramaData' is actually a thing!
I found it in the UnityEngine.Experimental.Director namespace and is used by the Animator.
It is a struct (System.ValueType) which seems to hold information on each frame. I assume this is the type used by the Frame Debugger. Given the existence of this Type and the fact that the Frame Debugger seems to work frame to frame it follows that there absolutely must be a way to manipulate the Animator Per-Frame, rather than the time and transition methods which are currently exposed.
the members of this class are as follows:
int updateId
float time
float lastTime
float deltaTime
float timeScale
double dTime
double dLastTime
double dDeltaTime
double dtimeScale
So with little else to go on, I am assuming in this case that updateID
represents the frame number.
I've not attempted to use this yet as Im still trying to find where to access the populated frame info, as this is not exposed in the Animator.
I just wanted to put this info somewhere and this was the most recent Animator-by-frame question. Sorry if it doesnt help you at all OP.
It does seem I might be disappointed as so far the only usage I can see for this in the Engine is by the Profiler.
@Eric5h5 and @Bunny83 Apologies for namedropping you dudes. You are highly versed with Unity internals so i would appreciate it greatly if you could solidify this or $$anonymous$$r it apart. No obligation, of course.
Your answer
Follow this Question
Related Questions
Not Able to Change sprite when animation running 0 Answers
2 Animation Clips, 1 Sprite 0 Answers
How to choose a size/resolution for the sprites used as game assets in Unity2D ? 2 Answers
How to bring empty game object in front of other Panels? 1 Answer
How to swap a spriteRenderers source sprite at runtime when used by animator 0 Answers