Problems with enemy script

Hey guys. I’ve been working on a mini game, its a mini 2D platformer.
I’m trying to make my enemy (kat) have a ‘patrol behavior’… I picked up this script via an Youtube tutorial. The Enemy behaves as it should (it detects 2D colliders then changes direction)
THE PROBLEM:
The enemy grows up immediately as i get into Play Mode (Example in the video).
I’m not a programmer, so i made this far via Youtube tutorials… i’m breaking my head trying to figure out why this happen with no luck.

Please, can somebody help?

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

public class EnemyPatrol : MonoBehaviour
{
    public float maxSpeed = 1f;
    public float speed = 1f;
    private Rigidbody2D rb2d;
   
   
    void Start ()
    {
    rb2d = GetComponent<Rigidbody2D>();
    }
   
   
    void FixedUpdate ()
    {
    rb2d.AddForce(Vector2.right * speed);
    float limitedSpeed = Mathf.Clamp(rb2d.velocity.x, -maxSpeed, maxSpeed);
    rb2d.velocity = new Vector2(limitedSpeed, rb2d.velocity.y);
   
    if (rb2d.velocity.x > -0.01f && rb2d.velocity.x < 0.01f)
    {
    speed = -speed;
    rb2d.velocity = new Vector2(speed, rb2d.velocity.y);
    }
   
    if (speed < 0.1f)
    {
    transform.localScale = new Vector3(1f, 1f, 1f);
    }
   
    if (speed > -0.1f)
    {
    transform.localScale = new Vector3(-1f, 1f, 1f);
    }
   
   
    } 

   
}

transform.localScale = new Vector3(1f, 1f, 1f);
you are overwriting the scale from the cat with 1 (i can’t see it in the video, i think the value is something like 0.1)

store somewhere the start value from the cat and use them for the flip.

Vector3 tmpScale;

void Start () {
tmpScale = transform.localScale;
}

void FixedUpdate () {
//.....
transform.localScale = tmpScale;
//...
transform.localScale = Vector3(-tmpScale.x, tmpScale.y, tmpScale.z);
2 Likes

I think i love you :smile:

changed the code values to my ‘inspector’ values:

if (speed < 0.1f)
{
transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
}

if (speed > -0.1f)
{
transform.localScale = new Vector3(-0.2f, 0.2f, 0.2f);
}

and now it works!

THANKS A LOT!!!

1 Like

I typically use public or serialized private fields for inserting objects or for testing for speed, jump height, damage factors, etc… However, when I find the solution, For future reference :wink:

1 Like

You can do this too, if you don’t want to change the scale of you character:

using UnityEngine;
public class EnemyPatrol : MonoBehaviour
{
    [SerializeField, Tooltip("Max Speed")]
    private float maxSpeed = 1f;
    [SerializeField, Tooltip("Speed")]
    private float speed = 1f;


    private Rigidbody2D rb2d;
    private SpriteRenderer sprite;
 
 
    void Start ()
    {
         rb2d = GetComponent<Rigidbody2D>();
         sprite = GetComponent<SpriteRenderer>();
    }
 
    void FixedUpdate ()
    {
         rb2d.AddForce(Vector2.right * speed);
         float limitedSpeed = Mathf.Clamp(rb2d.velocity.x, -maxSpeed, maxSpeed);
         rb2d.velocity = new Vector2(limitedSpeed, rb2d.velocity.y);
 
         if (rb2d.velocity.x > -0.01f && rb2d.velocity.x < 0.01f)
         {
              speed = -speed;
              rb2d.velocity = new Vector2(speed, rb2d.velocity.y);
         }
 
         if (speed < 0.1f)
         {
              sprite.flipX = true; // The flipX property of SpriteRenderer allows you to change the direction of the sprite through Boolean values. But this only affects the renderer.
         }
 
         if (speed > -0.1f)
         {
              sprite.flipX = false; // The flipX property of SpriteRenderer allows you to change the direction of the sprite through Boolean values. But this only affects the renderer.
         }
 
 
    }
 
}

Or instead of this. Spread the code like this:

if (speed < 0.1f)
{
     Vector3 newLocalScale = new Vector(0.2f, transform.localScale.y, transform.localScale.z); // Create a new Vector3 with different values in X. But in Y and Z place the value of localScale (because these values don't change as X).
     transform.localScale = newLocalScale;
}

if (speed > -0.1f)
{
     Vector3 newLocalScale = new Vector3(-0.2f, transform.localScale.y, transform.localScale.z);
     transform.localScale = newLocalScale;
}

Note: The [SerializeField] allows a private variable to be displayed in the inspector.

2 Likes