Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by hugopok · Mar 30, 2016 at 08:35 PM · sprite2d gameanimatorspriterendereranimationevent

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;
         }
     
     
 }


}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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 !!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Mar 31, 2016 at 10:14 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

AnimationEvent triggers twice 3 Answers

Unity 5.4 - Sprite Shadow 0 Answers

Swapping sprite of game object that has an animator. (Unity 2017.3.1) 0 Answers

Animating player in Unity 0 Answers

Change sprite in a sprite renderer with an animation 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges