Home > database >  My enemy script is not behaving as it should be in Unity
My enemy script is not behaving as it should be in Unity

Time:01-04

So, I wrote this enemy script in unity, but my enemy's flying, he's disobeying the laws of gravity and I am not too fond. Is there something wrong with the movement method? Please help me! His mass is 100 and gravity scale set to 32. I really don't know why this is happening.

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;
        }
    }

‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌‌

CodePudding user response:

Have you by accident unchecked the "Use Gravity" option at your RigidBody component?

If it doesn't work, try to disable your script in the inspector of your enemy and see if your enemy is still falling during Play time.

If he isn't falling, then your problem isn't from your code but from the RigidBody.

Otherwise, if he is falling, then your problem was indeed in your code. I would suggest to try Rb.velocity = dir * Time.fixedDeltaTime * Speed; instead of Rb.MovePosition(transform.position dir * Speed);.


Also, never ever put code related to physics in the Update() method, especially if you aren't using Time.deltaTime. Put it instead in the FixedUpdate() so your enemy speed does not depend on the computer performances. You can find more informations here.
Lastly, your variables should be camelCase instead of PascalCase.

  • Related