how to make Enemy AI to spot player position and hit his hand at that position C#
I want to make a Big Monster that spots the player last position and at that last position Monster Hit his Hand at that players last know position. Please Help me with the scripting part tell me how can i do it and please provide me a sample of script C#. Big Monster just stand and looks if he spots player monster only move his hand and hit his hand at that specific position only and then repeat to spot player.
public enum State { Idle, Attack, Investigate }
public GameObject target;
public State state;
private bool alive;
// Variables for Investigate
// Variable for Idle
// Variable for Attack
// Use this for initialization
void Start ()
{
state = EBasicAI.State.Investigate;
alive = true;
StartCoroutine("FSM");
}
// Update is called once per frame
void Update () {
}
IEnumerator FSM()
{
while (alive)
{
switch (state)
{
case State.Investigate:
Investigate();
break;
case State.Attack:
Attack();
break;
}
yield return null;
}
}
void Investigate()
{
Debug.Log("Investigate");
}
void Attack()
{
Debug.Log("Attacking");
}
void OnTriggerEnter(Collider coll)
{
if (coll.tag == "Player")
{
state = EBasicAI.State.Attack;
target = coll.gameObject;
}
//else
//{
// state = EBasicAI.State.Investigate;
// }
}
void OnTriggerExit(Collider coll)
{
if(coll.tag == "Player")
{
state = EBasicAI.State.Investigate;
}
}
I Created the states but how enemy find player last position and Hit his hand to that know position that i want please help me!!
This is quite a feature. I would advise to split this into smaller requests/questions.
How to store/locate player's last known position (AI)
How to bring a character's hand to a given position (I$$anonymous$$)
You'll have better chances to find an expert in each area to answer those questions.
How shall the enemy Spot the Player? more Informations needed.
Shall the head move to the player or just spot the player and hit him...
also has the player a possibility to hide so that the last position is diffrent to the last on...
let me know if my solution helped yor or if it need adjusments
Yep! this solution helped. Now enemy can spot player position. Thank you so much! please help me with enemy can hit his hand at that player position only.
Answer by StonedLover · Jul 15, 2017 at 08:00 AM
As an Example fo spotting the player: make a empty Gameobject infornt of the face of the Enemy then in the enemy script reference to this GO and make a Raycast after a LookAt and check if the player is visible
Vector3 lastPlayerPos;
RaycastHit hit;
emptyGo.transform.LookAt(Player.transform);
if (Physics.Raycast(emptyGo.transform.position, Vector3.forward, out hit))
{
if (hit.transform.tag.Equals("Player"))
{
Debug.Log("Detected the Player");
lastPlayerPos = hit.transform.position;
}
}
if you want a max distance it would be:
Vector3 lastPlayerPos;
RaycastHit hit;
emptyGo.transform.LookAt(Player.transform);
if (Physics.Raycast(emptyGo.transform.position, Vector3.forward, out hit, Distance in float))
{
if (hit.transform.tag.Equals("Player"))
{
Debug.Log("Detected the Player");
lastPlayerPos = hit.transform.position;
}
}
Your answer
Follow this Question
Related Questions
Isometric movement for MoveTowards 0 Answers
Bugs on move in air(falling); 0 Answers
moving an object continuously between waypoints 0 Answers
2D Object following the cursor without glitchyness at the center 0 Answers
Game Object jumps to z-axis 0 Answers