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 NewDevDave · Mar 26, 2017 at 05:18 PM · spritesflip

Could someone please help with making an enemy sprite flip when going the other way?

Hi I am in the process of making a 2d platformer and have got a simple enemy sprite who I have managed to get moving back and forth, but I am really having issues with getting the sprite to flip when going the other direction. I have tried various different ways but I have a feeling that the movement script I am using makes it difficult. I have spent the last 4 hours trying to get this to work and have become very frustrated! This is the script I am using to move him and I have tried adding a flip function but the sprite goes crazy when he reaches the point where he should flip. using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class GroundEnemyMove : MonoBehaviour
 {
 
     private Vector3 posA;
     private Vector3 posB;
     private Vector3 nextpos;
     public bool facingLeft;
     [SerializeField]
     public float speed;
     [SerializeField]
     private Transform Enemy;
     [SerializeField]
     private Transform transformB;
     // Use this for initialization
     void Start()
     {
         posA = Enemy.localPosition;
         posB = transformB.localPosition;
         nextpos = posB;
     }
 
     // Update is called once per frame
     void Update()
     {
         Move();
     }
 
     void Flip()
     {
         facingLeft = !facingLeft;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 
     private void Move()
     {
         Enemy.localPosition = Vector3.MoveTowards(Enemy.localPosition, nextpos, speed * Time.deltaTime);
         if (Vector3.Distance(Enemy.localPosition, nextpos) <= 0.1)
         {
             ChangeDestination();
         }
 
 
         }
 
     private void ChangeDestination()
     {
         nextpos = nextpos != posA ? posA : posB;
         transform.position = nextpos;
         Flip();
     }
 
 
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by The_Magical_Muffins · Mar 26, 2017 at 08:15 PM

Have you checked here? https://docs.unity3d.com/ScriptReference/SpriteRenderer-flipX.html

I would try something along the lines of, If the enemy has a velocity below 0(he would move left), then flip the image

Comment
Add comment · 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 NewDevDave · Mar 28, 2017 at 03:30 PM 0
Share

Thanks for the response I tried to play around with your suggestion but it became more complicated as I needed to add RigidBody for the direction and I couldn't get it to work. I have managed it now based from your suggestion with the SpriteRenderer and incorporating it into my existing flip function.

     void Flip()// This makes him turn at the end of each distance
     {
         if (nextpos == posA)
         {
             groundenemy.flipX = true;
         }
         if (nextpos == posB)
         {
             groundenemy.flipX = false;
         }
avatar image NewDevDave · Mar 28, 2017 at 04:18 PM 0
Share

Thanks for the response. I tried to play around with your suggestion but found myself getting bogged down in rigidbody2d and it seemed to get more and more complicated (and still didn't work!). I have managed it now though using the sprite renderer and the positioning.

void Flip()// This makes him at the end of each distance { if (nextpos == posA) { groundenemy.flipX = true; } if (nextpos == posB) { groundenemy.flipX = false; }

@ The_$$anonymous$$agical_$$anonymous$$uffins

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

sprite always flip to left,Sprite always flip to left 1 Answer

export max to unity ??? 2 Answers

Script not obeying? 2 Answers

Unity not recognizing true statement. 1 Answer

How to get an object to turn around/flip at a certain point 2 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