Game (2D) not rendering at all
I'm working on a small project in 2D, and made walk cycles for every direction but left, figuring I could just flip the sprite in code. So I made a bit of code to tell it to flip when moving -x, and it worked (judging by the inspector), but it also prevents everything from rendering, leaving only the background color. Here is the code that is causing the problem:
if (Input.GetAxisRaw("Horizontal") < -0.5f)
{
transform.localScale = new Vector2(-0.5f, 0.5f);
}
else
{
transform.localScale = new Vector2(0.5f, 0.5f);
}
Answer by I_Am_Err00r · Aug 22, 2019 at 04:15 PM
Add a bool variable isFacingLeft;
Change that block of code you provided to this:
float movementDirection = Input.GetAxisRaw("Horizontal");
if(movementDirection > 0 && isFacingLeft == true)
{
Flip()
}
else if(movementDirection < 0 && isFacingLeft != true)
{
Flip();
}
And then add this block of code to that script you have there:
private void Flip()
{
isFacingLeft = !isFacingLeft;
transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
}
That flips the moment you change directions; if you want you can change the if statements that look like this:
if(movementDirection > 0 && isFacingLeft == true)
To this (like you have in your code):
if(movementDirection > .5f && isFacingLeft == true)
That should flip character on movement changes, let me know if you have issues; I don't have Unity with me and wrote this answer on this forum, so there might be errors.
Answer by AstronautPug · Aug 22, 2019 at 04:51 PM
@I_Am_Err00r Thank you for the help - It now renders everything correctly on startup, but if you turn left, everything vanishes.
Please move your answer here to a comment under my answer, it helps with organization and if I provide the right answer, you can give me the points easier :)
Please provide the entire code that handles the movement, it shouldn't do that if it is setup correctly.
Here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : $$anonymous$$onoBehaviour
{
public float moveSpeed;
private bool isFacingLeft = false;
private void Flip()
{
isFacingLeft = !isFacingLeft;
transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
}
private Animator puganim;
// Start is called before the first frame update
void Start()
{
puganim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
{
transform.Translate(new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f));
}
if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
{
transform.Translate(new Vector2 (0f, (Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime)));
}
float movementDirection = Input.GetAxisRaw("Horizontal");
if (movementDirection > 0 && isFacingLeft == true)
{
Flip();
}
else if (movementDirection < 0 && isFacingLeft != true)
{
Flip();
}
puganim.SetFloat("$$anonymous$$oveX", Input.GetAxisRaw("Horizontal"));
puganim.SetFloat("$$anonymous$$oveY", Input.GetAxisRaw("Vertical"));
}
}
That's weird it's giving you isseues, I don't know why you were getting errors, but I cleaned up your code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : $$anonymous$$onoBehaviour
{
public float moveSpeed;
private bool isFacingLeft = false;
private bool isFacingUp = false;
private Animator puganim;
private float horizontal$$anonymous$$ovement;
private float vertical$$anonymous$$ovement;
void Start()
{
puganim = GetComponent<Animator>();
}
void Update()
{
horizontal$$anonymous$$ovement = Input.GetAxisRaw("Horizontal");
vertical$$anonymous$$ovement = Input.GetAxisRaw("Vertical");
}
void FixedUpdate()
{
if (horizontal$$anonymous$$ovement > 0.5f || horizontal$$anonymous$$ovement < -0.5f)
{
transform.Translate(new Vector2(horizontal$$anonymous$$ovement * moveSpeed * Time.deltaTime, 0f));
CheckHorizontalDirection();
}
if vertical$$anonymous$$ovement > 0.5f || vertical$$anonymous$$ovement < -0.5f)
{
transform.Translate(new Vector2 (0f, vertical$$anonymous$$ovement * moveSpeed * Time.deltaTime)));
CheckVerticalDirection();
}
puganim.SetFloat("$$anonymous$$oveX", horizontal$$anonymous$$ovement);
puganim.SetFloat("$$anonymous$$oveY", vertical$$anonymous$$ovement);
}
private void CheckHorizontalDirection()
{
if (horizontal$$anonymous$$ovement > .5f && isFacingLeft == true)
{
FlipX();
}
else if (horizontal$$anonymous$$ovement < .5f && isFacingLeft != true)
{
FlipX();
}
}
private void CheckVerticalDirection()
{
if (vertical$$anonymous$$ovement > .5f && isFacingUp != true)
{
FlipY();
}
else if (vertical$$anonymous$$ovement < .5f && isFacingUp == true)
{
FlipY();
}
}
private void FlipX()
{
isFacingLeft = !isFacingLeft;
transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
}
private void FlipY()
{
isFacingUp = !isFacingUp;
transform.localScale = new Vector2(transform.localScale.x, transform.localScale.y * -1);
}
}
Try that, let me know what happens, if you get an error, please take a screenshot of the transform and the object selected so I can see what is going on; also, do you have any other script that manipulate the scale or rotation of the player object?
Your answer
Follow this Question
Related Questions
2d Pathfinding in Unity 5.6, how it works? 0 Answers
Unity2D Move character in tile increments with collision 1 Answer
Object with rigidbody2D doesn't move when it's supposed to 0 Answers
Sprite facing the wrong way while not moving,Sprite facing the wrong way when not moving 2D 0 Answers
unity 2D limb solvers in wrong points 0 Answers