Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Haxxxxx · Apr 06, 2014 at 07:08 PM · rotationrigidbodyvector3directionflip

Flipping a 2D sprite based on direction along the x axis (2D)?

Simple question with probably a simple answer. I just can't seem to grasp the concept of the solution to my problem and I've looked around prior to this!

I have code that is moving an animated sprite along a predefined path as shown below:

alt text

As you can see the sprite is facing the same direction at all times. How do I work out which direction the sprite is facing and then flip it accordingly? As anything I've tried simply makes the sprite flip back and forth numerous times a second regardless of direction. On this map it would only need to flip the sprite twice, but I wouldn't like to hard code co-ordinates indicating when to flip it as I have many other maps and it would be much tidier, usable code.

Code below:

 using UnityEngine;
 using System.Collections;
 
 public class WayPointWalker : MonoBehaviour 
 {
     private int _targetWaypoint = 0;
     private Transform _waypoints;
     public bool facingRight = true;
     
     public float movementSpeed = 3f;
     
     // Use this for initialization
     void Start () 
     {
         _waypoints = GameObject.Find("Waypoints").transform;
     }
     
     // Update is called once per frame
     void Update () 
     {
         
     }
     
     // Fixed update
     void FixedUpdate()
     {
         handleWalkWaypoints();
     }
     
     // Handle walking the waypoints
     private void handleWalkWaypoints()
     {
         Transform targetWaypoint = _waypoints.GetChild(_targetWaypoint);
         Vector3 relative = targetWaypoint.position - transform.position;
         Vector3 movementNormal = Vector3.Normalize(relative);
         float distanceToWaypoint = relative.magnitude;
 
         /*if () 
         {
             facingRight = false;
         }
 
         if () 
         {
             facingRight = true;
         }
         
         if (!facingRight)
         {
             Flip();
         }
         else if (facingRight)
         {
             Flip();
         }*/
 
 
         if (distanceToWaypoint < 0.1f)
         {
             if (_targetWaypoint + 1 < _waypoints.childCount)
             {
                 // Set new waypoint as target
                 _targetWaypoint++;
             }
 
             else
             {
                 Destroy(gameObject);
                 return;
             }
         }
         else
         {
             // Walk towards waypoint
             rigidbody2D.AddForce(new Vector2(movementNormal.x, movementNormal.y) * movementSpeed);
         }
 
     }
 
     void Flip()
     {
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 
 }
 

untitled.png (268.7 kB)
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
Best Answer

Answer by Haxxxxx · Apr 07, 2014 at 10:18 PM

Turns out I needed to check it against velocity, answer below:

     // Use this for initialization
     void Start () 
     {
         waypoints = GameObject.Find("Waypoints").transform;
         someScale = transform.localScale.x;
     }
     
     // Update is called once per frame
     void Update () 
     {
         if (rigidbody2D.velocity.x >= 0) 
         {
             transform.localScale = new Vector2(someScale, transform.localScale.y);            
         }
         
         else
         {
             transform.localScale = new Vector2(-someScale, transform.localScale.y);
         }
     }
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 Fubiou · Sep 07, 2015 at 07:31 AM

I just found a simple way to flip the character without any problem:

just try this!

transform.localEulerAngles = transform.eulerAngles + Vector3(0,180,-2*transform.eulerAngles.z);

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

22 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

Related Questions

Get Direction from 2 Vectors - and Apply to Transform 0 Answers

How to rotate a direction (create a direction from another direction) 2 Answers

Understanding Space.World and Space.Self: Character changing movement direction 1 Answer

Rotate vector3 relative to another vector3 1 Answer

Flip over an object (smooth transition) 3 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