0
\$\begingroup\$

I am making a top down Pacman game. I have two sprites: one for facing up and one for facing left.

When I press W, the sprite showing is still the one for when Pacman is moving left, but I want to see the up sprite instead.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class pacman : MonoBehaviour
{
    public int speed;
    Vector2 dest;
    public Transform[] points;
    // Start is called before the first frame update
    void Start()
    {
        dest = transform.position;
        speed = 25;

    }

    // Update is called once per frame
    void FixedUpdate()
    {
        //Vector2 p = Vector2.MoveTowards(transform.position, dest, speed);
       // GetComponent<Rigidbody2D>().MovePosition(p);

        // Check for Input if not moving
        if (Input.GetKey("w"))
            transform.position = Vector3.MoveTowards(transform.position, points[0].position, speed * Time.deltaTime);

        gameObject.GetComponent<SpriteRenderer>().sprite = GameObject.Find("up").GetComponent<SpriteRenderer>().sprite;

        if (Input.GetKey("d"))
            transform.position = Vector3.MoveTowards(transform.position, points[2].position, speed * Time.deltaTime);

        if (Input.GetKey("s"))
            transform.position = Vector3.MoveTowards(transform.position, points[1].position, speed * Time.deltaTime);

        if (Input.GetKey("a"))
            transform.position = Vector3.MoveTowards(transform.position, points[3].position, speed * Time.deltaTime);

        gameObject.GetComponent<SpriteRenderer>().sprite = GameObject.Find("left").GetComponent<SpriteRenderer>().sprite;                
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ This is a pure syntax problem that has nothing to do with game-development specific concepts. I'd recommend working though some more basic C# tutorials until you have a solid grasp of the fundamentals like how control flow works. \$\endgroup\$ – DMGregory Jan 12 at 0:20
1
\$\begingroup\$

You need to use curly braces { } to group together statements that you want to execute only when the condition of the if statement is met.

if (condition)
   DoActionA();

DoActionB();

This says "Do action A only if condition is true, but always do action B no matter what"

if (condition)
{
    DoActionA();
    DoActionB();
}

This instead says "Do action A then action B only if condition is true, otherwise skip them both"

So, specific fixes:

  1. Don't call GetComponent every frame. That's just wasteful. Instead, call it during start-up and cache the result.

  2. Put the sprite assignments inside braces to bundle them with the if statements that should control them.

Like so:

// You could just make these public variables and assign the sprite in the inspector.
Sprite upSprite;
Sprite leftSprite;

SpriteRenderer myRenderer;

void Start()
{
    dest = transform.position;
    speed = 25;

    // Cache our sprites & renderer.
    myRenderer = GetComponent<SpriteRenderer>();
    upSprite = GameObject.Find("up").GetComponent<SpriteRenderer>().sprite;
    leftSprite = GameObject.Find("left").GetComponent<SpriteRenderer>().sprite;  
}

void FixedUpdate()
{
    if (Input.GetKey("w")) {
        transform.position = Vector3.MoveTowards(transform.position, points[0].position, speed * Time.deltaTime);
        myRenderer.sprite = upSprite;
    }
    if...
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.