- Home /
Help with trying to change the value of x value of the localScale of a game object with code
Hi there,
I'm trying to create a simple platformer game where I can just move the player game object left or right on the x-axis using the arrow keys on my keyboard. Although I've succeeded in making this functionality work properly through the usage of Rigidbody2D and code, I don't understand a couple of lines of code that I've written and why it works. You can see below in my code I've created a two part if else statement that makes the player sprite image flip depending on which direction the player is moving. Here's the code I've written:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private Rigidbody2D _rb;
public float _moveInput;
private int _speed = 7;
public Vector3 _scaler;
public bool isFacingRight = true;
// Start is called before the first frame update
void Start()
{
_rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
_scaler = transform.localScale;
_moveInput = Input.GetAxisRaw("Horizontal");
_rb.velocity = new Vector2(_moveInput * _speed, _rb.velocity.y);
if(isFacingRight == false && _moveInput > 0)
{
isFacingRight = true;
_scaler.x = _scaler.x * -1;
transform.localScale = _scaler;
Debug.Log("transform.localScale equals to " + _scaler.x);
} else if (isFacingRight == true && _moveInput <0)
{
isFacingRight = false;
_scaler.x = _scaler.x * -1;
transform.localScale = _scaler;
Debug.Log("transform.localScale equals to " + _scaler.x);
}
}//update
}
What I don't understand is if I remove the line "transform.localScale = _scaler;" from the two parts of the if statement the function wont work. I don't understand why it doesn't just work if I just write the if statements with that line removed like so:
if(isFacingRight == false && _moveInput > 0)
{
isFacingRight = true;
_scaler.x = _scaler.x * -1;
Debug.Log("transform.localScale equals to " + _scaler.x);
} else if (isFacingRight == true && _moveInput <0)
{
isFacingRight = false;
_scaler.x = _scaler.x * -1;
Debug.Log("transform.localScale equals to " + _scaler.x);
}
Can anyone explain why I need to include "transform.localScale = _scaler;" in order for the x value of the localScale to change correctly in my if statement?
All your code is doing is changing transform.localScale x value. Positive 1 faces right and -1 faces left.( depending on the way you drew your sprite. So you could say: transform.localScale = new Vector3(1, 1,1);
Or
transform.localScale = new Vector3(-1, 1, 1);
Answer by Wolfzoon · Feb 01, 2021 at 02:51 PM
@segasega89 Your confusion seems to stem from not knowing the difference between reference and value types. localScale
, like transform.position
, rotation
, and others, is a Vector3
struct, and structs are value types. Consider the following:
int a = 1;
int _scaler = a;
_scaler = 2;
print(a);
You would expect this to print 1, not 2. b
was set to the same value as a
, and that value was immediately overwritten with the value 2, while a
simply stayed the same. Now consider:
class MyInt {
public int x;
}
MyInt a = new MyInt { Number = 1};
MyInt _scaler = a;
_scaler.x= 2;
print(a.x);
This prints 2. This time you create a new object of type MyInt
on the managed heap. The "real" value of a is a pointer to this object, 0x123A56BE or whatever. b
is set to the same value, so it now references the same object. Afterwards, you change a value inside that object, and reference it through a
.
struct MyInt {
public int x;
}
MyInt a = new MyInt { Number = 1};
MyInt _scaler = a;
_scaler.x= 2;
print(a.x);
This prints 1. The assignment does the same as in the int
example, it copies the whole struct to the "new object" _scaler. Changing a value inside this other object does not change the original object it was copied from.
int
, float
, string
(ish), ulong
, byte
, char
, and every struct are value types, and behave like the first example. Classes behave like the second. I would point you to sets of guidelines, but those are broken as much as they are followed. Immutability of structs for one is not enforced, as you can set _scaler.x. Just know that Vector3
is a struct, and it behaves like int
. Other structs in Unity you might encounter are Color
, Quaternion
, and Scene
.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Flip over an object (smooth transition) 3 Answers
Cannot make bullet shoot right 2 Answers