• 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
Question by andrewwebber25 · Jun 29, 2017 at 07:29 PM · animationmovementanimator2d-platformermovement script

How to Apply Animations to Script

Hey guys so I am currently making a enemy AI for a 2D Platformer. I dont know how I havent been able to find an answer to t$$anonymous$$s cause it seems fairly simple. The enemy sprite hovers back and forth and if the player collides with it, the player respawns at the beginning of the level. The Code works well but the enemy just hovers back and forth. I created two animations, One called "Left" and one called "Right" as you can guess, they are of the enemy walking, facing that direction. Im having trouble incorporating them into my script though.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class HazardMove : MonoBehaviour{

  private ObjectMove player; //references the "player" for trigger
  public Transform start; //references the location the player will respawn on contact
  public float amounttomovex;
  public float speed;
  private float currentposx;
  private float currentposy;
  private int facing;
  public Animation anim; //for switc$$anonymous$$ng the two animatons
  
 
  void Start()
  {
      player = FindObjectOfType<ObjectMove>();
      currentposx = gameObject.transform.position.x;
      facing = 0;
  }
 
  void Update()
  {
      if (facing == 1 && gameObject.transform.position.x < currentposx - amounttomovex)
      {
          facing = 0;
      }
 
      if (facing == 0 && gameObject.transform.position.x > currentposx)
      {
          facing = 1;
      }
 
      if (facing == 0)
      {
          transform.Translate(Vector2.right * speed * Time.deltaTime);
          anim.CrossFade("Right");
      }
      else if (facing == 1)
      {
          transform.Translate(-Vector2.right * speed * Time.deltaTime);
          anim.CrossFade("Left");
      }
  }

As you can see at the last If and Else If statements, I tried using anim.CrossFade("Left"); and Right. T$$anonymous$$s doesn't seem to do anyt$$anonymous$$ng, the animations don't play at all and the sprite just hovers back and forth. Do I have these animations in the wrong spot or should they be in the code differently? Im sure its somet$$anonymous$$ng in the Transform.Translatet that is interfering but when I take parts of it out, it stops hovering back and forth. Ive been stuck on t$$anonymous$$s for a few days now so if anyone has any tips or knows a better way I should be attacking t$$anonymous$$s from, I greatly appreciate it!

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by HenryStrattonFW · Jun 29, 2017 at 09:30 PM

EDIT: So actually on second look at the code, you've got enough to go with already without adding extra flags. since you already have logic that happens only once as the direction switches, t$$anonymous$$s is where you want to change your animation, not every frame like the movement. I've reorganised the code and added some comments, hope t$$anonymous$$s helps.

 private ObjectMove player; //references the "player" for trigger
 public Transform start; //references the location the player will respawn on contact
 public float amounttomovex;
 public float speed;
 private float currentposx;
 private float currentposy;
 private int facing;
 public Animation anim; //for switc$$anonymous$$ng the two animatons
 
 
 void Start()
 {
     player = FindObjectOfType<ObjectMove>();
     currentposx = gameObject.transform.position.x;
 }
 
 void Update()
 {
     //Detect the moment of needing to change direction towards the right.
     if (facing == 1 && gameObject.transform.position.x < currentposx - amounttomovex)
     {
         facing = 0;
         anim.CrossFade("Right");
     }
 
     //Detect the moment of needing to change direction towards the left.
     if (facing == 0 && gameObject.transform.position.x > currentposx)
     {
         facing = 1;
         anim.CrossFade("Left");
     }
 
     // Then process the actual movement.
     if (facing == 0)
     {
         transform.Translate(Vector2.right * speed * Time.deltaTime);
     }
     else if (facing == 1)
     {
         transform.Translate(-Vector2.right * speed * Time.deltaTime);
     }
 }
Comment

People who like this

0 Show 2 · 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 andrewwebber25 · Jul 03, 2017 at 06:19 PM 0
Share

@HenryStrattonFW Thanks for the input but how exactly would I I put this into code? I tried setting "anim" in my inspector to "Left" but any animation I try to drag a drop into it in inspector gets an X over it and doesn't stay. Im sorry that im really new to Unity and app development but if you could help me out a bit more I would really appreciate it!

avatar image HenryStrattonFW andrewwebber25 · Jul 06, 2017 at 10:29 PM 0
Share

Updated with a more useful answer and code sample.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

181 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Player can't move left or right while holding W and S at the same time - No code making it do that? 2 Answers

Simple Animation and Scripting Problem. 1 Answer

I need help with player animation. 1 Answer

A way to "freeze" the position of the animation? 1 Answer

Animator behaviour on GameObject 1 Answer


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