0
\$\begingroup\$

I put a Sprite/Diffuse Material on my Player.

In the scene, before starting the game it looks ok (1st screenshot).

After starting the game it still works fine, but when I am starting to move the player on x-axis the it becomes black (2nd screenshot).

Why?

This is the script I use to transform the Player's position and scale:

void Update()
{
    Move();
    {
        float moving = Input.GetAxis("Horizontal");
        characterAnim.SetFloat("Speed", moving);
    }
}

void Move()
{
    if (Input.GetKey("right"))
    {
        transform.localScale = new Vector2(1, 1);
        transform.Translate(Vector2.right * speed * Time.deltaTime);
    }
    if (Input.GetKey("left"))
    {
        transform.localScale = new Vector2(-1, 1);
        transform.Translate(Vector2.right * speed * Time.deltaTime);
    }
    //    rb2D.AddForce(transform.up * 20);
}

Before running the scene After running the scene

\$\endgroup\$
0

1 Answer 1

0
\$\begingroup\$

It's not an issue form Lighting or Material. Modifying localScale with a 2D vector is making the Z value zero is causing the issue.

transform.localScale = new Vector3(1, 1, 1);

Also correct you Translate direction when taking input for Left.

void Move()
{
    if (Input.GetKey("right"))
    {
        transform.localScale = new Vector3(1, 1, 1);
        transform.Translate(Vector2.right * speed * Time.deltaTime);
    }
    if (Input.GetKey("left"))
    {
        transform.localScale = new Vector3(-1, 1, 1);
        transform.Translate(Vector2.left * speed * Time.deltaTime);
    }
}

Use Sprites-Default material unless you have other purpose. You also don't need a light to illuminate objects with Sprites-Default material in them.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .