- Home /
Question by
Therian13 · Jan 11, 2015 at 06:04 PM ·
raycastlayersraycasthitignore
Need raycast to target child, not parent.
Hello Everyone, I have a melee weapon script that uses a raycast to send damage to a target. The problem I am having though, is it keeps targeting the parent of the object. The parent is a empty game object with no colliders, but it still targets it, instead of the actual target. (its a ship, with different components that can be attacked). I've tried using layermasks, but it doesn't seem to work. It still targets the empty game object. Would someone please take a look at my script and let me know what I am doing wrong? thank you!
#pragma strict
var TheDamage : int = 50;
var Distance : float;
var MaxDistance : float = 3.0;
var TheAnimator : Animator;
var DamageDelay : float = 0.6;
var knifeHit: AudioClip;
var knifeSwing1: AudioClip;
var knifeSwing2: AudioClip;
var knifeHitDetect :LayerMask = 14;
private var knifeCanAttack :boolean = true;
private var Swing01Streak = 0;
private var Swing02Streak = 0;
var disableAttackknife : disableAttacks;
function Update ()
{
TheAnimator.SetBool ("speargun", false);
if (Input.GetButtonDown("Fire1") && disableAttackknife.CanAttack == true && (knifeCanAttack == true))
{
AttackDamage ();
disableAttackknife.CanAttack = false;
}
}
function AttackDamage ()
{
knifeCanAttack = false;
if (Random.value >= 0.5 && Swing01Streak <= 2)
{
TheAnimator.SetBool ("firing",true);
Swing01Streak += 1;
Swing02Streak = 0;
yield WaitForSeconds (0.5);
audio.PlayOneShot(knifeSwing1);
}
else
{
if (Swing02Streak <= 2)
{
TheAnimator.SetBool ("firing", true);
Swing01Streak = 0;
Swing02Streak += 1;
yield WaitForSeconds (0.5);
audio.PlayOneShot(knifeSwing2);
}
else
{
TheAnimator.SetBool ("firing", true);
Swing01Streak += 1;
Swing02Streak = 0;
yield WaitForSeconds (0.5);
audio.PlayOneShot(knifeSwing1);
}
}
yield WaitForSeconds (DamageDelay);
//Actual attacking
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (Vector3(Screen.width/2,Screen.height/2,0));
if (Physics.Raycast (ray, hit,100,knifeHitDetect.value))
{
Debug.Log(hit.transform);
Distance = hit.distance;
if (Distance < MaxDistance)
{
Debug.Log(hit.transform);
hit.transform.SendMessage ("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
audio.PlayOneShot(knifeHit);
}
}
TheAnimator.SetBool("firing",false);
TheAnimator.SetBool("firing",false);
knifeCanAttack = true;
disableAttackknife.CanAttack = true;
}
Comment