Hi. I have a code that is supposed to execute the Flip function every time the enemy gets close to a wall and then starts walking in the other direction.
But when he gets to the wall it change its velocity so it goes on the oposite direcction, but the localScale dosent invert to face the other direcction. So he starts walking backwards.
I know that the funtion FLip() is executing, and I have noticed that what does not work is the transform.localScale, because even if I put it in the start it doesent rotate.
This Flip() code is the one I use with my player and it works fine, but for some reason it doesn’t with the enemy’s prefab.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyPatrol_2 : MonoBehaviour
{
public float speed = 1f;
public float wallAware = 0.5f;
public LayerMask groundLayer;
public float playerAware = 3f;
public float aimingTime = 0.5f;
public float shootingTime = 1.5f;
private Rigidbody2D _rigidbody;
private Animator _animator;
private SmallWeapon _weapon;
// Movement
private Vector2 _movement;
private bool _facingRight;
private bool _isAttacking;
void Awake()
{
_rigidbody = GetComponent<Rigidbody2D>();
_animator = GetComponent<Animator>();
_weapon = GetComponentInChildren<SmallWeapon>();
}
// Start is called before the first frame update
void Start()
{
if (transform.localScale.x < 0f)
{
_facingRight = false;
}
else if (transform.localScale.x > 0f)
{
_facingRight = true;
}
}
// Update is called once per frame
void Update()
{
Vector2 direction = Vector2.right;
if (_facingRight == false)
{
direction = Vector2.left;
}
if (_isAttacking == false)
{
if (Physics2D.Raycast(transform.position, direction, wallAware, groundLayer))
{
Flip();
}
}
}
private void FixedUpdate()
{
float horizontalVelocity = speed;
if (_facingRight == false)
{
horizontalVelocity = horizontalVelocity * -1f;
}
_rigidbody.velocity = new Vector2(horizontalVelocity, _rigidbody.velocity.y);
}
private void LateUpdate()
{
_animator.SetBool("Idle", _rigidbody.velocity == Vector2.zero);
}
private void OnTriggerStay2D(Collider2D collision)
{
if (_isAttacking == false && collision.CompareTag("Player"))
{
StartCoroutine(AimAndShoot());
}
}
private void Flip()
{
_facingRight = !_facingRight;
float localScaleX = transform.localScale.x;
localScaleX = localScaleX * -1f;
transform.localScale = new Vector3(localScaleX, transform.localScale.y, transform.localScale.z);
Debug.Log("FLiped");
}
private IEnumerator AimAndShoot()
{
float speedBackup = speed;
speed = 0f;
_isAttacking = true;
yield return new WaitForSeconds(aimingTime);
_animator.SetTrigger("Shoot");
yield return new WaitForSeconds(shootingTime);
_isAttacking = false;
speed = speedBackup;
}
void CanShoot()
{
if (_weapon != null)
{
_weapon.Shoot();
}
}
}