// Update is called once per frame void Update() { Vector3 accel = Vector3.zero; // 集群加速度 accel += cohesion.GetSteering(sensor.targets) * cohesionWeight; // 分隔加速度 accel += separation.GetSteering(sensor.targets) * separationWeight; // 速度匹配加速度 accel += velocityMatch.GetSteering(sensor.targets) * velocityMatchWeight; // 如果没有那些 影响, 就漫游好了 if (accel.magnitude < 0.005f) { accel = wander.GetSteering(); } // 设置刚体速度 和 朝向 steeringBasics.Steer(accel); steeringBasics.LookWhereYoureGoing(); }
// Update is called once per frame void FixedUpdate() { Vector3 center = new Vector3(0, 0, 0); float count = 0; GameObject[] drones = GameObject.FindGameObjectsWithTag("Fish"); foreach (GameObject drone in drones) { center += drone.transform.position; count++; } var theCenter = center / count; Vector3 velocity = new Vector3(0, 0, 0); count = 0; foreach (GameObject drone in drones) { velocity += drone.GetComponent <Rigidbody>().velocity; count++; } var theVelocity = velocity / count; //separationOutput = separation.GetSteering(); //avoidanceOutput = avoidance.GetSteering(); velocityOutput = velocityMatching.GetSteering(leader.GetComponent <Rigidbody>().velocity); //arriveOutput = arrive.GetSteering(leader.transform.position); Move(separationOutput.m_linear + velocityOutput.m_linear + arriveOutput.m_linear + (3f) * avoidanceOutput.m_linear); if (m_droneGO.GetComponent <Rigidbody>().velocity.magnitude > m_maxSpeed) { m_droneGO.GetComponent <Rigidbody>().velocity = m_droneGO.GetComponent <Rigidbody>().velocity.normalized *m_maxSpeed; } float step = m_turnSpeed * Time.deltaTime; transform.rotation = Quaternion.Slerp(transform.rotation, leader.transform.rotation, Time.deltaTime * m_turnSpeed); //m_droneGO.GetComponent<Transform>().rotation = Quaternion.RotateTowards(transform.rotation, leader.transform.rotation , step); }