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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Fade out object(s) on collision

Discussion in 'Scripting' started by Zappageck, Aug 10, 2009.

  1. Zappageck

    Zappageck

    Joined:
    Jul 31, 2009
    Posts:
    26
    Hi !
    First of all: I am a newbie to Unity so please don´t throw tomatoes at me :oops:

    I have a problem that I can´t overcome.
    I have a road and have already built in some cars that
    drive on the road in a loop.
    The problem I have is that the scenery is limited and I have added an invisible plane at both ends of the road to allow the cars to turn around. Now I want to use the waypoint colliders before the plane to make the passing cars invisible to hide the turnaround process and fade them back in when they are on the road again (also with a waypoint collider).

    I´m having a hard time figuring this out by scripting.
    My problems: How can I fade out the cars via script and fade them back in when they hit the first waypoint back on the road again ?

    How can I determine the right car to fade out and affect only the car that has actually hit the collider ?

    I´m lost when it comes to Unity scripting and searching the forums didn´t really create a major bang in my brains :(

    Thx for input.
     
  2. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    Never! Potatoes hurt way more than tomatoes, so be prepared for heavy impacts! ;-)

    Tryig to understand. You wrote so far only what you did but not what you want to archieve or I didn't get it fully so far. But let me try:

    What you _could_ do is quite simply have an invisible gameobjet with a collider attached to it and once a car passes that object, you can react on that in the script and destry the car, fade it out, nuke it, paint it yellow, you get the point.

    See here the onCollisionEnter description:
    http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionEnter.html

    So if something passes that collider you can "see" which object it is by doing a:

    Code (csharp):
    1. function OnCollisionEnter(collision : Collision) {
    2.    print("Car passes collider: " + collision.rigidbody.gameObject.name);
    3.    // Example: Destroy object that goes through collider
    4.    Destroy(collision.rigidbody.gameObject;
    5. }
    (put that into a new script and attach it to the freshly created gameobject with a collider component attached to it)

    That collision object will give you exactly that instance of the car that just hit the collider.

    Fading in/out takes a bit of more code. In that case you could spawn a coroutine function that slowly fades out.
     
  3. Zappageck

    Zappageck

    Joined:
    Jul 31, 2009
    Posts:
    26
    Thx for the input so far.
    To clarify my question.
    I already have colliders embedded as the waypoints for the cars have box colliders attached.
    Once a car hits one of these colliders I want it to fade out to hide the turnaround process of the car that is done on an invisible plane. It would look odd if the viewer can see that, so I decided to fade out the objects (cars) before turning around and fade them back in once they hit the next waypoint collider back on the road again.
    Hmm...another idea, is it possible to instantly change the direction of the car, once it hits the collider ?
    What code would I need for such ?
    Another idea would be to disable the mesh renderer for the cars in question and reenable it, once the cars get back on the road again.
    I am having a hard time getting the code and syntax for such right, so any push into the right direction would be great.
     
  4. Zappageck

    Zappageck

    Joined:
    Jul 31, 2009
    Posts:
    26
    I´m going nuts. :(
    Tried to get into the scripting the whole afternoon but I just don´t get it.
    I already dropped the fade-thing as there is no way I´m going to make such anytime soon, so I simply wanted to set the direction of the car once it hits the collider. Bad luck though...
    Altered the above code but unity keeps on reporting errors. Problem is that I have 2 cars in my scene and script should only affect the one that is actually at the collider.

    Code (csharp):
    1. var Car : (collision.rigidbody.gameObject);
    2. function OnCollisionEnter(collision : Collision) {
    3.    print("Car passes collider: " + collision.rigidbody.gameObject.name);
    4.  
    5.    [collision.rigidbody.gameObject]  transform.rotation (0, 180, 0);
    Gives me a heap of errors but I don´t know how to alter the syntax to get it right.
    :?:
     
  5. bitlysub2anf

    bitlysub2anf

    Joined:
    May 8, 2020
    Posts:
    71
    It's been over 10 years and all we (newbies) need is a piece of code with some explanation of what to do. Why!?