- Home /
This question was
closed Dec 28, 2017 at 03:41 AM by
esandberg2000 for the following reason:
The question is answered, right answer was accepted
Question by
esandberg2000 · Dec 27, 2017 at 10:30 PM ·
charactercontrollernewbie2d animationcharacter movementflip
How to flip 2D character to face direction of movement
using System.Collections;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public bool dead = false;
public float speed;
private Rigidbody2D rb2d;
public GameObject[] health;
public GameObject Hazard;
public GameObject HealthPickup;
private bool facingRight;
private void Start()
{
facingRight = true;
rb2d = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
if (Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d"))
{//find controller inputs
float moveHorizontal = Input.GetAxisRaw("Horizontal");
float moveVertical = Input.GetAxisRaw("Vertical");
transform.Translate(moveHorizontal * speed * Time.deltaTime, moveVertical * speed * Time.deltaTime, 0);
}
else
{
float moveHorizontal = Input.GetAxisRaw("JoystickHorizontal");
float moveVertical = Input.GetAxisRaw("JoystickVertical");
transform.Translate(moveHorizontal * speed * Time.deltaTime, moveVertical * speed * Time.deltaTime, 0);
//wowie here im trying
}
}
private void Flip(float moveHorizontal)
{
if (moveHorizontal > 0 && !facingRight || moveHorizontal < 0 && facingRight)
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
public void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Hazard"))
{
SendMessage("TakeDamage", 1);
Destroy(other.gameObject);
}
if (other.CompareTag("HealthPickup"))
{
SendMessage("Heal");
Destroy(other.gameObject);
}
}
}
I'm sorry if I'm making a stupid mistake somewhere, I am very new to this. Any help would be appreciated. I'm trying to make my 2D character flip by making the x Scale -1 when the character switches direction.
Comment
Answer by Larry-Dietz · Dec 28, 2017 at 02:48 AM
Flip looks like it should work for you. You just need to call it. I would put in a call at lines 24 and 32.
If you add these calls and still have problems, let me know.
Hope this helps, -Larry
Follow this Question
Related Questions
Fliping character 180 degrees when moving left 1 Answer
Strange Character Controller Behavior Caused by Simulated Gravity and Ground Check 0 Answers
How to make character controller forward jump a certain distance? 1 Answer
Player Controller "Horizontal" input convert to "left" and "right" 1 Answer
Character Creeping Backwards 0 Answers