The layermask layer number looks like it matches in the layer menu(I doesn’t even matter what layer I set my raycast to detect, it still hits the player) However, if I disable raycast detection on my player it hit’s intended target behind the player? I’m lost.
using UnityEngine;
using System.Collections;
public class BasicEnemyAiV2 : MonoBehaviour {
public GameObject Player;
public Vector3 FirstLerpPoint;
private Vector3 FinalLerpPoint;
public bool StrikedAtPlayer;
public float Timer;
public float TimerStart;
public float DistanceInRay;
public float speed;
public float LerpTimer;
public float DistanceToEngage;
private bool OneTimeExecution = true;
public Animator anim;
static private Dash PlayerMovementScript;
void Start ()
{
Player = GameObject.FindGameObjectWithTag("Player");
TimerStart = Timer;
PlayerMovementScript = Player.GetComponent<Dash>();
}
public Vector3 PlayerNewPosition;
public bool ThePlayerIsMoving;
// Update is called once per frame
void Update ()
{
ThePlayerIsMoving = PlayerMovementScript.IsMoving;
PlayerNewPosition = PlayerMovementScript.newPosition;
//Debug.Log(Vector3.Distance(FirstLerpPoint, FinalLerpPoint));
if (Timer <= 0)
{
//Note to self: Keep this commented out for a potential lancing/prediction based enemy
/*if (ThePlayerIsMoving && OneTimeExecution == true)
{
Vector3 rayDir = PlayerNewPosition - transform.position;
Ray ray = new Ray(transform.position, rayDir);
FirstLerpPoint = ray.origin + (ray.direction * DistanceInRay);
Debug.DrawRay(transform.position, rayDir, Color.black, 3);
FinalLerpPoint = PlayerNewPosition;
StartCoroutine("LerpToPos");
OneTimeExecution = false;
}*/
if (OneTimeExecution == true)
{
anim.Play("DaggerChargeUp");
RaycastHit hit;
Vector3 rayDir = Player.transform.position - transform.position;
Ray ray = new Ray(transform.position, rayDir);
if (Physics.Raycast(ray, out hit, 1 << 8))
{
Debug.Log(hit.collider.gameObject);
FinalLerpPoint = hit.point;
}
FirstLerpPoint = ray.origin + (ray.direction * DistanceInRay);
Debug.DrawRay(transform.position, rayDir, Color.black, 3);
StartCoroutine("LerpToPos");
OneTimeExecution = false;
}
}
else
{
transform.LookAt(Player.transform);
Timer -= Time.deltaTime;
}
}
private IEnumerator LerpToPos()
{
StrikedAtPlayer = true;
while (Vector3.Distance(FirstLerpPoint, FinalLerpPoint) >= 2)
{
RaycastHit hits;
Vector3 rayDir = Player.transform.position - transform.position;
Ray PlayerInRangeRay = new Ray(transform.position, rayDir);
if (Physics.Raycast(PlayerInRangeRay, out hits, DistanceToEngage, 1 << 9))
{
LerpTimer = 0;
yield return StartCoroutine(PlayerCombatEncounter());
}
LerpTimer += Time.deltaTime;
transform.position = FirstLerpPoint;
FirstLerpPoint = Vector3.Lerp(FirstLerpPoint, FinalLerpPoint, LerpTimer * speed);
yield return null;
}
Timer = TimerStart;
LerpTimer = 0;
OneTimeExecution = true;
anim.SetTrigger("RotationIsDone");
}
private IEnumerator PlayerCombatEncounter()
{
if (anim.GetCurrentAnimatorStateInfo(0).IsName("DaggerChargeUp") && StrikedAtPlayer)
{
StrikedAtPlayer = false;
anim.SetTrigger("InProximity");
Debug.Log("Animation called");
if (Player != null)
{
anim.SetTrigger("MissStrike");
}
}
yield return null;
}
}