the ray is offset by like 50 units to the right and even when it does happen to hit the player i have an error saying cannot cast ray from source to destination.
zombie code:
using UnityEngine;
using System.Collections;
public class Zombie : MonoBehaviour {
public int Speed;
public GameObject PlayerGet;
public float MinPointStopDistance;
public float MaxPointStopDistance;
public float PointStopDistance;
private ArrayList PathPointGet;
public int CurrentPathPointGet;
public Vector3 TargetPos;
public bool CannotFollow;
private CapsuleCollider ColliderGet;
void Start () {
ColliderGet = GetComponent <CapsuleCollider> ();
TargetPos = transform.position;
PointStopDistance = Random.Range (MinPointStopDistance, MaxPointStopDistance);
PathPointGet = PlayerGet.GetComponent <PlayerController1> ().PathPoint;
CurrentPathPointGet = PlayerGet.GetComponent <PlayerController1> ().CurrentPathPoint;
}
void Update () {
Debug.Log (PlayerGet.transform.position);
CannotFollow = PlayerGet.GetComponent <PlayerController2> ().StopZombies;
Ray RayToPlayer = new Ray (new Vector3 (transform.position.x, transform.position.y, transform.position.z), new Vector3 (PlayerGet.transform.position.x, PlayerGet.transform.position.y + PlayerGet.GetComponent <CapsuleCollider> ().height / 2, PlayerGet.transform.position.z));
Debug.DrawRay (transform.position, PlayerGet.transform.position, Color.yellow);
RaycastHit HitPoint;
if (CannotFollow == false)
{
Vector3.MoveTowards (transform.position, TargetPos, Speed * Time.deltaTime);
transform.LookAt (TargetPos);
if (Physics.Raycast (RayToPlayer, out HitPoint, Mathf.Infinity))
{
if (HitPoint.collider.tag == "Player")
{
Debug.Log ("YES");
TargetPos = PlayerGet.transform.position;
} else {
if (transform.position.x < TargetPos.x + PointStopDistance || transform.position.x > TargetPos.x - PointStopDistance)
{
if (transform.position.y < TargetPos.y + PointStopDistance || transform.position.y > TargetPos.y - PointStopDistance)
{
if (transform.position.z < TargetPos.z + PointStopDistance || transform.position.z > TargetPos.z - PointStopDistance)
{
CurrentPathPointGet ++;
PathPointGet.Add (1);
TargetPos = (Vector3) PathPointGet [CurrentPathPointGet];
}
}
}
}
}
}
}
}
player code:
using UnityEngine;
using System.Collections;
public class PlayerController1 : MonoBehaviour {
public int Speed;
public ArrayList PathPoint;
public int CurrentPathPoint;
public Vector3 LastKnownPosition;
public bool CanMoveForward;
public bool CanMoveBack;
public bool CanMoveRight;
public bool CanMoveLeft;
private CapsuleCollider ColliderGet;
public float Sensitivity;
private float MouseX;
private float MouseXSet;
public float LookSmoothness;
private Animator AnimatorGet;
void Start () {
PathPoint = new ArrayList ();
ColliderGet = GetComponent <CapsuleCollider> ();
AnimatorGet = GetComponent <Animator> ();
}
void Update () {
MouseX += Input.GetAxis ("Mouse X") * Sensitivity * 100 * Time.deltaTime;
if (MouseXSet != MouseX)
{
transform.rotation = Quaternion.AngleAxis (MouseXSet, Vector3.up);
MouseXSet = Mathf.Lerp (MouseXSet, MouseX, LookSmoothness);
}
if (Input.GetKeyUp (KeyCode.W))
{
AnimatorGet.SetBool ("Walking", false);
}
}
void FixedUpdate () {
if (Vector3.Distance (LastKnownPosition, transform.position) >= 1)
{
PathPoint.Add (transform.position);
PathPoint [CurrentPathPoint] = transform.position;
CurrentPathPoint ++;
LastKnownPosition = transform.position;
}
MouseX += Input.GetAxis ("Mouse X") * Sensitivity * 200 * Time.deltaTime;
if (MouseXSet != MouseX)
{
transform.rotation = Quaternion.AngleAxis (MouseX, Vector3.up);
MouseXSet = Mathf.Lerp (MouseXSet, MouseX, Sensitivity * LookSmoothness);
}
Ray RightRay = new Ray (new Vector3 (transform.position.x, transform.position.y + ColliderGet.height / 2, transform.position.z), transform.right);
Ray LeftRay = new Ray (new Vector3 (transform.position.x, transform.position.y + ColliderGet.height / 2, transform.position.z), -transform.right);
Ray ForwardRay = new Ray (new Vector3 (transform.position.x, transform.position.y + ColliderGet.height / 2, transform.position.z), transform.forward);
Ray BackRay = new Ray (new Vector3 (transform.position.x, transform.position.y + ColliderGet.height / 2, transform.position.z), -transform.forward);
RaycastHit RightHitPoint;
RaycastHit LeftHitPoint;
RaycastHit ForwardHitPoint;
RaycastHit BackHitPoint;
if (Physics.Raycast (ForwardRay, out ForwardHitPoint, 2))
{
if (ForwardHitPoint.collider.tag == "Wall")
{
CanMoveForward = false;
} else {
CanMoveForward = true;
}
} else {
CanMoveForward = true;
}
if (Physics.Raycast (BackRay, out BackHitPoint, 2))
{
if (BackHitPoint.collider.tag == "Wall")
{
CanMoveBack = false;
} else {
CanMoveBack = true;
}
} else {
CanMoveBack = true;
}
if (Physics.Raycast (RightRay, out RightHitPoint, 2))
{
if (RightHitPoint.collider.tag == "Wall")
{
CanMoveRight = false;
} else {
CanMoveRight = true;
}
} else {
CanMoveRight = true;
}
if (Physics.Raycast (LeftRay, out LeftHitPoint, 2))
{
if (LeftHitPoint.collider.tag == "Wall")
{
CanMoveLeft = false;
} else {
CanMoveLeft = true;
}
} else {
CanMoveLeft = true;
}
if (Input.GetKey (KeyCode.W) && CanMoveForward == true)
{
AnimatorGet.SetBool ("Walking", true);
rigidbody.transform.position += transform.forward * Speed * Time.deltaTime;
}
if (Input.GetKeyUp (KeyCode.W) || CanMoveForward == false)
{
AnimatorGet.SetBool ("Walking", false);
}
if (Input.GetKey (KeyCode.S) && CanMoveBack == true)
{
rigidbody.transform.position -= transform.forward * Speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.D) && CanMoveRight == true)
{
rigidbody.transform.position += transform.right * Speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.A) && CanMoveLeft == true)
{
rigidbody.transform.position -= transform.right * Speed * Time.deltaTime;
}
}
}