Example #1
0
    void FixedUpdate()
    {
        if (Time.timeScale == 0)
        {
            return;
        }

        if (Player == null)
        {
            return;
        }

        if (bFreezed)
        {
            rigidbody.velocity        = Vector3.zero;
            rigidbody.angularVelocity = Vector3.zero;
            return;
        }

        if (transform.position.y > 0.8f)
        {
            return;
        }

        var state = GetState();

        if (state == null)
        {
            return;
        }

        if (state.Equals(AnimState.dead))
        {
            rigidbody.velocity        = Vector3.zero;
            rigidbody.angularVelocity = Vector3.zero;
            return;
        }

        if (state.Equals(AnimState.attack) && currentType >= EnemyType.boss_niu)
        {
            return;
        }

        if (state.Equals(AnimState.take1))
        {
            rigidbody.velocity        = Vector3.zero;
            rigidbody.angularVelocity = Vector3.zero;
            return;
        }

        if (state.Equals(AnimState.take2))
        {
            rigidbody.velocity        = Vector3.zero;
            rigidbody.angularVelocity = Vector3.zero;
            return;
        }

        if (state.Equals(AnimState.skill))
        {
            rigidbody.velocity        = Vector3.zero;
            rigidbody.angularVelocity = Vector3.zero;
            return;
        }

        if (state.Equals(AnimState.thinkAttack))
        {
            rigidbody.velocity        = Vector3.zero;
            rigidbody.angularVelocity = Vector3.zero;
            return;
        }

        if (HP < 0)
        {
            rigidbody.velocity        = Vector3.zero;
            rigidbody.angularVelocity = Vector3.zero;
            return;
        }

//		if (state.Equals(AnimState.attack))
//			return;

        float distance = Vector3.Distance(Player.transform.position, transform.position);

        faceDir = (Player.transform.position - transform.position).normalized;
        float rotationAngle = Ultilities.AngleAroundAxis(transform.forward, faceDir, Vector3.up);

        rigidbody.angularVelocity = (Vector3.up * rotationAngle * turningSmoothing);

        if (distance <= ViewRange)        // && Drop == 0)
        {
            paths.Clear();
            randomPos = Vector3.zero;
            if (distance <= AttackRange - 0.3f)
            {
                if (!GetState().Equals(AnimState.attack))
                {
                    rigidbody.velocity = Vector3.zero;

                    ChangeState(AnimState.thinkAttack);
                }
            }
            else if (distance <= ViewRange * 0.6f)
            {
//				nma.speed = speed;
//				nma.SetDestination(Player.transform.position);
                if (!GetState().Equals(AnimState.thinkAttack))
                {
                    Vector3 targetVelocity = faceDir * speed;
                    Vector3 deltavelocity  = targetVelocity - rigidbody.velocity;
                    rigidbody.AddForce(deltavelocity * velocitySnapness, ForceMode.Force);
                    ChangeState(AnimState.run);
                }
            }
            else if (HP > 0)
            {
//				nma.speed = speed * 0.6f;
//				nma.SetDestination(Player.transform.position);
                Vector3 targetVelocity = faceDir * speed * 0.6f;
                Vector3 deltavelocity  = targetVelocity - rigidbody.velocity;
                rigidbody.AddForce(deltavelocity * velocitySnapness, ForceMode.Force);
                ChangeState(AnimState.walk);
            }
        }
        else
        {
//			if (paths.Count == 0)
//				GenerateNewPos();
//
//			if (Vector3.Distance(randomPos, transform.position) < 0.2f || randomPos == Vector3.zero)
//			{
//				randomPos = paths[Random.Range(0, 4)];
//			}
//			faceDir = (randomPos - transform.position).normalized;
//
//			Vector3 targetVelocity = faceDir * speed * 0.2f;
//			Vector3 deltavelocity = targetVelocity - rigidbody.velocity;
//			rigidbody.AddForce (deltavelocity * velocitySnapness, ForceMode.Force);
//			ChangeState(AnimState.walk);
        }
    }
Example #2
0
    void FixedUpdate()
    {
        if (Time.timeScale == 0)
        {
            return;
        }

        if (heroTransform == null)
        {
            return;
        }

        float distance = Vector3.Distance(transform.position, heroTransform.position);

        if (distance > forceFollowDistance)
        {
            randomPetPos = GenerateRandomPetMovePos();
//			rigidbody.velocity = Vector3.zero;
            ChangeState(AnimState.walk);
        }

        if (GetState().Equals(AnimState.idle))
        {
            return;
        }

//		else
        {
            if (Vector3.Distance(randomPetPos, transform.position) < minDistance)
            {
                ChangeState(AnimState.idle);
                GetComponent <Rigidbody>().velocity        = Vector3.zero;
                GetComponent <Rigidbody>().angularVelocity = Vector3.zero;

                return;
            }
        }

        Vector3 dir = randomPetPos - transform.position;

        dir = dir.normalized;
        Vector3 targetVelocity = dir * speed;
        Vector3 deltavelocity  = targetVelocity - GetComponent <Rigidbody>().velocity;

        GetComponent <Rigidbody>().AddForce(deltavelocity * velocitySnapness, ForceMode.Force);

        Vector3 faceDir = dir;

        if (faceDir == Vector3.zero)
        {
            faceDir = transform.forward;
        }

        if (GetComponent <Rigidbody>().velocity == Vector3.zero)
        {
            faceDir = transform.forward;
        }

        float rotationAngle = Ultilities.AngleAroundAxis(transform.forward, faceDir, Vector3.up);

        GetComponent <Rigidbody>().angularVelocity = (Vector3.up * rotationAngle * turningSmoothing);
    }
