transform.localscale as a one time command rather than over time?

Hi there. I have the following code:

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

public class Player1CharacterController : MonoBehaviour
{

   public Transform P2;

void Update()
    {
        if (transform.position.x <= P2.position.x)
        {
            facingleft = 0;
        }
        if (transform.position.x >= P2.position.x)
        {
            facingleft = 1;
        }
        if (facingleft == 0)
        {
            transform.localScale = Vector3.Scale(transform.localScale, new Vector3(1, 1, 1));
        }
        if (facingleft == 1)
        {
            transform.localScale = Vector3.Scale(transform.localScale, new Vector3(-1, 1, 1));
        }
}
}

When the object crosses onto the other side of P2, the x scale should change to -1. However, when the object cross to the other side, it’s scale keeps flipping from -1 to 1. This makes me think the script is repeatedly multiplying the x-scale by -1, rather than just multiplying it once when the object moves over. I have looked at and tried to understand about 8 different threads now, as well as looking at the unity manual, and I am still stumped. Any help is appreciated.

Thank you,
Victor

Well, think about it. What is -1 * -1? It’s 1. What is -1 * 1? That’s -1. So it’s going to keep swapping because you keep telling it to multiply over and over each frame.

Yes I see that is the issue, however I do not know what to put instead? I have read that you cannot simply input values for localscale as you can for position and rotation, so I am stumped for what to do…

Umm…where did you read this?

If you just want it set to a value. Just set it. That’s technically all you’re doing anyways. Just set localScale = to your new Vector3 value.

I have tried this a few times, but perhaps I am doing it wrong. When I use the line:

            transform.localscale = new Vector3(-1, 1, 1);

I get the compiler error “Type ‘UnityEngine.Transform’ does not contain a definition for ‘localscale’ and no extension method ‘localscale’ of type ‘UnityEngine.Transform’ could be found. Are you missing an assembly reference?”

Well, localscale is not right. Caps matter!

localScale

Oddly, you had it right in your code above.

Wow, that was really all I had to change. Cant believe I spent over two hours on this smh…Thanks for your help

No problem. Glad to help!