示例#1
0
    void FixedUpdate()
    {
        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();
    }
示例#2
0
    public override SteeringOutput getSteering()
    {
        SteeringOutput steering = new SteeringOutput();
        //Direction to target
        Vector3 direction = target - character.position;
        float   distance  = direction.magnitude;

        //Check if we are there
        if (distance < targetRadius)
        {
            VelocityMatch getToZero = new VelocityMatch(this.character, new Vector3(0.0f, 0.0f, 0.0f), 5.0f);
            steering = getToZero.getSteering();
            return(steering);
        }

        //If we are outside the slow radius, go max speed
        float targetSpeed;

        if (distance > slowRadius)
        {
            targetSpeed = maxSpeed;
            //Otherwise calculate scaled speed
        }
        else
        {
            targetSpeed = maxSpeed * distance / slowRadius;
        }

        //Target velocity combines speed & direction
        Vector3 targetVelocity = direction.normalized * targetSpeed;

        //Acceleration tries to match target velocity
        steering.linear  = targetVelocity - character.velocity;
        steering.linear /= timeToTarget;
        //Clamp acceleration
        if (steering.linear.magnitude > maxAcceleration)
        {
            steering.linear = steering.linear.normalized * maxAcceleration;
        }

        //Return steering data
        return(steering);
    }
示例#3
0
    // Update is called once per frame
    void Update()
    {
        /*
         * My Implementation of flocking
         */

        //Get the average group velocity and position
        Vector3 averagePosition = new Vector3();
        Vector3 averageVelocity = new Vector3();

        foreach (GameObject character in characters)
        {
            averagePosition += character.transform.position;
            averageVelocity += character.GetComponent <SteeringCharacter>().velocity;
        }
        averagePosition *= 1.0f / characters.Count;
        averageVelocity *= 1.0f / characters.Count;

        //averagePosition = targetObject.transform.position;

        //Set to arrive at the average position
        arrivalBehavior.target = averagePosition;
        //Set to match the average velocity
        velocityMatchBehavior.target = averageVelocity;
        //Loop through every character
        foreach (GameObject character in characters)
        {
            SteeringCharacter thisChar = character.GetComponent <SteeringCharacter>();

            arrivalBehavior.character       = thisChar;
            separationBehavior.character    = thisChar;
            velocityMatchBehavior.character = thisChar;

            SteeringOutput arrivalSteering       = arrivalBehavior.getSteering();
            SteeringOutput separationSteering    = separationBehavior.getSteering();
            SteeringOutput velocityMatchSteering = velocityMatchBehavior.getSteering();

            thisChar.steering = new SteeringOutput();
            //Combine all steering behaviors
            thisChar.steering.linear = arrivalSteering.linear + separationSteering.linear + velocityMatchSteering.linear;
        }
    }
示例#4
0
 void Update()
 {
     //Perseguimos al enemigo
     // con arrive accels
     steeringAgent.UpdateSteering(velMatch.getSteering());
 }