Example #3
0
    // Update is called once per frame
    void FixedUpdate()
    {
        if (GameData.Instance.Pause)
        {
            joystickLeft.ResetJoystick();
            return;
        }

        if (Time.timeScale < 1f)
        {
            return;
        }


        if (GetState().Equals(AnimState.skill))
        {
            return;
        }

        if (GetState().Equals(AnimState.daojuAttack))
        {
            return;
        }


        if (GetState().Equals(AnimState.dead))
        {
            return;
        }

        Vector3 targetVelocity = dir * (speed + fuSpeed);
        Vector3 deltavelocity  = targetVelocity - GetComponent <Rigidbody>().velocity;

        GetComponent <Rigidbody>().AddForce(deltavelocity * velocitySnapness, ForceMode.Force);

        //		animation["idle"].weight = Mathf.Lerp(1, 0, Mathf.Abs(targetVelocity.magnitude));

        Vector3 faceDir = dir;

        if (faceDir == Vector3.zero)
        {
            faceDir = transform.forward;
        }

        if (dir == Vector3.zero)
        {
            GetComponent <Rigidbody>().angularVelocity = Vector3.zero;
        }

        if (isAutoAim)
        {
            //			if (Time.time > lastAimTime + AutoAimEnemyDuration || aimedController == null)
            {
                //				lastAimTime = Time.time;
                aimedController = EnemyManager.Instance.GetNearestEnemy(transform.position);
            }

            if ((isFired || automatic) && aimedController != null)
            {
                faceDir = aimedController.transform.position - transform.position;
            }
        }

        float rotationAngle = Ultilities.AngleAroundAxis(transform.forward, faceDir, Vector3.up);

        GetComponent <Rigidbody>().angularVelocity = (Vector3.up * rotationAngle * turningSmoothing * Time.deltaTime);

        if (isFired || automatic)
        {
            speed = 8f;
            if (GetState().Equals(AnimState.skill))
            {
                GetComponent <Rigidbody>().angularVelocity = Vector3.zero;
            }
            else if (Mathf.Abs(targetVelocity.magnitude) == 0)
            {
                ChangeState(AnimState.staticAttack);
            }
            else
            {
                ChangeState(AnimState.attack);
            }

            Weapon currentWeapon = WeaponController.Instance.GetWeaponByID(currentWeaponId);

            //			if (automatic)
            //				currentWeapon = GetComponent<FakeWeaponController>().currentWeapon;

            if (currentWeapon == null)
            {
                Debug.LogError("can't find the weapon by id " + currentWeaponId);
                return;
            }

            //近战
            if (currentWeaponId < WeaponType.gun_area)
            {
                if (currentWeaponId == WeaponType.dianju)
                {
                    if (Time.time > lastFireTime + 1f / currentWeapon.Frequency)
                    {
                        lastFireTime = Time.time;

                        Quaternion r      = Quaternion.AngleAxis(chopAngles / 2f, Vector3.up) * transform.rotation;
                        Vector3    rPoint = r * Vector3.forward * currentWeapon.range + transform.position;

                        Debug.DrawLine(transform.position, rPoint, Color.red, 10f);

                        Quaternion l      = Quaternion.AngleAxis(chopAngles / 2f, Vector3.down) * transform.rotation;
                        Vector3    lPoint = l * Vector3.forward * currentWeapon.range + transform.position;
                        Debug.DrawLine(transform.position, lPoint, Color.green, 10f);
                        Ultilities.gm.audioScript.dianjuFX.play();

                        for (int i = EnemyManager.Instance.transform.childCount - 1; i >= 0; i--)
                        {
                            if (EnemyManager.Instance.transform.GetChild(i).GetComponent <EnemyController>() != null && Helper.isINTriangle(EnemyManager.Instance.transform.GetChild(i).position, transform.position, lPoint, rPoint))
                            {
                                StartCoroutine(hitBack(EnemyManager.Instance.transform.GetChild(i), EnemyManager.Instance.transform.GetChild(i).position, EnemyManager.Instance.transform.GetChild(i).position + Vector3.back * (hitDistance + currentWeapon.hitDistance), 0.2f));

                                //							EventService.Instance.GetEvent<EnemyStateChangeEvent>().Publish(AnimState.take, enemy.EnemyId);
                                EnemyManager.Instance.transform.GetChild(i).GetComponent <EnemyController>().onDamage(currentWeapon.atk, EnemyManager.Instance.transform.GetChild(i).position);
                                if (!automatic)
                                {
                                    WeaponController.Instance.DecreaseWeaponCapacity();
                                }

                                Ultilities.gm.audioScript.dianjuHitFX.play();
                            }
                        }
                    }
                    //
                }
            }
        }
        else
        {
            speed = 9f;
            if (GetComponent <Rigidbody>().velocity == Vector3.zero)
            {
                if (!GetState().Equals(AnimState.dead))
                {
                    ChangeState(AnimState.idle);
                }
            }
            else
            {
                ChangeState(AnimState.walk);
            }
        }
    }