unity3d 我的敌人脚本在Unity中的表现不正常

6za6bjd0  于 2023-01-05  发布在  其他
关注(0)|答案(1)|浏览(171)

所以,我写了这个敌人的剧本,但是我的敌人在飞,他违反了重力定律,我不太喜欢。移动方法有问题吗?请帮帮我!他的质量是100,重力刻度设置为32。我真的不知道为什么会发生这种情况。

using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class CobraScript : MonoBehaviour {

        private GameObject Player;
        private Rigidbody2D Rb;
        public float Speed;
        private float AttackDelay;

        public GameObject Walking;
        public GameObject AttackPoint; 

        private AudioSource Src;
        public AudioClip Clip;

        private bool Aggro;
        public LayerMask PlayerLayer;
        public float AggroRange;
        public float AttackRange;

        public float ReturnSpeed = 0.3f;

        private bool ShouldWalking = true;

        void Start()
        {
            Player = GameObject.Find("DrawCharacter");
            Src = Camera.main.GetComponent<AudioSource>();
            Rb = GetComponent<Rigidbody2D>();
        }

        void Update()
        {
            AttackDelay -= Time.deltaTime;

            Collider2D[] PlayerFind = Physics2D.OverlapCircleAll(transform.position, AggroRange, PlayerLayer);
            if (PlayerFind.Length > 0)
            {
                Aggro = true;
            }
        
            if (Aggro)
            {
                if (ShouldWalking)
                {
                    gameObject.GetComponent<Animator>().Play("XenusRun");
                    Walking.SetActive(true);
                }else
                {
                    Walking.SetActive(false);
                }

                Collider2D[] PlayerAttack = Physics2D.OverlapCircleAll(AttackPoint.transform.position, AttackRange, PlayerLayer);
                if (PlayerAttack.Length > 0 && AttackDelay <= 0)
                {
                    StartCoroutine(Attack());

                    AttackDelay = 3;
                }

                if (Player.transform.position.x < this.transform.position.x)
                {
                    transform.localScale = new Vector3(-1, 1, 1);
                }else
                {
                    transform.localScale = new Vector3(1, 1, 1);
                }

                Vector3 dir = Player.transform.position - transform.position;
    ---->        Rb.MovePosition(transform.position + dir * Speed);
            }
        }

        void OnDrawGizmosSelected()
        {
            Gizmos.color = Color.red;
            Gizmos.DrawWireSphere(AttackPoint.transform.position, AggroRange);
            Gizmos.DrawWireSphere(AttackPoint.transform.position, AttackRange);
        }

        private IEnumerator Attack()
        {
            ShouldWalking = false;
            GetComponent<Animator>().Play("XenusAttack");
            Speed = 0;

            yield return new WaitForSeconds(0.4f);
        
            Src.PlayOneShot(Clip);
            AttackPoint.SetActive(true);
        
            yield return new WaitForSeconds(0.2f);
            
            ShouldWalking = true;
            AttackPoint.SetActive(false);

            yield return new WaitForSeconds(ReturnSpeed);

            Speed = 0.059f;
        }
    }

"""""""""""""

nzkunb0c

nzkunb0c1#

您是否无意中取消选中了RigidBody组件上的“Use Gravity”选项?
如果它不起作用,尝试在敌人的检查器中禁用您的脚本,并查看您的敌人是否仍在游戏时间内倒下。
如果他没有掉下来,那么问题就不是出在代码上,而是出在刚体上。
否则,如果他正在下降,那么您的问题确实出在代码中。我建议尝试Rb.velocity = dir * Time.fixedDeltaTime * Speed;而不是Rb.MovePosition(transform.position + dir * Speed);
另外,永远不要把与物理相关的代码放在Update()方法中,特别是如果你没有使用Time.deltaTime。把它放在FixedUpdate()中,这样你的敌人的速度就不会依赖于计算机的性能。你可以在here中找到更多的信息。
最后,您的变量应该是camelCase而不是PascalCase。

相关问题