Ejemplo n.º 1
0
        public static SteeringOutput KinematicFace(Body character, Vector2 target)
        {
            SteeringOutput output = new SteeringOutput();

            //output.IsInstantOrientation = true;
            //output.IsOriented = false;

            // work out the direction to target
            Vector2 direction = target - character.position;

            // Check for a zero direction, and make no change if so
            if (direction.sqrMagnitude < float.Epsilon * float.Epsilon)
            {
                return(new SteeringOutput());
            }

            output.AngularInDegree = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;

            return(output);
        }
Ejemplo n.º 2
0
        //private void Start()
        //{
        //OrientationInDegree = 0;
        //_rotationInDegree = 0;
        //}

        public void Actualize(SteeringOutput steeringOutput, float maxSpeed, float deltaTime)
        {
            if (steeringOutput.StopVelocity)
            {
                _rigidbody.velocity = Vector2.zero;
            }
            else
            {
                if (steeringOutput.IsInstantVelocity)
                {
                    _rigidbody.velocity = steeringOutput.Linear;
                }
                else
                {
                    Vector2 currentVelocity = _rigidbody.velocity;

                    currentVelocity += steeringOutput.Linear * deltaTime;

                    if (currentVelocity.sqrMagnitude > maxSpeed * maxSpeed)
                    {
                        currentVelocity.Normalize();
                        currentVelocity *= maxSpeed;
                    }

                    _rigidbody.velocity = currentVelocity;
                }

                FaceMovementDirection(_rigidbody.velocity);
            }


            if (steeringOutput.StopRotation)
            {
                _rigidbody.angularVelocity = 0;
            }
            else
            {
                _rigidbody.angularVelocity += steeringOutput.AngularInDegree;
            }
        }
Ejemplo n.º 3
0
        public static SteeringOutput KinematicArrive(Body character,
                                                     Vector2 target,
                                                     float targetRadius,
                                                     float speed,
                                                     float slowRadius)
        {
            SteeringOutput output = new SteeringOutput();

            // First work out the direction
            output.Linear  = target;
            output.Linear -= character.position;

            float squareDistance = output.Linear.SqrMagnitude();

            // If there is no direction, do nothing
            if (squareDistance < targetRadius * targetRadius)
            {
                output.Linear = Vector2.zero;
            }
            else
            {
                output.Linear.Normalize();
                output.Linear *= speed;

                // if we are outside the slowRadius, then go maxSpeed (and no changement)
                // Otherwise calculate a scaled speed
                if (squareDistance < slowRadius * slowRadius)
                {
                    output.Linear *= Mathf.Sqrt(squareDistance) / slowRadius;
                }

                // If that is too fast, then clip the speed
                output.Linear = Vector2.ClampMagnitude(output.Linear, speed);
            }

            return(output);
        }
Ejemplo n.º 4
0
 public void Add(SteeringOutput steeringOutput)
 {
     Linear          += steeringOutput.Linear;
     AngularInDegree += steeringOutput.AngularInDegree;
 }