I get this error when trying this code on my 2D game:
Assets\Scripts\Enemy Spawn\FlockUnit.cs(28,26): error CS0246: The type or namespace name 'Flock' could not be found (are you missing a using directive or an assembly reference?)
Assets\Scripts\Enemy Spawn\FlockUnit.cs(16,10): error CS0246: The type or namespace name 'Flock' could not be found (are you missing a using directive or an assembly reference?)
This is the script:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlockUnit : MonoBehaviour
{
[SerializeField] private float FOVAngle;
[SerializeField] private float smoothDamp;
[SerializeField] private LayerMask obstacleMask;
[SerializeField] private Vector3[] directionsToCheckWhenAvoidingObstacles;
private List<FlockUnit> cohesionNeighbours = new List<FlockUnit>();
private List<FlockUnit> avoidanceNeighbours = new List<FlockUnit>();
private List<FlockUnit> aligementNeighbours = new List<FlockUnit>();
private Flock assignedFlock;
private Vector3 currentVelocity;
private Vector3 currentObstacleAvoidanceVector;
private float speed;
public Transform myTransform { get; set; }
private void Awake()
{
myTransform = transform;
}
public void AssignFlock(Flock flock)
{
assignedFlock = flock;
}
public void InitializeSpeed(float speed)
{
this.speed = speed;
}
public void MoveUnit()
{
FindNeighbours();
CalculateSpeed();
var cohesionVector = CalculateCohesionVector() * assignedFlock.cohesionWeight;
var avoidanceVector = CalculateAvoidanceVector() * assignedFlock.avoidanceWeight;
var aligementVector = CalculateAligementVector() * assignedFlock.aligementWeight;
var boundsVector = CalculateBoundsVector() * assignedFlock.boundsWeight;
var obstacleVector = CalculateObstacleVector() * assignedFlock.obstacleWeight;
var moveVector = cohesionVector avoidanceVector aligementVector boundsVector obstacleVector;
moveVector = Vector3.SmoothDamp(myTransform.forward, moveVector, ref currentVelocity, smoothDamp);
moveVector = moveVector.normalized * speed;
if (moveVector == Vector3.zero)
moveVector = transform.forward;
myTransform.forward = moveVector;
myTransform.position = moveVector * Time.deltaTime;
}
private void FindNeighbours()
{
cohesionNeighbours.Clear();
avoidanceNeighbours.Clear();
aligementNeighbours.Clear();
var allUnits = assignedFlock.allUnits;
for (int i = 0; i < allUnits.Length; i )
{
var currentUnit = allUnits[i];
if (currentUnit != this)
{
float currentNeighbourDistanceSqr = Vector3.SqrMagnitude(currentUnit.myTransform.position - myTransform.position);
if (currentNeighbourDistanceSqr <= assignedFlock.cohesionDistance * assignedFlock.cohesionDistance)
{
cohesionNeighbours.Add(currentUnit);
}
if (currentNeighbourDistanceSqr <= assignedFlock.avoidanceDistance * assignedFlock.avoidanceDistance)
{
avoidanceNeighbours.Add(currentUnit);
}
if (currentNeighbourDistanceSqr <= assignedFlock.aligementDistance * assignedFlock.aligementDistance)
{
aligementNeighbours.Add(currentUnit);
}
}
}
}
private void CalculateSpeed()
{
if (cohesionNeighbours.Count == 0)
return;
speed = 0;
for (int i = 0; i < cohesionNeighbours.Count; i )
{
speed = cohesionNeighbours[i].speed;
}
speed /= cohesionNeighbours.Count;
speed = Mathf.Clamp(speed, assignedFlock.minSpeed, assignedFlock.maxSpeed);
}
private Vector3 CalculateCohesionVector()
{
var cohesionVector = Vector3.zero;
if (cohesionNeighbours.Count == 0)
return Vector3.zero;
int neighboursInFOV = 0;
for (int i = 0; i < cohesionNeighbours.Count; i )
{
if (IsInFOV(cohesionNeighbours[i].myTransform.position))
{
neighboursInFOV ;
cohesionVector = cohesionNeighbours[i].myTransform.position;
}
}
cohesionVector /= neighboursInFOV;
cohesionVector -= myTransform.position;
cohesionVector = cohesionVector.normalized;
return cohesionVector;
}
private Vector3 CalculateAligementVector()
{
var aligementVector = myTransform.forward;
if (aligementNeighbours.Count == 0)
return myTransform.forward;
int neighboursInFOV = 0;
for (int i = 0; i < aligementNeighbours.Count; i )
{
if (IsInFOV(aligementNeighbours[i].myTransform.position))
{
neighboursInFOV ;
aligementVector = aligementNeighbours[i].myTransform.forward;
}
}
aligementVector /= neighboursInFOV;
aligementVector = aligementVector.normalized;
return aligementVector;
}
private Vector3 CalculateAvoidanceVector()
{
var avoidanceVector = Vector3.zero;
if (aligementNeighbours.Count == 0)
return Vector3.zero;
int neighboursInFOV = 0;
for (int i = 0; i < avoidanceNeighbours.Count; i )
{
if (IsInFOV(avoidanceNeighbours[i].myTransform.position))
{
neighboursInFOV ;
avoidanceVector = (myTransform.position - avoidanceNeighbours[i].myTransform.position);
}
}
avoidanceVector /= neighboursInFOV;
avoidanceVector = avoidanceVector.normalized;
return avoidanceVector;
}
private Vector3 CalculateBoundsVector()
{
var offsetToCenter = assignedFlock.transform.position - myTransform.position;
bool isNearCenter = (offsetToCenter.magnitude >= assignedFlock.boundsDistance * 0.9f);
return isNearCenter ? offsetToCenter.normalized : Vector3.zero;
}
private Vector3 CalculateObstacleVector()
{
var obstacleVector = Vector3.zero;
RaycastHit hit;
if (Physics.Raycast(myTransform.position, myTransform.forward, out hit, assignedFlock.obstacleDistance, obstacleMask))
{
obstacleVector = FindBestDirectionToAvoidObstacle();
}
else
{
currentObstacleAvoidanceVector = Vector3.zero;
}
return obstacleVector;
}
private Vector3 FindBestDirectionToAvoidObstacle()
{
if (currentObstacleAvoidanceVector != Vector3.zero)
{
RaycastHit hit;
if (!Physics.Raycast(myTransform.position, myTransform.forward, out hit, assignedFlock.obstacleDistance, obstacleMask))
{
return currentObstacleAvoidanceVector;
}
}
float maxDistance = int.MinValue;
var selectedDirection = Vector3.zero;
for (int i = 0; i < directionsToCheckWhenAvoidingObstacles.Length; i )
{
RaycastHit hit;
var currentDirection = myTransform.TransformDirection(directionsToCheckWhenAvoidingObstacles[i].normalized);
if (Physics.Raycast(myTransform.position, currentDirection, out hit, assignedFlock.obstacleDistance, obstacleMask))
{
float currentDistance = (hit.point - myTransform.position).sqrMagnitude;
if (currentDistance > maxDistance)
{
maxDistance = currentDistance;
selectedDirection = currentDirection;
}
}
else
{
selectedDirection = currentDirection;
currentObstacleAvoidanceVector = currentDirection.normalized;
return selectedDirection.normalized;
}
}
return selectedDirection.normalized;
}
private bool IsInFOV(Vector3 position)
{
return Vector3.Angle(myTransform.forward, position - myTransform.position) <= FOVAngle;
}
}
CodePudding user response:
The error was just the name of the script it was FlockScript Instead of Flock by itself. and this is the script that was causing the problem
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Flock : MonoBehaviour
{
[Header("Spawn Setup")]
[SerializeField] private FlockUnit flockUnitPrefab;
[SerializeField] private int flockSize;
[SerializeField] private Vector3 spawnBounds;
[Header("Speed Setup")]
[Range(0, 10)]
[SerializeField] private float _minSpeed;
public float minSpeed { get { return _minSpeed; } }
[Range(0, 10)]
[SerializeField] private float _maxSpeed;
public float maxSpeed { get { return _maxSpeed; } }
[Header("Detection Distances")]
[Range(0, 10)]
[SerializeField] private float _cohesionDistance;
public float cohesionDistance { get { return _cohesionDistance; } }
[Range(0, 10)]
[SerializeField] private float _avoidanceDistance;
public float avoidanceDistance { get { return _avoidanceDistance; } }
[Range(0, 10)]
[SerializeField] private float _aligementDistance;
public float aligementDistance { get { return _aligementDistance; } }
[Range(0, 10)]
[SerializeField] private float _obstacleDistance;
public float obstacleDistance { get { return _obstacleDistance; } }
[Range(0, 100)]
[SerializeField] private float _boundsDistance;
public float boundsDistance { get { return _boundsDistance; } }
[Header("Behaviour Weights")]
[Range(0, 10)]
[SerializeField] private float _cohesionWeight;
public float cohesionWeight { get { return _cohesionWeight; } }
[Range(0, 10)]
[SerializeField] private float _avoidanceWeight;
public float avoidanceWeight { get { return _avoidanceWeight; } }
[Range(0, 10)]
[SerializeField] private float _aligementWeight;
public float aligementWeight { get { return _aligementWeight; } }
[Range(0, 10)]
[SerializeField] private float _boundsWeight;
public float boundsWeight { get { return _boundsWeight; } }
[Range(0, 100)]
[SerializeField] private float _obstacleWeight;
public float obstacleWeight { get { return _obstacleWeight; } }
public FlockUnit[] allUnits { get; set; }
private void Start()
{
GenerateUnits();
}
private void Update()
{
for (int i = 0; i < allUnits.Length; i )
{
allUnits[i].MoveUnit();
}
}
private void GenerateUnits()
{
allUnits = new FlockUnit[flockSize];
for (int i = 0; i < flockSize; i )
{
var randomVector = UnityEngine.Random.insideUnitSphere;
randomVector = new Vector3(randomVector.x * spawnBounds.x, randomVector.y * spawnBounds.y, randomVector.z * spawnBounds.z);
var spawnPosition = transform.position randomVector;
var rotation = Quaternion.Euler(0, UnityEngine.Random.Range(0, 360), 0);
allUnits[i] = Instantiate(flockUnitPrefab, spawnPosition, rotation);
allUnits[i].AssignFlock(this);
allUnits[i].InitializeSpeed(UnityEngine.Random.Range(minSpeed, maxSpeed));
}
}
}