I am very inexpereinced with Raycasts but i know they are one of the ways to make a gun 'shoot'. I want to do this for the simple reason that i wants to reduce clutter in the scene (i know how to destroy a object after it hits something or even over time, but it just seems messy to use GameObjects for bullets.) So i decided to try and make a raycast shooting script. Here is what i got so far:
var par : ParticleEmitter;
var damage = 2;
function Update () {
var hit : RaycastHit;
if (Input.GetMouseButtonDown(0)) {
if (Physics.Raycast (transform.position,transform.forward,hit, 100)) {
Instantiate(par, hit.transform.position, transform.rotation);
if (hit.collider.gameObject.tag == "Enemy") {
HealthAI.can_die(damage);
}
}
}
}
Now the whole 'damage' and 'if (hit.collider.gameObject.tag == "Enemy")' thing is where i have the problem. I wish to make it so that when my ray hits something established as 'Enemy' it will access the function 'can_die' in the script 'HealthAI', here is that script.
static var aihitPoints = 10;
static function can_die ( damage ) {
aihitPoints -= damage;
if(aihitPoints < 1 ) {
Destroy(gameObject, 0.0);
}
}
function breakObj (damage) {
Destroy(gameObject, 0.0);
}
Now the whole problem with this is, A) It gives me a error saying the CapsuleCollider is not a memeber of other.SendMessage. B) gameObject is not a non static member. I have no idea how to fix this.
Also, when i instantiate the 'par' ParticleEmitter it always instanitates in the same direction, regardless of where it hits, any way to fix that? Quaternion.identity does not work, nor does transform.rotation. I want it so the sparks appear on the side the object was hit on.
Lastly, i want the ray to be only in the center of the screen, not the whole side of the camera (where i have the script attached) any way to make it do that? Mostly, just so when i point the reticule at something it hits just that.