- Home /
Fliping character 180 degrees when moving left
So I am using the PlayerPlatformerController that Unity provides and the script flips the Character. I dont want that because I used skeletal animation and if I flip every single sprite it doesn't look right, so instead i want to rotate my character by 180 degrees but i dont know how to do that via this script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerPlatformerController : PhysicsObject
{
public float maxSpeed = 7;
public float jumpTakeOffSpeed = 7;
private SpriteRenderer[] spriteRenderer;
private Animator animator;
// Use this for initialization
void Awake()
{
spriteRenderer = GetComponentsInChildren<SpriteRenderer>();
animator = GetComponent<Animator>();
}
protected override void ComputeVelocity()
{
Vector2 move = Vector2.zero;
move.x = Input.GetAxis("Horizontal");
if (Input.GetButtonDown("Jump") && grounded)
{
velocity.y = jumpTakeOffSpeed;
}
else if (Input.GetButtonUp("Jump"))
{
if (velocity.y > 0)
{
velocity.y = velocity.y * 0.5f;
}
}
bool flipSprite = (spriteRenderer[0].flipX ? (move.x > 0.01f) : (move.x < 0.01f));
if (flipSprite)
{
for (int i = 0; i < spriteRenderer.Length; i++)
{
spriteRenderer[i].flipX = !spriteRenderer[i].flipX;
}
}
animator.SetBool("grounded", grounded);
animator.SetFloat("velocityX", Mathf.Abs(velocity.x) / maxSpeed);
targetVelocity = move * maxSpeed;
}
}
Answer by DiegoSLTS · Apr 19, 2019 at 02:06 PM
You can change the X scale to flip the whole object to get a similar effect than flipping the sprites. I guess something like this should work:
Vector3 scale = transform.localScale;
bool flip = (scale.x < 0.0f ? (move.x > 0.01f) : (move.x < 0.01f)); // this assumes that a positive X scale is the default state, so a negative x would indicate an already flipped character
if (flip)
{
scale.x *= -1.0f;
transform.localScale = scale;
}
Try that instead of the "bool flipSprite" code.
Your answer
Follow this Question
Related Questions
Player acts weirdly when I release movement input 0 Answers
Controller Movement Problem 1 Answer
Character Controller Move in X and Z axis via camera 1 Answer
The character controller stop moving when the Speed Start increase Endless runner 0 Answers
How to flip 2D character to face direction of movement 1 Answer