- Home /
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:
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;
}
}
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);
}
}
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);
Your answer
Follow this Question
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