- Home /
making models move correctly
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[i] = (GameObject)Instantiate(FishPrefab, pos, Quaternion.identity);
fishy[i].GetComponent<FishMovement>().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));
}
}
Answer by Ady_M · Jan 08, 2020 at 11:51 AM
If you cannot fix the models permanently in a 3D modelling tool like Blender, Maya, etc. then one workaround is to create an empty gameobject (per fish), place the model inside the empty gameobject and rotate the model in the Scene view so the head of the fish is aligned with the empty gameobject's Z axis.
Attach your scripts to the empty gameobject instead of the model.
If you decide to experiment with rigidbodies later the rigidbody should be on the empty gameobject.
that was explained to me before and I was hoping to use a different strategy before I went with that option. I wouldn't say its a wrong one but I fell as though its just being lazy. as if I chose the silly answer over a better, stronger one that wasn't directly in front of me, you know?
If you're talking about solving the problem with an empty gameobject as a parent then yes, I completely understand what you mean; it's really not the "correct way". If you want to do it correctly you should fix the models.
Your answer
Follow this Question
Related Questions
W key to move doesnt work,Movement Script doesnt do anything when i press w 0 Answers
Player Sliding without any Input 2 Answers
2D Movement with axis? 1 Answer
Stop movement while attacking 1 Answer
New to Unity & Scripting, How Do I Create a Movement System Thats About Building Up Speed? 0 Answers