Esempio n. 1
0
            public void Update(GameObject cruiser, GameObject target)
            {
                if (cruiser == null)
                {
                    return;
                }

                if (timer > 0)
                {
                    timer -= 1 * Time.deltaTime;
                }
                else
                {
                    timer = Random.Range(minReflex, maxReflex);

                    float angle = 0;

                    if (target)
                    {
                        angle = Math2DHelpers.GetAngle(target, cruiser);
                    }
                    else
                    {
                        angle = Random.Range(0, 359) * Mathf.Deg2Rad;
                    }

                    _speed += Random.Range(minSpeed, maxSpeed) * new Vector3(Mathf.Cos(angle), Mathf.Sin(angle));
                }

                cruiser.transform.position += _speed * Time.deltaTime;

                _speed.x *= .99f;
                _speed.y *= .99f;
            }
Esempio n. 2
0
            public void Update(PlayerControls playerControls)
            {
                if (Input.GetMouseButton(0) && PlayersGroup.selectedPlayer == playerControls.gameObject)
                {
                    var mousePosition = CameraHelpers.GetMousePosition();

                    if (_previous == Vector3.zero)
                    {
                        _previous = mousePosition;
                    }

                    velocity += (mousePosition - _previous) * speed * playerControls.speed * Time.deltaTime;

                    _previous = mousePosition;
                }
                else if (Input.GetMouseButtonDown(0) && PlayersGroup.selectedPlayer != playerControls)
                {
                    var mousePosition = CameraHelpers.GetMousePosition();

                    if (Math2DHelpers.PointInsideRect(mousePosition.x, mousePosition.y, playerControls.gameObject.transform.position.x, playerControls.gameObject.transform.position.y, playerControls.additionalSettings.size.x, playerControls.additionalSettings.size.y))
                    {
                        PlayersGroup.selectedPlayer = playerControls.gameObject;
                    }
                }
                else
                {
                    _previous = Vector3.zero;
                }

                float angle = Mathf.Atan2(velocity.y, velocity.x) * Mathf.Rad2Deg;

                if (rotation)
                {
                    var targetRotation = Quaternion.AngleAxis(angle, Vector3.forward);

                    playerControls.gameObject.transform.rotation = Quaternion.RotateTowards(playerControls.gameObject.transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
                }

                if (horizontal == false)
                {
                    velocity.x = 0;
                }
                if (vertical == false)
                {
                    velocity.y = 0;
                }

                playerControls.gameObject.transform.position += velocity * Time.deltaTime;

                if (boundaries && playerControls.additionalSettings.bounds != Vector2.zero)
                {
                    _Bounds(playerControls);
                }

                if (friction != 0)
                {
                    velocity *= 1 / (1 + (Time.deltaTime * friction));
                }
            }
Esempio n. 3
0
            public void Update(GameObject magnet, GameObject target)
            {
                if (magnet == null)
                {
                    return;
                }
                if (target == null)
                {
                    return;
                }

                var angle    = Math2DHelpers.GetAngle(target, magnet);
                int distance = (int)(Mathf.Abs(target.transform.position.x - magnet.transform.position.x) + Mathf.Abs(target.transform.position.y - magnet.transform.position.y));

                if (distance > 0)
                {
                    _speed.x += Mathf.Cos(angle) * (int)(range / distance) * speed * Time.deltaTime;
                    _speed.y += Mathf.Sin(angle) * (int)(range / distance) * speed * Time.deltaTime;
                }

                if (_speed.x > maxSpeed)
                {
                    _speed.x = maxSpeed;
                }
                if (_speed.x < -maxSpeed)
                {
                    _speed.x = -maxSpeed;
                }
                if (_speed.y > maxSpeed)
                {
                    _speed.y = maxSpeed;
                }
                if (_speed.y < -maxSpeed)
                {
                    _speed.y = -maxSpeed;
                }

                magnet.transform.position += globalPolarity * polarity * _speed * Time.deltaTime;

                if (friction != 0)
                {
                    _speed *= 1 / (1 + (Time.deltaTime * friction));
                }
            }
Esempio n. 4
0
        public static void Aim(GameObject bullet, GameObject target, float speed, Vector3 position = default)
        {
            if (position != default)
            {
                bullet.transform.position = position;
            }

            bullet.SetActive(true);

            var bulletBase = bullet.GetComponent <BulletBase>();

            if (bulletBase == null)
            {
                bulletBase = bullet.AddComponent(typeof(BulletBase)) as BulletBase;
            }

            var angle = Math2DHelpers.GetAngle(target, bullet);

            bulletBase.transform.rotation = Quaternion.Euler(0, 0, angle * Mathf.Rad2Deg);
            bulletBase.velocity           = new Vector3(Mathf.Cos(angle) * speed, Mathf.Sin(angle) * speed);
        }