I’m trying to apply a code I followed online to a model with different axis (original fish faced in the z axis, new fish faces in the -x axis). using the code below the original fish works fine. when I use it on the new fish however, the fish just move sideways awkwardly. I changed the speed vector to the x axis and the fish move properly, but after that they don’t seem to communicate to one another. They just swim around the boundary in a big circle. I’m positive it involves the axis somehow, but I don’t know enough about coding to fix it. any suggestions?
fish movement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FishMovement : MonoBehaviour
{
public FishManager myManager;
float speed;
bool turning = false;
// Start is called before the first frame update
void Start()
{
speed = Random.Range(myManager.minSpeed, myManager.maxSpeed);
}
// Update is called once per frame
void Update()
{
//determine the bounding box of the manager cube
Bounds b = new Bounds(myManager.transform.position, myManager.swimLimits * 2);
//if fish is outside the bounds of the cube or about to hit something
// then start turning around
RaycastHit hit;
Vector3 direction = Vector3.zero;
if (!b.Contains(transform.position))
{
turning = true;
direction = myManager.transform.position - transform.position;
}
else if (Physics.Raycast(transform.position, this.transform.forward * 50, out hit))
{
turning = true;
direction = Vector3.Reflect(this.transform.forward, hit.normal);
}
else
{
turning = false;
}
if (turning)
{
// turn towards the center of the manager cube
transform.rotation = Quaternion.Slerp(transform.rotation,
Quaternion.LookRotation(direction),
myManager.rotationSpeed * Time.deltaTime);
}
else
{
if (Random.Range(0, 100) < 10)
{
ApplyRules();
}
}
// fish movement
transform.Translate(0, 0, speed * Time.deltaTime);
}
void ApplyRules()
{
GameObject[] gos;
gos = myManager.fishy;
Vector3 vcentre = Vector3.zero;
Vector3 vavoid = Vector3.zero;
float gSpeed = 0.01f;
float nDistance;
int groupSize = 0;
foreach(GameObject go in gos)
{
if(go != this.gameObject)
{
nDistance = Vector3.Distance(go.transform.position, this.transform.position);
if (nDistance <= myManager.neightbourDistance)
{
vcentre += go.transform.position;
groupSize++;
if(nDistance < 1.0f)
{
vavoid = vavoid + (this.transform.position - go.transform.position);
}
FishMovement anotherFlock = go.GetComponent<FishMovement>();
gSpeed = gSpeed + anotherFlock.speed;
}
}
}
if (groupSize > 0)
{
vcentre = vcentre / groupSize + (myManager.goalPos - this.transform.position);
speed = gSpeed / groupSize;
Vector3 direction = (vcentre + vavoid) - transform.position;
if (direction != Vector3.zero)
transform.rotation = Quaternion.Slerp(transform.rotation,
Quaternion.LookRotation(direction),
myManager.rotationSpeed * Time.deltaTime);
}
}
}
Fish Manager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FishManager : MonoBehaviour
{
public GameObject FishPrefab;
public float speed = 5.0f;
private int numberOfFish = 50;
public GameObject[] fishy;
public Vector3 swimLimits = new Vector3(5, 5, 5);
public Vector3 goalPos;
[Header("Fish Settings")]
[Range(0.0f, 5.0f)]
public float minSpeed;
[Range(0.0f, 5.0f)]
public float maxSpeed;
[Range(1.0f, 10.0f)]
public float neightbourDistance;
[Range(0.0f, 5.0f)]
public float rotationSpeed;
// Start is called before the first frame update
void Start()
{
fishy = new GameObject[numberOfFish];
for(int i = 0; i < numberOfFish; i++)
{
Vector3 pos = this.transform.position + new Vector3(Random.Range(-swimLimits.x, swimLimits.x),
Random.Range(-swimLimits.y, swimLimits.y),
Random.Range(-swimLimits.z, swimLimits.z));
fishy *= (GameObject)Instantiate(FishPrefab, pos, Quaternion.identity);*
fishy*.GetComponent().myManager = this;*
}
goalPos = this.transform.position;
}
// Update is called once per frame
void Update()
{
if(Random.Range(0,100) < 10)
goalPos = this.transform.position + new Vector3(Random.Range(-swimLimits.x, swimLimits.x),
Random.Range(-swimLimits.y, swimLimits.y),
Random.Range(-swimLimits.z, swimLimits.z));
}
}