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!
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
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.
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!!!
Oh, I made a mistake in that code. Sorry for that! It is fixed now (forgot to add $$anonymous$$athf.Abs).
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
);
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!!
You will get the hang of it eventually, its always hard at the beginning, that's totally normal.
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
}
}
Your answer
Follow this Question
Related Questions
how do I make 2 player sprites not interact? 0 Answers
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