// Update is called once per frame void Update() { float distance = Vector3.Distance(target.position, transform.position); //计算玩家与野怪的距离 if (distance <= maxDistance) //判断玩家是否在野怪的攻击范围内 { state = fishState.Attack; } if (state == fishState.Attack) { AutoAttack(); //野怪执行攻击 } else if (state == fishState.Death) //如果野怪的状态是死亡,则播放死亡动画 { animation.CrossFade(animall_death); } else//野怪处于其他状态 { animation.CrossFade(animall_now);//播放野怪当前动画 if (animall_now == animall_walk) { cc.SimpleMove(transform.forward * speed); } timer += Time.deltaTime;//计时器 if (timer >= time) { timer = 0; Randomstate();//随机产生行走和静立状态 } } }
void Init() { getInput(); SetScene(); InitShader(); SetDelegates(); int threadsPgroup = 1024 / 4; // Shader parameter, number of threads per group, to get the number of groups nbGroups = Mathf.CeilToInt(numFishes / (float)threadsPgroup); states = new fishState[2][]; states [0] = new fishState[numFishes]; states [1] = new fishState[numFishes]; nbmat = Mathf.CeilToInt(numFishes / (float)instanceLimit); // Number of matrices which need to be created to draw all the instances left = numFishes - ((nbmat - 1) * instanceLimit); // Number of instances in the last matrix, as it could be less than 1023 fishArray = new Matrix4x4[nbmat][]; // Creation of the matrices for (int i = 0; i < nbmat; i++) { if (i != nbmat - 1) { fishArray [i] = new Matrix4x4[instanceLimit]; } else { fishArray [i] = new Matrix4x4[left]; } } }
public fishState(fishState state) { speed = state.speed; position = state.position; forward = state.forward; rotation = state.rotation; }
// Start is called before the first frame update void Start() { state = fishState.Idle; //初始化初始状态 animall_now = animall_idle; //初始化初始动画 aniname_attack_now = aniname_normalattack; //初始化攻击动画 cc = this.GetComponent <CharacterController>(); player = GameObject.FindGameObjectWithTag("Player"); //获取玩家对象 target = player.transform; //获取玩家的位置 animation = GetComponent <Animation>(); render = GetComponent <Renderer>(); }
void SetFish(int i) // Creation of a fish with random parameters { float speed = Random.Range(0.5f, max_speed); Vector3 pos = new Vector3(Random.Range(-borderX, borderX), Random.Range(-borderY, borderY), Random.Range(-borderZ, borderZ)); Vector3 goal = new Vector3(Random.Range(-borderX, borderX), Random.Range(-borderY, borderY), Random.Range(-borderZ, borderZ)); while (pos == goal) { goal = new Vector3(Random.Range(-borderX, borderX), Random.Range(-borderY, borderY), Random.Range(-borderZ, borderZ)); } Vector3 forward = (goal - pos).normalized; // rotate to make it face its randomly set direction Quaternion rotation = Quaternion.Slerp(Quaternion.identity, Quaternion.LookRotation(forward), rotationSpeed * Time.deltaTime); // initialize the read/write buffer with the state of the fish states [0][i] = new fishState(speed, pos, forward, rotation); states [1][i] = new fishState(states [0][i]); }
// Use this for initialization void Start() { fishAgent = GetComponent <NavMeshAgent>(); GameObject.FindGameObjectsWithTag("fish"); fishAgent.updatePosition = true; fishAgent.updateRotation = true; wayPoints = GameObject.FindGameObjectsWithTag("waypoint"); wayPointInd = Random.Range(0, wayPoints.Length); AIState = CatFishAI.fishState.MOVE; RB = GetComponent <Rigidbody>(); StartCoroutine("FSM"); moveSpeed = Random.Range(0.8f, 1.2f); }
public void TakeDamage(int attack) { if (state == fishState.Death) { return; } float value = Random.Range(0f, 1f); if (value < miss_rate)//Miss效果 { //AudioSource.PlayClipAtPoint(miss_sound, transform.position); } else { this.hp -= attack; Slider_c4.instance.SetValue(hp); //StartCoroutine(ShowBodyRed()); if (hp <= 0) { state = fishState.Death; Destroy(this.gameObject, 2); } } }
void Calc(int index) // Update the properties of each fish { fishState other, current = states [read] [index]; Vector3 center = Vector3.zero, forward = Vector3.zero, avoid = Vector3.zero, position = current.position, curfwd = current.forward; Quaternion rotation = current.rotation; float speed = 0.0f, curspeed = current.speed; int numNeighbors = 0; System.Random random = new System.Random(); // Each fish has to look at all the other fish, if one is close enough to create a flock, the flocking behavior is set. for (int j = 0; j < numFishes; j++) { if (index != j) { other = states [read] [j]; Vector3 othpos = other.position, othfwd = other.forward; if (Call(position.x, othpos.x, distNeighbor)) // Check if the x-position of two fish is close enough to create a flock { float dist = Vector3.Distance(othpos, position); // To avoid always calculating the distance, which is quite heavy if (Neighbor(othfwd, curfwd, dist, distNeighbor)) { numNeighbors++; // UPDATE FLOCKING BEHAVIOR center += othpos; // COHESION speed += other.speed; forward += othfwd; // ALIGNMENT if (dist <= 0.30f) // SEPARATION { avoid += (position - othpos).normalized / dist; } } } } } if (numNeighbors == 0) // if the fish has no one to swim with { speed = curspeed; forward = curfwd; } else // if not alone, updating its properties considering its neighbors <-> flocking rules { forward = (direction_velocity * forward / numNeighbors) + (avoid_velocity * avoid) + (center / numNeighbors) + curfwd - position; speed = curspeed + (((speed / numNeighbors) - curspeed) * 0.50f); // linearly converges to the average speed of the flock } if (OutofBounds(position)) // if a fish reaches the limit of the tank, change its direction and speed { Vector3 rand = new Vector3(borderX * Mathf.Cos(360 * (float)random.NextDouble()), borderY * Mathf.Cos(360 * (float)random.NextDouble()), borderZ * Mathf.Cos(360 * (float)random.NextDouble())); forward = forward.normalized + (deltaTime * outOfBoundsSpeed * (rand - position)); speed = (float)random.NextDouble() * max_speed; } forward += Rocks(position, forward, speed, max_speed); // Check if the fish is close to a rock - if so, add an avoidance force position += (forward.normalized * deltaTime * speed); // Translate the fish if (forward != Vector3.zero) { rotation = Quaternion.Slerp(rotation, Quaternion.LookRotation(forward), rotationSpeed * deltaTime); // Rotate the fish towards its forward direction } states[write][index].Set(speed, position, forward.normalized, rotation); // update the write state buffer fishArray [idx] [index % instanceLimit].SetTRS(position, rotation, scale); // update the matrix to draw the fish }