Search Unity

Instantiate an object at a specific location using animation events

Discussion in 'Scripting' started by Sicklilmonky, May 17, 2011.

  1. Sicklilmonky

    Sicklilmonky

    Joined:
    Feb 15, 2011
    Posts:
    30
    OK I figure I'll try my luck here since I'm getting Jack for help on this over at Unity answers.
    I've been at this for months now trying to solve this issue.

    Basically I have an animation that uses animation events to make certain things happen.
    1- the UFO takes off if the FPS player gets within rage - DONE and working wonderfully

    2- UFO has a mid air explosion (Animation event) - DONE and working perfectly

    3- UFO crashes and gets replaced with a wrecked UFO (Animation event) - DONE and working perfectly

    Now here the truly sucking part!

    4- UFO is suposed to drop off an alien artifatct (Animation Event) that the FPS player is supposed to be able to pick up.
    Artifact is suppoed to be dropped at a certain location, I've got coordinatess and everything.
    Can anyone get it to work? Frak No!

    Here is what we've been trying code wise:

    Code (csharp):
    1. var explosion : GameObject;
    2. var wreck : GameObject;
    3. var artifactTarget : Transform;
    4. //var Artifact01 : Transform;
    5.  
    6.  
    7. function ExplodeUFO() //Animation Event that creates the mid air explosion
    8. {
    9.         Instantiate(explosion, transform.position, transform.rotation);
    10. }
    11.  
    12. function KillSelf () { // Animation Event that replaces the UFO with the wrecked UFO  alien corpse
    13.         yield WaitForSeconds(3);
    14.         var wreckClone = Instantiate(wreck, transform.position, transform.rotation);
    15.         Destroy(gameObject);
    16. }
    17.  
    18. function dropArtifact () { //Animation Event that is SUPPOSED to drop of the Alien Artifact object that the player
    19.                                     // can pick up
    20.  
    21.     var artifactPosition = Vector3(276.7286, 5.241356, 963.852);
    22.     var artifactRotation = Vector3(270, 0,0);
    23.    
    24.     var Artifact01 = Instantiate(artifactTarget, artifactTarget.position, artifactTarget.rotation);
    25.     //Instantiate(artifactTarget);
    26.    
    27.     print("Artifact created");
    28. }
     
  2. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Shouldn't

    Code (csharp):
    1.  
    2. var Artifact01 = Instantiate(artifactTarget, artifactTarget.position, artifactTarget.rotation);
    3.  
    be:

    Code (csharp):
    1.  
    2. var Artifact01 = Instantiate(artifactTarget, artifactPosition, artifactRotation);
    3.  
    Or are you intentionally ignoring those values for now?
     
  3. Sicklilmonky

    Sicklilmonky

    Joined:
    Feb 15, 2011
    Posts:
    30
    Well to tell you the truth I've been trying so many different variations on this thing that's it's probably all mucked up... all except for the part early on in the code that as i've mentioned are working, it's just that trying to get my artifact to instantiate in the right place is not working. It keeps placing it in the air way out of reach of my player.

    plus if I try it your way I get a big O'l error message, so that can't be right either??
     
    Last edited: May 17, 2011
  4. simone84

    simone84

    Joined:
    Nov 3, 2010
    Posts:
    2
    Is the artifact a child of anything? Can you force it to instantiate in world coordinates rather than local?

    Bit of a n00b scripter here so I don't know, but it's a thought.
     
  5. Sicklilmonky

    Sicklilmonky

    Joined:
    Feb 15, 2011
    Posts:
    30
    Hi simone:
    Thanks for trying to help out with this. First off, Nope, the artifact is NOT a child of anything just a prefab.
    2nd, I'm still quite new to scripting myself and have only been at this for a little over 3 months so I don't know what you mean by local or world coordinates... what I mean is, is that I understand the difference but how that is scripted I have no idea. All my scripting should be Java if that helps any?
     
  6. ahmetDP_1

    ahmetDP_1

    Joined:
    Sep 23, 2010
    Posts:
    113
    What happens when number 4 does not work? Any error messages, any instatiated objects elsewhere?

    Is the dropArtifact () function ever called?

    Normally this

    should work.
     
  7. Sicklilmonky

    Sicklilmonky

    Joined:
    Feb 15, 2011
    Posts:
    30
    No error messages. I believe it gets to the end of the code because my Print reads "Artifact created" which it does. It just creates it way up in the air far out of reach of my player. it's almost like it just completely ignores the location instantiation stuff. I have tried so many variations trying to get it to work but it's not no matter what I do. It's REALLY holding up my game creation because I can't solve this problem in order to get on with the other stuff.
     
  8. amirghayes

    amirghayes

    Joined:
    Apr 8, 2011
    Posts:
    98
    if I understand right
    What I would suggest is that you attach a rigid body to the instantiated prefab and a collider to it so it will keep falling till it hits the ground and then the player can pick it up
    if the prefab already have a collider and u marked is trriger then add istrriger is false first to your script then u may turn it back on when it hits the ground
    if my understanding is wrong then ignore my reply :D

    btw , the "Artifact created" will alwayes be printed wethere it is instantiated the prefab or not cause I don't see an if statement
     
    Last edited: Jun 8, 2011