Sprite will change whenever it collides with a certain imageObject. HELP!

Hi I’m just new to unity3d. Just want to know how to make this possible with the help from you. :slight_smile:

1.) First, the hero_sprite1 remain unchanged when not in collision with the circle_object.
see image below:

2.)When the circle_object collides with the hero_sprite1, it changes to hero_sprite2.
see image below:

3.) Finally, after the collision, the hero_sprite2 will return back to hero_sprite1.

Thanks :slight_smile:

Something like this should work:

using UnityEngine;
using System.Collections;

public class Collision : MonoBehaviour
{
    public Sprite normalSprite;
    public Sprite colliderSprite;

    private SpriteRenderer sr;

    void Start()
    {
        sr = GetComponent<SpriteRenderer>();
    }

    void OnCollisionEnter2D(Collision2D other)
    {
        // You might want to check that "other" really is the object you want. For example by checking its tag
       sr.sprite = colliderSprite;
    }

    void OnCollisionExit2D(Collision2D other)
    {
        sr.sprite = normalSprite;
    }
}
1 Like

THank You Very Much sir… I will start working on it :smile:

This is my first stepping stone to understand how to create a simple game. Thanks alot :smile:

Sorry, I’ve just realised I made an error. It should ofcourse be “Collision2D”! I’ve edited the example to reflect this.

1 Like

No Probs… the script error said: This message parameter has to be of type: “Collision2D” XD

btw what if… the normalSprite stays as normalSprite whenever it collides only in “BoxCollider2D”. Example i made a wall or floor.

You could tag all gameobjects that should affect the player and then check if the collision object have the correct tag. Like this:

if(other.gameObject.tag == "sometag") {
}
1 Like

Thanks again… i will try this up! :smile: