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 celestar · Jan 16, 2015 at 03:08 AM · 2d gamedirectionaxislookatflip

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 :

alt text

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!

exempletarget.png (45.5 kB)
Comment
Add comment · Show 2
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 celestar · Jan 16, 2015 at 06:56 AM 0
Share

If my question wasn't correct, someone can change it to adapt perfectly my request.

avatar image celestar · Jan 16, 2015 at 06:57 AM 0
Share

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).

alt text

Thanks!

uvintro.png (269.3 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

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 celestar · Jan 16, 2015 at 03:49 PM 0
Share

(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.

avatar image celestar · Jan 16, 2015 at 04:47 PM 0
Share

I found this to understand UV coordinates. alt text

link source: http://www.rozengain.com/blog/2007/08/26/uv-coordinate-basics/

cube-skin-coords.jpg (34.3 kB)
avatar image celestar · Jan 24, 2015 at 01:34 AM 0
Share

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 }
 }
 }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Move to the direction of movement, in a mechanic similar to Mario Galaxy... 0 Answers

How can I get my enemy to rotate to look at my character in my 2D game?? 2 Answers

How to implement a flip direction to my 2D character controller? 0 Answers

Rotate with joystick - LookAt axis flipping 1 Answer

RigidBody LookAt object in Y axis using AddTorque 0 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