my gameobject rulls between position to local scale.

Hi all,
This is my first text on unity forums, and sorry for my english.

I want to change my enemys localscale.x to -1f when enemys transform.position.x going to less then before,
else localscale.x must bu 1f.

i tried many ways but i could not change it. (i am using MoveTowards for move my enemy)

thanks all

Can you post some code ?
What do you want to accomplish with a negative scale? :slight_smile:

1 Like

this is my enemies ( 2d game) when he go left he must turn left and when it he go right it musr turn right.
only this simple i need to do :). today i evening i can send my code also. thank you for answer.

I don’t think you can have a negative localscale. you’re talking about move towards so you most likely mean localtransform.x not localscale

Ya, I don’t think localscale can be negative, either :slight_smile: (I mean, I think you can set it to a negative value, but <= 0 would be invisible?)
Maybe he’s asking about rotation … to “flip” the character?

I’ve reread his question about 10 times. I think this is what he is after.

public GameObject enemy;  //set this gameobject in the inspector
public float lessThenBefor = 5f;  // change this number to desired distance

void Update()
{
distance =Vector3.Distance(transform.position,enemy.position);

    if(distance < lessThenBefor)
    {
        enemy.localscale.x = 0;
    }else
    {
        enemy.localscale.x = 1;
    }
}

so he wants to have a scale of 0? :slight_smile: You could be right. Honestly, I have no idea. @Biallt , could you use a few more words to describe what you’re hoping to achieve? :slight_smile:

Just post in turkish or your own language. There is probably someone around.

on 2d the my image is looking right side when localscale.x is 1f, so when i make it -1f not 0 this this will be opposite of this image and will be look left.

exactly i need flip to caracter. as i told i have low level english :).

lol, it was my bad memory. It is to flip it! I just remembered badly… Are you moving the character with some input keys ?

Like, if you were using , for instance: ‘A’ = left and ‘D’ = right, then when you click ‘A’ set the localScale to -1 and ‘D’ set it to 1.
If it’s something like tap/click to move towards, then Check if that position is less than or greater than your current position.x ?
I’m not sure how much/which parts you need help with (now) :slight_smile:

Caracter moving by itself on a path from checkpoints[target] to checkpoints [target+1]

There is 19 checkpoint
So it must be -1 if target <7
Else localscale will be 1

Something ı can use like this also
Or transform.position.x going less

For sure… whenever you set the new move towards, simply compare the destination’s position.x to your transform.position.x and if it’s less, then set the scale -1 , else 1
You could do it with targets < 7 also but in case you add more targets or anything else, that may not work the same way, so the position one is better/will always work, I think.
Wherever you set it up, try something like this…

// setting new checkpoint
if(checkpoints[target+1].position.x < transform.position.x) transform.localScale = new Vector3(-1f,1,1);
else transform.localScale = new Vector3(1,1,1);
// This is if the checkpoint array is a Transform []
// otherwise, checkpoints[target+1].transform.position (etc)
1 Like

now i am trying :).

ask a quesion looks like harder than this perfect answers :).

it’s work! thank you!!!

first time i used unity forums and i love it :slight_smile:

but i get 1 alert on console while targets become max volue, then it moving to exit.
also for this i tried this code and it is worked perfectly.

    void Yonlendirme()
    {
        if (hedef < araNoktalar.Length)
        {
            if (araNoktalar[hedef].position.x < dusman.position.x)
                transform.localScale = new Vector3(-1, 1, 1);
            else transform.localScale = new Vector3(1, 1, 1);
        }
        else
        {
            if (cikisNoktasi.position.x < dusman.position.x)
                transform.localScale = new Vector3(-1, 1, 1);
            else transform.localScale = new Vector3(1, 1, 1);
        }
    }

sorry again my codes are turkish so you may not understand

this is code whit english

    void FlipEnemy()
    {
        if (target < checkpoints.Length)
        {
            if (checkpoints[target].position.x < enemy.position.x)
                transform.localScale = new Vector3(-1, 1, 1);
            else transform.localScale = new Vector3(1, 1, 1);
        }
        else
        {
            if (exitpoint.position.x < enemy.position.x)
                transform.localScale = new Vector3(-1, 1, 1);
            else transform.localScale = new Vector3(1, 1, 1);
        }
    }

Hey there, I’m happy for you and glad things are working.
If there is an error in the console, can you explain one more time when/how this is happening? :slight_smile:

Of target is become more than checkpoints.lengt
System cant find checkpoints[target].position.x so i add little rule for this

Ah, okay, of course :slight_smile: