• 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 /
  • Help Room /
avatar image
0
Question by Upsightly · Mar 31, 2018 at 05:58 PM · newbie2d-gameplayflipping

Mirror player gameobject to face direction of travel

Hi

I'm very new to all this, and just starting to get my head around how scripting works.

I'm trying to set up a simple 2d character to move around a map, changing from an idle animation to a walking animation when he moves. So far i have that working and can move him all around the map. What I can't figure out how to do, is get the game object to flip when he wants to walk left. At the moment he just moonwalks when moving left.

I've tried a couple of things (commented out below) that have had limited results. What I want to do is have the x scale move to -1 when his movement is negative in the x axis. I know about 'flip sprite' but this doesn't help as I have a light as part of the player game object and it ends up in the wrong place (ie it doesn't flip with the sprite).

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class tl : MonoBehaviour 
 {
     public float moveSpeed;
     Animator anim;
     Transform trans;
 
     // Use this for initialization
     void Start ()
     {
         //starting speed
         moveSpeed = 10f;
         anim = GetComponent<Animator> ();
         trans = GetComponent<Transform> ();
     }
 
     // Update is called once per frame
     void Update ()
     {
         //simple arrow movement
         transform.Translate (moveSpeed * Input.GetAxis ("Horizontal") * Time.deltaTime, moveSpeed * Input.GetAxis ("Vertical") * Time.deltaTime, 0f);
 
         //Animation state controls
         if (Input.GetKeyDown (KeyCode.RightArrow)) {
             anim.SetInteger ("state", 1);
         }
         if (Input.GetKeyUp (KeyCode.RightArrow)) {
             anim.SetInteger ("state", 0);
         }
         if (Input.GetKeyDown (KeyCode.LeftArrow)) {
             anim.SetInteger ("state", 1);
         }
         if (Input.GetKeyUp (KeyCode.LeftArrow)) {
             anim.SetInteger ("state", 0);
         }
     }
 }
         //Flip Player
 
         //if (moveSpeed*Input.GetAxis ("Horizontal") * Time.deltaTime < 0) {
 
         //trans.localScale = new Vector3 (transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);
         //}
         //OR
         //if (Input.GetKeyDown (KeyCode.LeftArrow)) {
         //trans.localScale = new Vector3 (transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);
         //}
         //if (Input.GetKeyDown (KeyCode.RightArrow)) {
         //trans.localScale = new Vector3 (transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);
         //}
 
         //Hmmm
         //if (player = Vector2(-1, 0))
         //trans.localScale = new Vector3 (transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);

I've been getting nowhere for a couple of days now and could really do with some advice!

TIA!

Comment
Add comment · Show 8
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 andrew-lukasik · Mar 31, 2018 at 06:46 PM 0
Share

Since Update is executed repeatedly, your "* -1" makes it flip every frame back and forth. What you want to do ins$$anonymous$$d is to write it so sign(x) is always the same as your sign(input axis), for example, using $$anonymous$$athf.Sign and $$anonymous$$athf.Abs:

 float horizontalAxis = Input.GetAxis ("Horizontal");
 float direction = $$anonymous$$athf.Sign( horizontalAxis );
 trans.localScale = new Vector3 (
     $$anonymous$$athf.Abs( transform.localScale.x ) * direction,
     transform.localScale.y,
     transform.localScale.z
 );

EDIT: fixed

avatar image andrew-lukasik · Mar 31, 2018 at 06:51 PM 0
Share

Nevertheless I think the proper way to do this is not flipping scale, but moving that light ins$$anonymous$$d. Scaling something to negative values is something of a hack. $$anonymous$$ight be good enough for prototyping tho.

avatar image Upsightly · Apr 03, 2018 at 09:08 AM 0
Share

Thanks for the suggestion @andrew-lukasik. I put in the code you suggested, but it didn't work:( I just get my character flipping every tick when I ask it to walk left (I'd achieved the same result with some of my code!). I can't believe that this is so difficult to achieve! All I need is my game object to face the direction its walking!!!

avatar image andrew-lukasik Upsightly · Apr 03, 2018 at 01:59 PM 0
Share

Oh, I made a mistake in that code. Sorry for that! It is fixed now (forgot to add $$anonymous$$athf.Abs).

avatar image andrew-lukasik Upsightly · Apr 03, 2018 at 02:37 PM 0
Share

Btw. If that works consider going back to flipping this sprite again and implement this as a solution to animate that light you mentioned.

 float horizontalAxis = Input.GetAxis ("Horizontal");
 float direction = $$anonymous$$athf.Sign( horizontalAxis );
 lightTransform.localPosition = new Vector3 (
     $$anonymous$$athf.Abs( lightTransform.localPosition.x ) * direction,
     lightTransform.localPosition.y,
     lightTransform.localPosition.z
 );
avatar image Upsightly · Apr 03, 2018 at 04:06 PM 0
Share

That's great! Thanks! He's doing what I want him to do now! The only thing is when I stop moving left (stop pressing 'A') he flips back to facing right. What do I need to have him remain facing left until i press right? Sorry if these seem like very basic questions, I'm really struggling with this at the moment!!

avatar image andrew-lukasik Upsightly · Apr 03, 2018 at 08:00 PM 0
Share

You will get the hang of it eventually, its always hard at the beginning, that's totally normal.

avatar image andrew-lukasik Upsightly · Apr 03, 2018 at 08:12 PM 0
Share

To deal with 0 you can test for it and keep past direction value:

 float direction;
 void Update ()
 {
     float horizontalAxis = Input.GetAxis ("Horizontal");
     if( horizontalAxis>0f || horizontalAxis<0f )
     {
         direction = $$anonymous$$athf.Sign( horizontalAxis );
     } else
     {
         //else its 0, so we keep old direction
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Upsightly · Apr 13, 2018 at 09:42 AM

hey @andrew-lukasik Thanks again!!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

I'm making a 2D game with no levels like flappy bird. How many scenes will I need? 1 Answer

Why is there a large border around background image in 2D project? 0 Answers

Changing player variables through button presses. 0 Answers

2d supporting characters 0 Answers

Simple 2D sprite animator code :( 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges