- Home /
Flip 2D object on X axis because of a moving target.
Hi, I found some nice scripts here, but not exactly what I want. The ones I found one uses: Input command. But I don't want this because the object won't be controlled by the player. I'm looking this :
When the target is now to the left of our object, the object flips in the opposite direction and vice versa!
I need :
public Transform target;
In my opinion transform.eulerAngles could be the solution. But I don't how to build the script, I'm just a beginner and I'm not able to find the solutionsince the last two days.
I'm probably near of the goal with this :
if (x > 0)
{
transform.Translate (x * Time.deltaTime, 0, 0);
transform.eulerAngles = new Vector2 (0, 0);
}
if (x < 0)
{
transform.Translate (-x * Time.deltaTime, 0, 0);
transform.eulerAngles = new Vector2 (0, 180);
}
I'm probably missing a big part or completely wrong.
Thank you very much!
If my question wasn't correct, someone can change it to adapt perfectly my request.
I'm really confused. When I use a script to control a character or an object with Input.GetAxis ("Horizontal") I use too only one animation (same side) and with this entire code... :
using UnityEngine;
using System.Collections;
public class test1600 : $$anonymous$$onoBehaviour {
// public Transform target; //// I would like to use this in the script
public Animator anim;
public float speed = 60f;
void Start ()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
float x = Input.GetAxis ("Horizontal"); // I don't want to use : Input Controller.
anim.SetFloat ("speed", $$anonymous$$athf.Abs (x));
if (x > 0)
{
transform.Translate (x * speed * Time.deltaTime, 0, 0);
transform.eulerAngles = new Vector2 (0, 0);
}
if (x < 0)
{
transform.Translate (-x * speed * Time.deltaTime, 0, 0);
transform.eulerAngles = new Vector2 (0, 180);
}
}
}
I'm able to flip the character or object even if I don't have a sprite or an animation in the opposite direction. Is it different with animation to reverse 180 degrees my character.
I'm really confused, because after to read your explanation, (I'm agree) :
You are trying to turn a sprite around and see it "from the other side". But a sprite is a polygon with a texture mapped to it and a polygon only has one side.
but I am not understand perfectly because the code above allow me to flip my character.
I don't understand at all how is possible.
$$anonymous$$aybe I'm unclear. And I don't either know how to proceed.
(sorry for my english).
I'm studying, U, V coordinates (I'm bad in maths and geometry).
Thanks!
Answer by Jeff-Kesselman · Jan 16, 2015 at 03:11 AM
What you are trying to do is fundamentally impossible.
You are trying to turn a sprite around and see it "from the other side". But a sprite is a polygon with a texture mapped to it and a polygon only has one side.
What you need to do in order to create a sprite that is flipped horizontally is to reverse the texture by swapping the u,v coordinates.
My guess is however you don't understand what U,V coordinates are.
The simplest solution for you probably thus is just to flip your art in a 2D art program and make two different sprites, one for each facing.
(I added more details but it appears only in my first post, so it's edited). So please take a look.
$$anonymous$$y guess is however you don't understand what U,V coordinates are.
True and I feel dumb.
The simplest solution for you probably thus is just to flip your art in a 2D art program and make two different sprites, one for each facing.
I flipped in 2d art program, for the next step, I don't know how to proceed.
Thanks.
I found this to understand UV coordinates.
link source: http://www.rozengain.com/blog/2007/08/26/uv-coordinate-basics/
I found what I need, but only in Java, if someone can write it in C#.
pragma strict
var player : Transform; // player
var self : Transform; // object you want to face the player on x axis only
function Start () {
}
function Update () {
if (player.transform.position.x <= self.transform.position.x) //players spot in world space as opposed to enemy "self" spot
{
self.transform.rotation = Quaternion(0,180,0,0); // flips enemy around to face the player on x axis only
}
else if (player.transform.position.x >= self.transform.position.x) //players spot in world space as opposed to enemy "self" spot
{ self.transform.rotation = Quaternion(0,0,0,0); // flips enemy around to face the player on x axis only }
}
}