- Home /
Parenting Weapon to Empty = Framerate drops to 2fps
Hello everyone,!
here is a small demonstation of my problem :)
http://pn.dyna-studios.com/TA-Problemdemo/WebPlayer.html
If you get to this spear and press E, the weapon is parented to an empty gameObject in the right hand. Then the Weapon follows the Hand perfectly, except the framerate drops to the ground. I've been working for days on this issue and I have no idea why this is the case.
The Weapon has no scripts attached to it. I have also made a Screenshot at Runtime:
Here is my "ItemPickup" Script; it's attached to the Player:
var clone : GameObject;
var item_name;
var WeaponHand : GameObject;
var WeaponHandParent : GameObject;
var showGUI = false;
WeaponHand = GameObject.FindWithTag("weaponmount"); //The Tag of The Empty that's parented to the right Hand
function OnTriggerEnter (other : Collider) {
if (other.gameObject.tag == "weapon"){
showGUI = true; //activate Weapon Pickup Text on collision
}
}
function OnTriggerStay (other : Collider) {
if (other.gameObject.tag == "weapon"){
Debug.Log("WaffenKollision!"); //Collision
item_name = other.gameObject.name;
Debug.Log("item_name: " + item_name); // Get the Items Name
if (Input.GetKeyDown ("e")){
clone = GameObject.Find(item_name); // Get the Item we are colliding with
Debug.Log("clone: " + clone);
clone.transform.parent = WeaponHand.transform;
clone.transform.position = WeaponHand.transform.position;
clone.transform.rotation = WeaponHand.transform.rotation;
Debug.Log("Parented !");
}
}
}
function OnTriggerExit (other : Collider) {
if (other.gameObject.tag == "weapon"){
showGUI = false; //deactivate Weapon Pickup Text
}
}
function OnGUI(){
if (showGUI){
GUI.Label (Rect (500,500,1000,1000), "[E] Waffe aufnehmen"); //"Press E to Pickup the Weapon"
}
}
I hope somebody has an idea of how to solve this, this drives me crazy.
Greetings from Germany :)
Answer by ThunderbirdX11 · Sep 29, 2012 at 08:43 PM
Ok, I've finally figured it out :D
I have a second PlayerModel that has only 1593 triangles and no animations. I set everything up just like my textured player. Then I moved and grabbed the Spear, and....no performance drop!
That led me to try out my textured player with deactivated animations in script....no performance drop!
I turned the animations back on, pressed play and deactivated the Players "Animate Physics" setting at runtime. Then the Framerate came back from 2 to 9 FPS :)
Thanks for your help :)
They say every time a unity answers question gets answered, an angel gets its wings lol
Answer by Bunny83 · Sep 27, 2012 at 08:02 PM
I don't have a good framerate even without the spear attached :D But yes, it get's worse if i do so. First of all, get rid of the Debug.Logs. In the editor they are really slow especially when executed once per frame. Second, you should deactivate (or remove) the script after you parented the object. Does the spear has a rigidbody attached? Does it have a collider that is not set to trigger? Have you disabled the collision between the spear and your player?
It's hard to tell what's wrong here ;)
(btw. Greetings from Germany :D )
Yeah it's still kinda way too much polys for now, I'll reduce it later :)
Thx for the advice about deactivating the Debug.Logs. I didn't know, they eat up that much performance. The Framerate slightly improved now, but it still drops to the bottom when the Spear gets attached.
Actually, I was about to write just one dynamic PickUp script, that will stay attached to the player for picking up other Items too. Is that possible? It would be much work to write down $$anonymous$$iscripts seperately for each Item. The Spear has his Box-Collider, it is set to Trigger. There is no Rigidbody attached to it.
Can I turn off collision just between the spear and the player? $$anonymous$$aybe after the parenting section in the script, right?
hehe a lot of germans here ;) Greetings from Bonn and thanks for answering :)
Answers should answer the question. I've converted you answer into a comment. You can also edit your question at any time.
Well, that's strange. Usually such an action shouldn't have an impact on the fps. You should remove the Animation component when you don't need the animation on the spear. You can disable the animation importing on the model-importer or remove it manually. I'm not sure if that would cause such an fps drop, but remove everything you don't need ;)
You can use Physics.IgnoreCollision to enable / disable the collision between two colliders, or setup your collision matrix and use layers.
Well, it's of course better to use the same script when you wan to pickup multiple objects, but it seems you still can pick up the spear when it's already attached. So maybe you can remove the collider from the spear, or do you need it for other things?
Thanks, seems like I posted it wrong.
I removed the animation component off the spear and added Physics.IgnoreCollision after parenting the object, but it hasn't got any better.
Don't I need the spear collider when I need to hit enemies?
you need the collider only if you plan on detecting hits that way, you could just cast rays or something a certain distance in front of the player to check for hits when he swings.
I had a closer look your project but couldn't find anything terrible wrong. Do you have Unity pro? What does the profiler say?
when you test in the editor you can "edit" the scene at runtime. Those changes will not be saved (except material property changes). You can delete / move / add / parent objects while the game is running. I suggest you put the scene and game view next to each other (like you already have it) and try unparenting it manually at runtime. Does the FPS come back? What when you delete the parented spear? Try just removing the collider of the spear (all at runtime).