Header Ads

Ghosts move and hide in unity 2D

In the video the ghost will move, then disappear and move to the next location, will hide present every random time

C#:
1:movingghost

[SerializeField] Transform[] point;
    [SerializeField] float moveSpeed = 2f;
   [SerializeField]public int pointIndex;
   [SerializeField] bool time_nn;
   bool facingRight=false;
  int number;

    void Start()
    {
        this.pointIndex = 0;
        this.transform.position = this.point[this.pointIndex].transform.position;
    }

    // Update is called once per frame
    void Update()
    {
       
            this.FlipEnemy();
       
        if(this.pointIndex<= this.point.Length -1)
        {
            this.transform.position = Vector3.MoveTowards(this.transform.position,
this.point[this.pointIndex].transform.position,this.moveSpeed * Time.deltaTime);
            if(this.transform.position == this.point[this.pointIndex].transform.position)
            {
                this.pointIndex +=1;
                if(this.time_nn==true)
                {
                    float nn = Random.Range(0.5f,2f);
                    this.moveSpeed = nn;
                }
            }
        }
        else
        {
            this.pointIndex = 0;
        }
       
    }
     void FlipEnemy()
    {
         if(this.pointIndex == this.point.Length)
        {
             this.number = 0;
        }
        else
        {
            this.number = this.pointIndex;
        }
        if(this.transform.position.x < this.point[this.number].position.x && facingRight)
        {
            flip();
        }
        else if(this.transform.position.x > this.point[this.number].position.x && !facingRight)
        {
            flip();
        }
    }
         void flip()
   {
   
       Vector3 thescale = this.transform.localScale;
       thescale.x *= -1;
       this.transform.localScale = thescale;
       this.facingRight = !this.facingRight;
   }
2:ghost

 [SerializeField] GameObject ghost_obj;
   [SerializeField]float value =5f;
   
    // Update is called once per frame
    void Update()
    {
        if(this.value > 0)
        this.value -= Time.deltaTime;
        else
        {  
           
                if(this.ghost_obj.activeSelf)
            {
                ghost_obj.GetComponent<Animator>().SetBool("disappear",true);
                if (this.ghost_obj.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0)
.IsName("ghost_disappear"))
                {
                 if(this.ghost_obj.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).normalizedTime > 0.5f)
                    {
                        this.ghost_obj.SetActive(false);
                        this.value = Random.Range(3f,10f);
                    }
                }
            }
            else
            {
                ghost_obj.GetComponent<Animator>().SetBool("disappear",false);
                this.ghost_obj.SetActive(true);
                this.value = Random.Range(3f,10f);
            }
               
           
        }

    }

Video

No comments

Powered by Blogger.