Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 andrewwebber25 · Jun 23, 2017 at 07:45 PM · animationmovement scriptmove an objectmirror2d platformer

Mirroring Animation with Object Move

Hey guys so I started adding my first bad guy to my 2D platformer. So far he works perfect, I added an ObjectMove script to him that allows me to adjust (in inspector view) how far he can pace back and forth and at what speed he does it. I have a sprite animation that makes him look like he is walking from left to right (while technically still being in place). This animation plays continuously as he walks back and forth. So basically half of the time he is walking from left to right as he should and then when he paces back from right to left, he looks like hes moon walking.

If I set his move distance to 6 for example, is there a way to rig something up that the animation will mirror when he reaches the end of his distance, to make it look like he is turning around? I actually think this is a pretty difficult task and some people were saying you cant do it but I figured I would ask. Im also willing to just flip all the sprites in the animation, make a second animation and have that one played at the end of the first ones cycle if that is possible. Thanks for the help guys!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class ObjectMove : MonoBehaviour
 {
     public float amounttomovex;
     public float speed;
     private float currentposx;
     private float currentposy;
     private int facing;
 
     void Start()
     {
         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);
         }
         else if (facing == 1)
         {
             transform.Translate(-Vector2.right * speed * Time.deltaTime);
         }
     }
 }
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 VinceC80 · Jun 23, 2017 at 08:28 PM

Maybe I'm oversimplifying, but can't you just set the Y scale to a negative value to flip everything?

 else if (facing == 1)
          {
              transform.localScale = new Vector3(1, -1, 1);
              transform.Translate(-Vector2.right * speed * Time.deltaTime);
          }

Comment
Add comment · Show 3 · 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 · Jun 23, 2017 at 08:43 PM 0
Share

Hmm didn't seem to work quite. I took your code and subbed it for my last "else if" statement and some weird stuff happened. The enemy grew about 5x its size, rotated 180 degrees so it looked like he was walking on his head and still only animated in one direction. I tried putting the -1 for x ins$$anonymous$$d of the y scale and it didn't make a difference

avatar image VinceC80 andrewwebber25 · Jun 23, 2017 at 09:18 PM 0
Share

You are right, it should be X scale, not Y. Give this a shot, this should retain the current scale (and not enlarge your object).

  else if (facing == 1)
           {
               transform.localScale = new Vector3(-transform.localScale.x,  transform.localScale.y,  transform.localScale.z);
               transform.Translate(-Vector2.right * speed * Time.deltaTime);
           }
avatar image andrewwebber25 VinceC80 · Jun 26, 2017 at 05:44 PM 0
Share

Its getting a little closer. So when he walks from right to left the image is mirroring repeatedly very fast the whole time. As soon as he walks all the way left and starts walking back to the right, he walks normal, sometimes he is facing the correct direction, sometimes he is moonwalking (the scale stays either .1 or -.1). Then when he walks right as far as he can, he starts walking left while vigorously mirror swaps the whole time again (constantly swapping -.1 and .1 until he reaches the end of the walk). This keeps going till I stop it :/

avatar image
0

Answer by andrewwebber25 · Jun 26, 2017 at 07:25 PM

Moderators Please Feel free to delete this question!

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

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

160 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

Related Questions

Movement and Animation state conflict 1 Answer

How to make enemy detect a player in a certain range? 3 Answers

How to drag a game object with a mouse (along x axis)? 1 Answer

How can I make a prefab object from a list to move?,How can I make an object from a list to move by itself? 2 Answers

How to Move an object by tilting the phone? 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