Your Cart
Loading
unity 2d change sprite color on collision how to change sprite color in unity 2d

Unity 2D Change Sprite Color On Collision | Unity Tutorial

Unity 2D Change Sprite Color On Collision | Unity Tutorial


In this video tutorial, you'll learn how to change a 2D sprite's color in Unity when it collides with other game objects. We'll walk you through creating a simple script that detects collisions and updates the sprite's color based on the type of object it collides with. You'll discover how to use Unity's tag system to specify which objects trigger the color change and how to revert the sprite's color once the collision ends. This tutorial is perfect for adding visual feedback to your 2D games and enhancing the player experience with dynamic interactions.



WATCH FULL TUTORIAL ON YOUTUBE



CHECK OUR PROJECTS


SUBSCRIBE FOR LATEST OFFERS



=========================================================


3 Unity Projects Only $50 - ON SALE!!!!

https://youtu.be/7mI-fJpXN88


Contact: unitycoderz2022@gmail.com


=========================================================



Script:


PlayerController.cs


using UnityEngine;


public class PlayerController : MonoBehaviour

{

  public float moveSpeed = 5f;

  public float jumpForce = 10f;

  public Transform groundCheck;

  public LayerMask groundLayer;

  public float groundCheckRadius = 0.2f;


  public Color originalColor = Color.white; // Public color for the original state

  public Color collisionColor = Color.red; // Color to change to on collision


  public string enemyTag = "Enemy"; // Tag to identify collision with enemies


  private Rigidbody2D rb;

  private Vector2 moveDirection;

  private bool isGrounded;

  private SpriteRenderer spriteRenderer;


  private void Start()

  {

    rb = GetComponent<Rigidbody2D>();

    spriteRenderer = GetComponent<SpriteRenderer>();


    if (spriteRenderer != null)

    {

      // Set the sprite color to originalColor at the start

      spriteRenderer.color = originalColor;

    }

  }


  private void Update()

  {

    // Input handling

    float moveX = Input.GetAxis("Horizontal");

    moveDirection = new Vector2(moveX, 0).normalized;


    // Check if player is on the ground

    isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);


    // Jump input

    if (isGrounded && Input.GetButtonDown("Jump"))

    {

      rb.velocity = new Vector2(rb.velocity.x, jumpForce);

    }


    // Flip sprite based on movement direction

    if (moveDirection.x != 0)

    {

      spriteRenderer.flipX = moveDirection.x < 0; // Flip sprite if moving left

    }

  }


  private void FixedUpdate()

  {

    // Apply movement

    rb.velocity = new Vector2(moveDirection.x * moveSpeed, rb.velocity.y);

  }


  private void OnCollisionEnter2D(Collision2D collision)

  {

    // Check if the collision is with an object that should change the color

    if (spriteRenderer != null && collision.gameObject.CompareTag(enemyTag))

    {

      spriteRenderer.color = collisionColor;

    }

  }


  private void OnCollisionExit2D(Collision2D collision)

  {

    // Revert to original color when not colliding

    if (spriteRenderer != null && collision.gameObject.CompareTag(enemyTag))

    {

      spriteRenderer.color = originalColor;

    }

  }

}





DOWNLOAD CODESTER FILES