I just started working on 3d game development in unity and tried to copy a script tutorial I found on YouTube for basic enemy detection (https://www.youtube.com/watch?v=9ts9qXt8o2k). The tutorial goes back into Unity at minute 8 without any problems but I keep getting the error stated in the title.
Here is the exact script I have at the moment (Unity states the error at 47,36 aka the if(Physics.Raycast line):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float Maxspeed;
private float Speed;
private Collider[] hitColliders;
private RaycastHit Hit;
public float SightRange;
public float DetectionRange;
public Rigidbody rb;
public GameObject Target;
private bool seePlayer;
// Start is called before the first frame update
void Start()
{
Speed = Maxspeed;
}
// Update is called once per frame
void Update()
{
// detect if a player is in range
if (!seePlayer)
{
hitColliders = Physics.OverlapSphere(transform.position, DetectionRange);
foreach (var HitCollider in hitColliders)
{
if(HitCollider.tag == "Player")
{
Target = HitCollider.gameObject;
seePlayer = true;
}
}
}
else
{
if(Physics.RayCast(transform.position, (Target.transform.position - transform.position), out Hit, SightRange))
{
if(Hit.collider.tag != "Player")
{
seePlayer = false;
}
else
{
//calculate the player's direction
var Heading = Target.transform.position - transform.position;
var Distance = Heading.magnitude;
var Direction = Heading / Distance;
Vector3 Move = new Vector3(Direction.x * Speed, 0, Direction.z * Speed);
rb.velocity = Move;
transform.forward = Move;
}
}
}
}
}
The title is the entire name for the error I received minus the time of the error and the file name. I have tried changing line 47 to start with if(UnityEngine.Physics.RayCast but still got the error.
LilTimmy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.