I think my transform.localScale doesn't work

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

Please don’t cross-post. You already asked this question here . Also, as requested in that thread, please edit your posts to use code-tags because a wall of plain text is much harder to follow.

Ok, Thanks. I didn’t know how it worked, so thanks for the link. And i´m trying to delete the other one because i didn’t know how to ad tags after i created it, so i created a new one.

1 Like

I can delete the other thread if you wish. Not a problem.

Yeah, thank a lot for the help.

OnTriggerStay2D is not called when something first enters a trigger, that’d be OnTriggerEnter2D. OnTriggerStay2D is continuously called if you’re still in contact with the trigger. Note that you’re starting a new coroutine each time which is 50 times/sec (default FixedUpdate rate) so that’s not good.

Also, if you continuously (per-frame) check for a raycast and flip, you’ll keep flipping while the raycast touches something. That doesn’t sound like what you want.

Finally, I would consider flipping the visuals (SpriteRenderer?) if the “character” colliders are symmetric along the vertical axis. This is because when you modify the Transform, all attached colliders need to be recreated from scratch. Doing this for a handful of things isn’t so bad but it scales poorly and not required if only visuals are flipping.

thanks, i created it from scratch, tweaking some things from the code and now its working. So thanks for the help

1 Like