- Home /
Shooting script no working correctly
Here's the script:
var shotSound: AudioClip; // drag a shot sound here, if any
var bloodPrefab: GameObject; // drag the blood prefab here, if any
var sparksPrefab: GameObject; // drag the sparks prefab here, if any
var effect : GameObject;
var waterPrefab : GameObject;
function Shoot(){
if (shotSound) audio.PlayOneShot(shotSound); // play the shot sound
var hit: RaycastHit;
if (Physics.Raycast(transform.position, transform.forward, hit)){
// prepare rotation to instantiate blood or sparks
var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
if (hit.transform.tag == "Enemy"){ // if enemy hit...
if (bloodPrefab) Instantiate(bloodPrefab, hit.point, rot); // make it bleed...
hit.collider.gameObject.GetComponent(Animator).SetBool("Dying", true);
hit.Destroy(CharacterController);
} // and consume its health
if (hit.transform.tag == "Water"){
Instantiate(waterPrefab, hit.point, rot);
}
else { // otherwise emit sparks at the hit point
if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
}
}
}
function Update(){
if (Input.GetButtonDown("Fire2")){
Shoot();
var fx : GameObject = Instantiate(
effect, transform.position, transform.rotation );
}
}
When I shoot the enemy, this error message pops up:
MissingMethodException: UnityEngine.RaycastHit.Destroy
Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.ProduceExtensionDispatcher ()
Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.Create ()
Boo.Lang.Runtime.RuntimeServices.DoCreateMethodDispatcher (System.Object target, System.Type targetType, System.String name, System.Object[] args)
Boo.Lang.Runtime.RuntimeServices.CreateMethodDispatcher (System.Object target, System.String name, System.Object[] args)
Boo.Lang.Runtime.RuntimeServices+<Invoke>c__AnonStorey15.<>m__9 ()
Boo.Lang.Runtime.DynamicDispatching.DispatcherCache.Get (Boo.Lang.Runtime.DynamicDispatching.DispatcherKey key, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args)
UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args, System.Type scriptBaseType)
RayCast.Shoot () (at Assets/Standard Assets/Tutorial de shooter/RayCast.js:16)
RayCast.Update () (at Assets/Standard Assets/Tutorial de shooter/RayCast.js:29)
I know the problem is in this line
hit.Destroy(CharacterController);
but I don't know how to solve it. Please can anybody help me???
PD: Sorry for my bad English
Answer by robertbu · Jan 17, 2014 at 05:48 PM
You are trying to destroy the character controller attached to this specific game object? Try this:
Destroy(hit.collider.GetComponent(CharacterController));
If for some reason you have trouble with this, you can do:
hit.collider.GetComponent(CharacterConroller).enabled = false;
This will disable the character controller, not destroy it.
How do I check if the enemy has a character cotroller from the script? I get this error:
$$anonymous$$issingComponentException: There is no 'CharacterController' attached to the "GameObject" game object, but a script is trying to access it.
You probably need to add a CharacterController to the game object "GameObject". Or your script needs to check if the component is attached before using it.
RayCast right.Shoot () (at Assets/Standard Assets/Tutorial de shooter/RayCast right.js:16)
RayCast right.Update () (at Assets/Standard Assets/Tutorial de shooter/RayCast right.js:29)
I've tried both thing you said
Now I understand what you were trying to do. I edited my answer. If you are not sure if the enemy has a CharacterController, you can do:
var cc = hit.collider.GetComponent(CharacterConroller);
if (cc != null)
Destroy(cc);
Your answer
Follow this Question
Related Questions
FPS PISTOL 1 Answer
How do I make a gun project a particle? 1 Answer
Where can i find an easy raycast shooting tutorial for C#? 2 Answers
Shoot Projectile 0 Answers
Raycast Positioning 2 Answers