Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How can I get one item to turn into another item once it passes an Object?

Discussion in '2D' started by LuckyDucky12, Jan 22, 2020.

  1. LuckyDucky12

    LuckyDucky12

    Joined:
    Feb 5, 2016
    Posts:
    16
    I'm making a physics game and I have three things ready.

    I have a fruit (its just a square right now)

    I have a blender


    and I have juice.

    I'd like the fruit to turn into juice after it passes through the blender, but I have no idea how to accomplish this. I assume I need the fruit to spawn the juice after it passes the blender, but after spending a year learning Blueprints in Unreal, I have no idea how to make this happen anymore.

    Bonus question! How would I handle an "unblender" item where I put all the juice in and unblend it back into a fruit?
     
    Last edited: Jan 22, 2020
  2. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    81
    Attach the following script to your fruit object and put a collider on the blender object. Put the collider in the area you want the fruit to turn into juice. When the fruit object hits the blender it will create the "juice object" and destroy itself.


    //FruitObject.cs
    //Make a fruit object and add this script, then make a juice object and create prefabs out of both objects.

    using UnityEngine;
    using System.Collections;

    public class FruitObject : MonoBehaviour
    {
    public Transform JuicePrefab;
    //public Transform SplashPrefab; //uncomment this line to add a splash when the fruit enters the blender
    public AudioSource enterSound; //The sound played when your fruit first enters the blender, a splash maybe?

    void OnCollisionEnter(Collision collision)
    {
    enterSound.Play();
    //the fruit has passed into the blender...
    ContactPoint contact = collision.contacts[0];
    Quaternion rotation = Quaternion.FromToRotation(Vector3.up, contact.normal); //so that your juice has the same path your fruit did
    Vector3 position = contact.point;
    //if (collision.relativeVelocity.magnitude > 3){Instantiate(SplashPrefab, position, rotation);} //uncomment this line to add a splash when the fruit enters the blender
    Instantiate(JuicePrefab, position, rotation);
    //transform.gameObject.SetActive(false); //to just disable the fruit object instead of destroying it, uncomment out this line and comment out the Destroy function below
    Destroy(transform.gameObject);
    }
    }


    If your object is going to be changing back and forth to juice and fruit, instead of destroying and creating objects, you could just disable and enable the fruit and juice objects back and forth, using transform.gameObject.SetActive(false).


    I attached an example project that has a simple demonstration.

    I plan to release free tutorials, projects and resources soon at http://honorsoft.tripod.com/honorsoft-home.html

    That was just a quick solution, you would likely want to make a separate collider(s) for the blender to hold the liquid, and then in the collider around the top of the blender, checkmark "isTrigger" in the inspector so the collider will not stop the fruit or juice, but just act as a detector to tell if the fruit is inside the blender "area".
     

    Attached Files:

    Last edited: Jan 23, 2020
  3. LuckyDucky12

    LuckyDucky12

    Joined:
    Feb 5, 2016
    Posts:
    16

    Sorry it took me so long to reply, I'm having computer issues.

    Still, thanks a lot for the help. Unfortunately, its not really working out for me. I examined your example project and see that yours is in 3D. Maybe I should have specified that my project is a 2d one, I'm sorry about that.

    Perhaps the collisions aren't working right because I need to use 2D collision handlers in the script?

    Also, thanks for telling me about your website! I'll be sure to check it out so that I can use Unity better (I mostly use Unreal these days).
     
  4. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    81
    Sorry about that, I actually have more experience in 3D Unity than 2D, but most of what I wrote should apply to 2D, just add "2D" to the collider stuff. I could use some more practice with 2D, so I will try to make a better example project in 2D for you or anyone it might help.

    PS, Nothing solid has been added to my website on Tripod yet, but I will post some tutorials there and likes to resources on my GoogleDrive, but I was mainly just testing out free webhosting sites. I find Tripod unacceptable due to excessive pop-ups and bugs.
     
  5. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    81
    I was working on a 2D version of that collision example project for practice. Everything went okay, except I am having visibility issues with the sprites, some are being masked by the background, etc. I am not as familiar with the 2D side of Unity, so until I figure out the issue, here is just the script that works in 2D Unity to detect a trigger-collision and create another object and destroy the fruit object.

    * I should note that I mistakenly said before to use "OnCollisionEnter" before for a trigger, a trigger uses OnTriggerEnter.

    Code (CSharp):
    1. //FruitObject.cs
    2. //Make a fruit object and add this script, then make a juice object and create prefabs out of both objects.
    3.  
    4. using UnityEngine;
    5. using System.Collections;
    6.  
    7. public class FruitObject : MonoBehaviour
    8. {
    9.     //public Transform JuicePrefab; // TO DO: make a juice prefab for a filling the blender.
    10.     public Transform SplashPrefab;
    11.    
    12.      void OnTriggerEnter2D(Collider2D collision2D)
    13.     {
    14.     //the fruit has passed into the blender...
    15.         Instantiate(SplashPrefab, transform.position, transform.rotation);
    16.         //transform.gameObject.SetActive(false); //to just disable the fruit object instead of destroying it, uncomment out this line and coment out the Destroy function below
    17.         Destroy(transform.gameObject);
    18.     }
    19. }
    20.  
     
  6. LuckyDucky12

    LuckyDucky12

    Joined:
    Feb 5, 2016
    Posts:
    16
    Thanks a lot for this help! I'm in the middle of working on another unity project, but I'll be sure to try this code out when I get the chance.
     
  7. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    81
    Here's a 2D version of the project using Unity 2018 demonstrating collision (trigger) with a basic menu and examples of video background, music, sounds, animated text, quit menu, etc.
    Create a new 2D project, then import the package.
    (Just be sure to add both scenes in the Scenes folder to the Build Settings "Scenes In Build", start_scene goes first.)

    Juiced1.jpg

    Collision2D_JuicedExampleProj (Unity package, 8.34mb):
    https://drive.google.com/file/d/1LoIahgidmyxusXj97fn-opBwF-cg0Px7/view?usp=sharing
     

    Attached Files:

    Last edited: Feb 10, 2020