Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in '2D' started by gladiuscloud, Jan 11, 2015.

  1. gladiuscloud

    gladiuscloud

    Joined:
    Jan 11, 2015
    Posts:
    44
    Hi I'm just new to unity3d. Just want to know how to make this possible with the help from you. :)

    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 :)
     
    Last edited: Jan 11, 2015
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Something like this should work:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Collision : MonoBehaviour
    5. {
    6.     public Sprite normalSprite;
    7.     public Sprite colliderSprite;
    8.  
    9.     private SpriteRenderer sr;
    10.  
    11.     void Start()
    12.     {
    13.         sr = GetComponent<SpriteRenderer>();
    14.     }
    15.  
    16.     void OnCollisionEnter2D(Collision2D other)
    17.     {
    18.         // You might want to check that "other" really is the object you want. For example by checking its tag
    19.        sr.sprite = colliderSprite;
    20.     }
    21.  
    22.     void OnCollisionExit2D(Collision2D other)
    23.     {
    24.         sr.sprite = normalSprite;
    25.     }
    26. }
    27.  
     
    Last edited: Jan 11, 2015
    gladiuscloud likes this.
  3. gladiuscloud

    gladiuscloud

    Joined:
    Jan 11, 2015
    Posts:
    44
    THank You Very Much sir.. I will start working on it :D
     
  4. gladiuscloud

    gladiuscloud

    Joined:
    Jan 11, 2015
    Posts:
    44
    This is my first stepping stone to understand how to create a simple game. Thanks alot :D
     
  5. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Sorry, I've just realised I made an error. It should ofcourse be "Collision2D"! I've edited the example to reflect this.
     
    gladiuscloud likes this.
  6. gladiuscloud

    gladiuscloud

    Joined:
    Jan 11, 2015
    Posts:
    44
    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.
     
  7. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    You could tag all gameobjects that should affect the player and then check if the collision object have the correct tag. Like this:
    Code (CSharp):
    1. if(other.gameObject.tag == "sometag") {
    2. }
    3.  
     
    gladiuscloud likes this.
  8. gladiuscloud

    gladiuscloud

    Joined:
    Jan 11, 2015
    Posts:
    44
    Thanks again... i will try this up! :D