T
T
Taisu Black2020-05-16 18:30:50
Unity
Taisu Black, 2020-05-16 18:30:50

How to fix an error in object transform?

There is such an enemy code

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

public class Enemy : MonoBehaviour
{
    public int health = 10;
    Animator anim;
    public Transform target;
    public float speed = 3f;
    public float attackRange = 1f;





    public void TakeDamage(int damage)
    {
        health -= damage;

        if(health <= 0)
        {
            anim.SetInteger("Death", 2);
            anim.SetInteger("DeathEnemy", 2);
            Die();
        }
    }
    void Die()
    {
        Destroy(gameObject, 1f);
    }

    void Start()
    {
        
        anim = GetComponent<Animator>();
    }

    void Update()
    {
        transform.LookAt(target);

        if(Vector3.Distance (transform.position, target.position) > attackRange)
        {   
            transform.position += transform.forward * speed * Time.deltaTime;
            if (Input.GetAxis("Horizontal") < 0)
            {
                transform.localRotation = Quaternion.Euler(0, 180, 0);
            }
            if (Input.GetAxis("Horizontal") > 0)
            {
                transform.localRotation = Quaternion.Euler(0, 0, 0);
            }
        }
        
       

    }

}

The problem is that the enemy turns into a thin object when he follows me, what's the problem? and when it goes it flashes and flips in different directions just like that5ec007219a694104834708.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question