示例#1
0
    // Update is called once per frame
    void FixedUpdate()
    {
        var targetPosition = _headsetTransform.localToWorldMatrix.MultiplyPoint(Offset);

        targetPosition.y = Mathf.Clamp(
            targetPosition.y,
            _headsetTransform.position.y - .2f,
            _headsetTransform.position.y + .2f
            );

        var steeringForce = Seek.Calculate(transform.position, targetPosition, MaxSpeed);

        // Calculate velocity
        _velocity = Vector3.ClampMagnitude(steeringForce / Mass * Time.fixedDeltaTime, MaxSpeed);

        // Add velocity to position
        transform.position += _velocity;

        // Add continuous rotation
        transform.eulerAngles = new Vector3(
            transform.eulerAngles.x,
            transform.eulerAngles.y + RotationSpeed,
            transform.eulerAngles.z
            );
    }
示例#2
0
    public static Vector3 Calculate(Steering instance, Vector3 target)
    {
        Vector3 toEvader = target - instance.transform.position;

        float RelativeHeading = Vector3.Dot(instance.transform.position.normalized, target.normalized);

        if ((Vector3.Dot(toEvader, instance.transform.position.normalized) > 0) && (RelativeHeading < -0.95))
        {
            return(Seek.Calculate(instance, target));
        }

        float LookAheadTime = toEvader.magnitude / instance.Owner.maxSpeed + target.magnitude;

        return(Seek.Calculate(instance, target + target.normalized * LookAheadTime));
    }
示例#3
0
    public static Vector3 Calculate(Steering instance, List <Agent> entities)
    {
        /*int NeighborCount = 0;
         * Vector3 CenterOfMass = Vector3.zero;
         *
         * foreach (MovingEntity o in entities)
         * {
         *  if (o != instance)
         *  {
         *      CenterOfMass += o.transform.position;
         *
         *      NeighborCount++;
         *  }
         * }
         *
         * if (NeighborCount > 0)
         * {
         *  CenterOfMass /= NeighborCount;
         *
         *  CenterOfMass = Seek.Calculate(instance, instance.GetUsefulTarget(CenterOfMass));
         * }*/

        float   neighbourDist = 50f;
        Vector3 sum           = new Vector3();
        int     count         = 0;

        for (int i = 0; i < entities.Count; i++)
        {
            float dist = Vector3.Distance(instance.Owner.Position, entities[i].Position);
            if (dist > 0)
            {
                sum += entities[i].Position;
                count++;
            }
        }

        if (count > 0)
        {
            sum /= (float)count;
            return(Seek.Calculate(instance, sum));
        }
        else
        {
            return(Vector3.zero);
        }

        //return CenterOfMass;
    }
示例#4
0
    public static Vector3 Calculate(Rigidbody rigidbody, Transform target)
    {
        var toEvader = target.position - rigidbody.position;

        var lookAheadTime = toEvader.magnitude;

        // Add turn around time
        const float coefficient = 0.5f;

        lookAheadTime += (
            Vector3.Dot(Vector3.Normalize(rigidbody.velocity), Vector3.Normalize(toEvader)) - 1)
                         * -coefficient;

        // Seek to predicted position
        return(Seek.Calculate(rigidbody.transform.position, target.position + rigidbody.velocity * lookAheadTime));
    }
示例#5
0
    public static Vector3 Calculate(Steering instance, Deceleration deceleration, float decelerationRate)
    {
        if (instance.path == null)
        {
            return(Vector3.zero);
        }

        instance.path.Update(instance.Owner.Position);

        if (!instance.path.IsFinished)
        {
            return(Seek.Calculate(instance, instance.path.Current));
        }
        else
        {
            return(Arrive.Calculate(instance, instance.path.Current, deceleration, decelerationRate));
        }
    }
示例#6
0
    // Update is called once per frame
    void Update()
    {
        if (leader == true)
        {
            force = s.Calculate();
        }
        else
        {
            force = o.Calculate();
        }
        acceleration        = force / mass;
        velocity           += acceleration * Time.deltaTime;
        velocity            = Vector3.ClampMagnitude(velocity, maxSpeed);
        velocity           *= 0.99f;
        transform.position += velocity * Time.deltaTime;

        if (velocity.magnitude > float.Epsilon)
        {
            transform.forward = velocity;
        }
    }