項目來源
這個項目來自於我的同學,對於海洋環境和魚類游動進行了簡單模擬
項目分析
本系統以魚類游泳和感知仿真為主,結合海洋環境的建模與仿真,基於魚類行為學,通過Untiy3D、3DS MAX兩種三維軟件來進行創作,模擬魚類的游泳動作、感知行為和多樣性行為,實現模擬海洋環境和魚類行為,用虛擬的手段進行海底環境的仿真。可以通過WASD進行鏡頭的切換和跟隨
不足之處
對於魚類的游動只進行了簡單模擬,在游動時較為死板,不符合真實魚類的游動情況
改進方案
利用動畫機控制魚的骨骼和隨機游動的代碼來控制魚的游動
代碼清單
flock_whale.cs
/控制魚的隨機游動/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class flock_whale : MonoBehaviour
{
bool turnningaction = false;
private Animator animation;
public float speed = 1f;
float rotationSpeed = 0.08f;
Vector3 averageHeading;
Vector3 averagePosition;
float neighbourDistance = 80.0f;
bool turning = false;
void Start()
{
animation = GetComponent<Animator>();
speed = Random.Range(0.5f, 1);
if (Vector3.Distance(transform.position, globalFlock_whale.goalPos) >= globalFlock.tankSize)
turnningaction = true;
//animation.Play("downleft");
}
void Update()
{
if (Vector3.Distance(transform.position, globalFlock_whale.goalPos) >= globalFlock.tankSize*1.7f)
{
turning = true;
}
else
turning = false;
if (turning)
{
Vector3 direction = globalFlock_whale.goalPos - transform.position;
transform.rotation = Quaternion.Slerp(transform.rotation,
Quaternion.LookRotation(direction),
rotationSpeed * Time.deltaTime);
if (turnningaction)
{
animation.CrossFade("downleft", 0.2f);
//print("1");
turnningaction = false;
}
//print("1");
speed = Random.Range(0.5f, 1);
}
else
{
turnningaction = true;
if (Random.Range(0, 5) < 3)
ApplyRules();
}
//print(Time.deltaTime);
transform.Translate(0, 0, Time.deltaTime*speed*1.2f);
}
void ApplyRules()
{
GameObject[] gos;
gos = globalFlock.allFish;
Vector3 vcentre = globalFlock_whale.goalPos;
Vector3 vavoid = globalFlock_whale.goalPos;
float gSpeed = 0.1f;
Vector3 goalPos = globalFlock.goalPos;
float dist;
int groupSize = 0;
foreach (GameObject go in gos)
{
if (go != this.gameObject)
{
dist = Vector3.Distance(go.transform.position, this.transform.position);
if (dist <= neighbourDistance)
{
vcentre += go.transform.position;
groupSize++;
if (dist < 1.0f)
{
vavoid = vavoid + (this.transform.position - go.transform.position);
}
flock anotherFlock = go.GetComponent<flock>();
gSpeed = gSpeed + anotherFlock.speed;
}
}
}
if (groupSize > 0)
{
vcentre = vcentre / groupSize + (goalPos - this.transform.position);
speed = gSpeed / groupSize;
//print(speed);
Vector3 direction = (vcentre + vavoid) - transform.position;
if (direction != globalFlock_whale.goalPos)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
if (turnningaction)
{
animation.CrossFade("downright", 0.2f);
turnningaction = false;
}
// print("1");
}
}
}
}
心得體會
在改進的過程中,查找了許多資料,對於虛擬現實技術有了更加深入的了解,這一方面有着巨大的發展前景。而且這一次的改動並沒有完全解決這個項目的不足之處,只是略微增加了魚游動時的生動性,與現實中的實際情況還有很大的差距,我將繼續學習新的知識,完善這個缺點。