- Home /
Scripts don't work on model
I have scripts that make it so the player can attack the enemy and the enemy can attack the player. When i apply the scripts to a game object such as a cube or cylinder they work perfectly, but when i download a character model from the asset store and apply the scripts to them it doesn't work. How can I fix this?
That's terribly unspecific. No script, you don't tell us what the script does to "work", you don't tell us how it fails - e.g. is the code still executed? Do Debug.Logs get printed? $$anonymous$$aybe some other script overrides movement or whatever your attack script does.
I have a script that tells the enemy to attack the player and cause health deduction, and another script that lets the player attack the enemy. The script that allows the enemy to attack the player works, but the other script does not. When the player attacks the enemy it is suppose to Destroy (gameObject); but it does not work when i import a character model. The script works fine on game objects such as a cube or sphere, but not an imported model.
Ok, but this would still be much easier with a script. How are you telling the other objects that they've been hit, or that something is close? Because that method could be very important to your game objects.
Scripts
the script below uses a raycast to detect if an enemy is in range, if it is then the enemy takes damage.
function Update () { if (Input.GetButtonDown("Fire1")) { The$$anonymous$$ace.animation.Play("Attack2"); var hit : RaycastHit; if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit)) { Distance = hit.distance; if (Distance < $$anonymous$$axDistance) { hit.transform.Send$$anonymous$$essage("ApplyDammage", TheDammage, Send$$anonymous$$essageOptions.DontRequireReceiver); } } }
the script below calls the applydamage function from above and applys damage to the enemy
function Update () { if(Health <= 0) { Dead(); } }
function ApplyDammage (TheDammage : int) { Health -= TheDammage; }
function Dead() { Destroy (gameObject); }
the script below tells the enemy to attack the player, which works fine on a character model
function Start () { timeCheck = false; Ewaitseconds = Random.Range(EA$$anonymous$$Wait, EAmaxWait); }
function Update () { var Ehit : RaycastHit; if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), Ehit)) { Edistance = Ehit.distance; if (Edistance < E$$anonymous$$axDistance) { Ewaittime(); if(timeCheck) { Ehit.transform.Send$$anonymous$$essage("ApplyPlayerDammage", Edammage, Send$$anonymous$$essageOptions.DontRequireReceiver); timeCheck = false; } } } }
if(!timeCheck) { yield WaitForSeconds(Ewaitseconds); timeCheck = true; }
function Ewaittime() { yield WaitForSeconds(Ewaitseconds); timeCheck = true; }
The comment made the scripts unbarable to read. Apologies.
Answer by sparkzbarca · Jan 03, 2014 at 03:47 AM
based on the small amount of info provided i'd say your imported model lacks a collider.
A cube has one by default your imported model may not.
I have added colliders and that doesn't work. To get a little more specific I downloaded a character from the asset store and when i imported it into unity it had a parent object that was empty and two objects under that. One that was an object that only had one component which was a skin mesh renderer, and another that had editable geometry such for rotating hands etc. Ive tried everything and my script just doesn't work on it. The gameObject is not being destroyed even though it works on a simple cube or capsule.
your not using mesh colliders are you?
are you perhaps destroying something other than the parent, perhaps just a part of it.
maybe destroy(objecttobekilled.transform.root);
are you using on collision enter to detect and destory stuff?
did you debug.log what you hit to see if collisions are being detected?
it might be best to simply dropbox your project
Im not using $$anonymous$$esh colliders. Not destroying anything other than the parent. Tried the code with a transform variable and it didn't work. Not using on collision enter. I did debug log and the collision from the raycast is a success on objects such as capsules, cubes, ect. but not successful on imported objects. I now know that raycast is not picking up imported objects, what may cause that?
Answer by justinc847 · Jan 03, 2014 at 07:15 AM
I've solved the problem, turns out it was because the colliders I added weren't big enough. Thanks for your help everyone!
Your answer
Follow this Question
Related Questions
Sharing animations with models 2 Answers
How can I find the position of my weapon? 1 Answer
How i can setup Character 0 Answers
How to make Pixel Graphics? 0 Answers
Character Animation Help 0 Answers