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

GameObject Moving Thorugh Player When Animating?

Discussion in 'Scripting' started by Quist, Oct 7, 2014.

  1. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    So i have a Parent object, the Cabinet which have 4 child objects: CabinetBody, Drawer, Wing1 & Wing 2.
    Wing1 have a child object named Glass1
    Wing2 have a child object named Glass2

    So what i have done so far is that i have made a Script which when i left click, makes the Wing1 and 2 play an Open and Closing animation made with the build in animation system in Unity.
    And i have placed a mesh collider on the parent object Cabinet and mesh colliders on the wing1 and wing2.

    So the problem is: When the object is playing the animation it goes through the player.
    I want it to push the player back instead of going through.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var theLeftCloset : Transform;
    4. var theRightCloset : Transform;
    5. var closetIsClosed = true;
    6. private var drawGUI = false;
    7.  
    8. function Update ()
    9. {
    10.     if (drawGUI == true && Input.GetKeyDown("mouse 0") && closetIsClosed == true)
    11.     {
    12.            TheClosetOpens();
    13.     }
    14.     if (drawGUI == true && Input.GetKeyDown("mouse 0") && closetIsClosed == false)
    15.     {
    16.         TheClosetCloses();
    17.     }
    18. }
    19.  
    20. function OnTriggerEnter (theCollider : Collider)
    21. {
    22.     if (theCollider.tag == "Player")
    23.     {
    24.         drawGUI = true;
    25.     }
    26. }
    27.  
    28. function OnTriggerExit (theCollider : Collider)
    29. {
    30.     if (theCollider.tag == "Player")
    31.     {
    32.         drawGUI = false;
    33.     }
    34. }
    35.  
    36. function OnGUI ()
    37. {
    38.     if (drawGUI == true)
    39.     {
    40.         GUI.Box (Rect (Screen.width*0.5-50, Screen.height*0.5-25, 170, 22), "Left click to open the closet");
    41.     }
    42. }
    43.  
    44. function TheClosetOpens()
    45. {
    46.         theLeftCloset.animation.CrossFade("SmallLeftOpen");
    47.         theRightCloset.animation.CrossFade("SmallRightOpen");
    48.         yield WaitForSeconds(0.1);
    49.         closetIsClosed = false;
    50. }
    51.  
    52. function TheClosetCloses()
    53. {
    54.         theLeftCloset.animation.CrossFade("SmallLeftClose");
    55.         theRightCloset.animation.CrossFade("SmallRightClose");
    56.         yield WaitForSeconds(0.1);
    57.         closetIsClosed = true;
    58. }
    59.  
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    1. What kind of collider is on the player?

    2. Please post the inspector views of Wing1 and Glass1. At least one of them should have a collider whose Is Trigger is not ticked, and a rigidbody that you can mark Is Kinematic.