Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Why Is my sprite Not Changing

COOLCODERCOOLCODER Member
edited December 2020 in Programming

My sprite is not changing when i press space bar and jump.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class Playercontrols : MonoBehaviour

{

  private float leftdirection;

  private float xdirection;

  Rigidbody2D Rb;

  float grounddistance = 0.4f;

  bool isGrounded ;

  public LayerMask ground;

  public Transform groundchecktrans;

  

  public Sprite jump;

  public Sprite idle;

  void Start()

  {

     

    Rb = GetComponent<Rigidbody2D>();

     

  }


  // Update is called once per frame

  void Update()

  {



    isGrounded = Physics2D.OverlapCircle(groundchecktrans.position, grounddistance, ground);

    xdirection = Input.GetAxis("Horizontal");

     


    if (Input.GetKey(KeyCode.D))

    {

      Rb.velocity = new Vector2(10, Rb.velocity.y);

    }

     

    if (Input.GetKey(KeyCode.A))

    {

      Rb.velocity = new Vector2(-10, Rb.velocity.y);

      this.gameObject.GetComponent<SpriteRenderer>().sprite = idle;

    }

    

    if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)

    {

       

      Rb.velocity = new Vector2(Rb.velocity.x,10);

      this.gameObject.GetComponent<SpriteRenderer>().sprite = jump;

    }

    

Best Answer

  • HNJHNJ Member
    Accepted Answer

    just accept an comment as your answer and people will see it has an accepted answer, they will not come here.

Answers

Sign In or Register to comment.