鱼群的简单游动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

这是用来控制鱼群的脚本

public class ZoomCtrl : MonoBehaviour {
//鱼的数组`
public fishctrl[] fishes;

public int SpeedCount = 0;
void Start () {
    foreach (fishctrl fish in fishes)
    {
        fish.ResetPosition(Random.Range(0.0f,360.0f));
    }
}

void Update () {
    int index = Random.Range(0, fishes.Length);
    if (fishes[index].state == fishctrl.FishStae.Idle)
    {
        fishes[index].ResetPosition(Random.Range(0.0f, 360.0f));
        fishes[index].BeginSwim();
        return;
    }
}

}

这是用来控制鱼游动的脚本

using UnityEngine;
using System.Collections;

public class FishControl : MonoBehaviour
{
//鱼的状态
public enum FishState
{
Idle,
Swim,
};

public float warnRange = 0;
public float forceLimit = 0;

/*---------------------------------------------*/
public FishState state;

public float _direction;
public float _speed;
public Vector3 _force;

Animator animator;
/*---------------------------------------------*/
void Start()
{
    state = FishState.Idle;

    _direction = 0.5f;
    _speed = 1.0f;

    animator = GetComponent<Animator>();
    animator.applyRootMotion = false;
    animator.SetFloat("Direction", _direction);

}

/*---------------------------------------------*/
void Update()
{
    ProcessFish();


}


/*---------------------------------------------*/
void ProcessFish()
{
   Animator animator = GetComponent<Animator>();

    switch (state)
    {
        case FishState.Idle:
            break;
        case FishState.Swim:           
            Vector2 force = Vector2.zero;
            Vector2 pos = Camera.main.WorldToViewportPoint(transform.position);

            if (pos.x > 1.3 || pos.x < -0.3 || pos.y > 1.3 || pos.y < -0.3)
            {
                StopSwim();
            }

       
   
            else
            {
                force = Vector3.Lerp(_force, Vector3.zero, Time.deltaTime);
                _force = force;
            }

            float angle = 0;
            if (force != Vector2.zero)
            {
                Vector3 fn = new Vector3(force.x, 0, force.y);
                angle = Vector3.Angle(transform.right, fn);
                if (Vector3.Dot(transform.forward, fn) < 0)
                    angle = -angle;
            }

            float acceleration = force.magnitude;

            float direction = 1.0f - (angle * acceleration / forceLimit + 180.0f) / 360.0f;
            direction = Mathf.Clamp(direction, 0, 1);
            _direction = Mathf.Lerp(_direction, direction, Time.deltaTime * 5.0f);
            animator.SetFloat("Direction", _direction);

            if (Mathf.Abs(_direction - 0.5f) > 0.4f)
                GameObject.Find("SummerPool").GetComponent<SummerPool>().SpeedCount += 1;

            float speed = Mathf.Abs(direction - 0.5f) * 1.5f + Mathf.Abs(direction - 0.5f);
            _speed = Mathf.Lerp(_speed, speed, Time.deltaTime * 5.0f);
            animator.SetLayerWeight(1, (1.0f - _speed) * 0.5f);
           animator.SetFloat("Speed", _speed);
            animator.speed = 1.0f + _speed * 2.0f;

            break;
        
    }
}

//这是在鱼群控制脚本中调用的方法
//赋予每个鱼位置以及偏转角度
/---------------------------------------------/
public void ResetPosition(float angle)
{
Vector3 viewPos = new Vector3((Mathf.Cos(angle * Mathf.Deg2Rad) * 0.8f + 0.5f),
(Mathf.Sin(angle * Mathf.Deg2Rad) * 0.8f + 0.5f),
245.0f);
Vector3 dstPos = Camera.main.ViewportToWorldPoint(viewPos);

    transform.position = dstPos;
    transform.LookAt(new Vector3(0, -25, 0), transform.up);
    transform.Rotate(transform.up, Random.Range(-60.0f, -120.0f), Space.World);
}

public void BeginSwim()
{
    state = FishStae.Swin;        
}

public void StopSwim()
{
    state = FishStae.Idle;            
}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值