- Home /
SendMessage/BroadcastMessage has no receiver EnemyAI to NPCMovement
here is the movement script:
[RequireComponent(typeof(CharacterController))]
public class NPCMovement : MonoBehaviour
{
public enum State
{
_Idle,
_Initialize,
_Setup,
_Action
}
public enum MoveVertical
{
Down = -1,
None = 0,
Up = 1
}
public MoveVertical _MoveVertical = 0;
public enum MoveHorizontal
{
Left = -1,
None = 0,
Right = 1
}
public MoveHorizontal _MoveHorizontal = 0;
//Enemy Movement
public Transform _NPCPrefab;
public CharacterController NPCMove;
private Vector2 _Move = Vector2.zero;
//Bools
public bool Run = false;
public bool Slow = false;
//Move Speeds
public float NPCSlowSpeed = 0.75f;
public float NPCWalkSpeed = 1.5f;
public float NPCRunSpeed = 3.0f;
private State _State;
//------------------------------------------
void Awake ()
{
_NPCPrefab = transform;
NPCMove = this.GetComponent<CharacterController>();
_State = NPCMovement.State._Initialize;
}
//------------------------------------------
IEnumerator Start ()
{
while(true)
{
switch(_State)
{
case State._Initialize:
Initialize();
break;
case State._Setup:
Setup();
break;
case State._Action:
Action();
break;
default:
break;
}
yield return null;
}
}
//------------------------------------------
void Update ()
{
}
void Action()
{
Moving();
}
void Initialize()
{
if(!GetComponent<CharacterController>())
{
return;
}
_State = NPCMovement.State._Setup;
}
void Setup()
{
_Move = Vector2.zero;
Run = false;
_State = NPCMovement.State._Action;
}
private void Moving()
{
_Move = new Vector2((int)_MoveHorizontal, (int)_MoveVertical);
_Move = _NPCPrefab.TransformDirection(_Move).normalized;
_Move *= NPCWalkSpeed;
NPCMove.Move(_Move * Time.deltaTime);
}
}
here is the section I'm getting the error in my enemy ai script:
if(distance >= StopRange)
{
if(Direction.x > 0 && Direction.y > 0)
{
BroadcastMessage("MoveHorizontal", NPCMovement.MoveHorizontal.Right);
BroadcastMessage("MoveVertical", NPCMovement.MoveVertical.Up);
}
if (Direction.x < 0 && Direction.y < 0)
{
BroadcastMessage("MoveHorizontal", NPCMovement.MoveHorizontal.Left);
BroadcastMessage("MoveVertical", NPCMovement.MoveVertical.Down);
}
}
ive used both send and broadcast but get simular errors I can move the enemy manully thru the inspector
Answer by xephin2004 · Oct 06, 2015 at 06:39 PM
so instead of trying to send a message to my enums, I'm guess you cant, I made indivdual functions for moving up left right and down and used sendmessage to those in NPCMovement
public void MoveUp()
{
_Move = new Vector2((int)MoveHorizontal.None, (int)MoveVertical.Up);
_Move = _NPCPrefab.TransformVector(_Move).normalized;
_Move *= NPCWalkSpeed;
NPCMove.Move(_Move * Time.deltaTime);
}
and in enemy AI
Vector2 Direction = (Target.transform.position - Enemy.position);
float distance = Vector2.Distance(Target.position, Enemy.position);
if(distance >= StopRange)
{
if(Direction.x > 0 && Direction.y > 0)
{
SendMessage("MoveRight", NPCMovement.MoveHorizontal.Right);
SendMessage("MoveUp", NPCMovement.MoveVertical.Up);
}
the enemy now follows the player around till the player is out of range just as intended
Answer by GlatiatorRX · Oct 06, 2015 at 05:11 PM
It looks like you do not have an actual function named "MoveVertical" or "MoveHorizontal" in your code. You have enums defined, but in order for BroadcastMessage, you need to input a function name in those quotations. Not just enum names.
If you are looking to simply update the vertical or horizontal values you have there, make a setter call kind of function. So like this:
public void MoveNPCHorizontal(NPCMovement.MoveHorizontal inHorizontalMovement)
{
_MoveHorizontal = inHorizontalMovement;
}
public void MoveNPCVertical(NPCMovement.MoveVertical inVerticalMovement)
{
_MoveVertical = inVerticalMovement;
}
And then call the function like this:
if(Direction.x > 0 && Direction.y > 0)
{
BroadcastMessage("MoveNPCHorizontal", NPCMovement.MoveHorizontal.Right);
BroadcastMessage("MoveNPCVertical", NPCMovement.MoveVertical.Up);
}
if (Direction.x < 0 && Direction.y < 0)
{
BroadcastMessage("MoveNPCHorizontal", NPCMovement.MoveHorizontal.Left);
BroadcastMessage("MoveNPCVertical", NPCMovement.MoveVertical.Down);
}
I have not used broadcast message much, so I cannot say if it will work, but if it does, great! If not, I'll be around more if there is still an issue. Good luck!
~ Matt
Good tip!
Btw, your answer should take the praise. Not the issuer of the question that read your answer and then posted the corrected code, and never thanked!
Your answer
Follow this Question
Related Questions
How to forbid an NPC to move diagonally using Character Controller? 1 Answer
How to check if a character hasn't changed its position in the last x seconds? 1 Answer
Controlling AI Movement 0 Answers
Mecanim Root Motion -- Not Moving Forward? 1 Answer
Should you use CharacterController.Move or SimpleMove for enemies? 1 Answer