problems with raycast

SOLUTION:
the physics raycast should have been like this
My enemy script contains a void name EnemyMelleeDamaged

This is the raycast im using

public void MeleeAttack() {
		canAttack = false;
		RaycastHit hit;
		Ray rightRay = new Ray(transform.position, Vector3.right);
		Ray leftRay = new Ray(transform.position, -Vector3.right);
		
		if(lookRight){
			if(Physics.Raycast(rightRay, out hit, attackRange))
			{
				if(hit.collider.gameObject.tag == "Enemy"){
					gameObject.SendMessage("EnemyMelleeDamaged",damageValue,SendMessageOptions.DontRequireReceiver);
					Debug.Log ("Hit Right");
						}
				}
		}else{
			if(Physics.Raycast(leftRay, out hit,attackRange)){
				if(hit.collider.gameObject.tag == "Enemy"){
					gameObject.SendMessage("EnemyMelleeDamaged",damageValue,SendMessageOptions.DontRequireReceiver);
						Debug.Log ("Hit Left");
							}
						}	
					}
				}

when the enemy is within ranged i cant Hit Right or Hit Left printed but the enemies dont get damaged , any idea why?

Sounds like your messages are not getting through. Check the callbacks have the correct name and remove the don’t require receiver to confirm this.

K

public void EnemyMelleeDamaged(int damageValue){
		if(enemyHealth > 0){
			enemyHealth -= damageValue;
			TakenDamage();
		}
	}

Is EnemyMelleeDamaged() being called? You could try a quick Debug.Log() call to see if anything shows up in the log.

Possible problems:

  • Maybe you’re calling an event that doesn’t exist?
  • Maybe your event exists, but not in any script that’s attached to your target?
  • Maybe your raycast is finding the wrong GameObject?
  • If you’re sure you have the right GameObject, and you’re sure the event is being called, maybe the enemy’s health is <= 0 when it’s called?

Always check your assumptions. Make sure you have the right target, that it has the script attached, that your event is being called… a pretty big part of debugging is narrowing down the problem.

I added a print function to the event , it’s never called , I still think that the problem lies here

but I cant seem to find a solution…

What type is damage value? Is it an int or a float?

Try changing the message handler to take a float:

public void EnemyMelleeDamaged(float damageValue){

K

Youre not referencing the intended target…

Youre saying gameObject.SendMessage, which refers to the GameObject running the MeleeAttack function

what I assume youre trying to do is this…

hit.collider.gameObject.SendMessage(blah blah blah)