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:
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?
I understand that examples of datatypes are int, float ,string etc. Am I wrong in saying that Vector2 and Vector3 in this context are also datatypes? I’m wondering why do you refer to them as objects? I understand that Vector2 and Vector3 represent xyz co ordinates etc but I’m also wondering what exactly do I call “_scaler” when I write the following:
public Vector3 _scaler;
Do I call it a Vector 3 variable? I’m obviously storing xyz co ordinates inside of it…
I’m doing research on the difference between value types and reference types. From watching the following video, examples of value types are supposed to be things like int, floats and bools and examples of reference types are classes and objects: https://www.youtube.com/watch?v=KGFAnwkO0Pk
I’m just wondering why do you refer Vector2 and Vector3’s as objects if they’re supposed to be value types?
“Object” is a loose term, usually used to refer to a thing that has more than one item inside, but objects can even have ZERO things inside them, such as an empty class or interface with no methods.
And technically at some level everything is an object in some sense. Like an int is actually a handy alias for a System.Int32… same exact animal. Hover over an int and see:
All those interfaces are also implemented for integers, telling you what all you can do with them: comparable, equatable, formattable, convertible, etc.
Really the main thing here is:
value types : the equal sign makes a fresh clean unrelated copy of the value to the new variable
reference types : the equal sign makes a new variable that is an arrow saying “I am that same exact thing over there by a different name” (but it does NOT make a copy of the thing)
So in the update function I have the _scaler variable equal to the game object’s current localScale values:
_scaler = transform.localScale
Later on in the code I change the value of the x value that’s stored within the _scaler variable.
But just because I have changed this value within the _scaler variable it does not mean that I have changed the x value of the game object’s localScale.
The reason for this is because the _scaler variable was declared as a Vector2 variable which is a value type. Therefore _scaler only stores a copy of the value of localScale and does not directly refer to its value.
As for terminology, many folks (myself included) are not always precise in their language. Variable, field, object, struct, class, property, are often mis-used slightly (even thought they all imply different things), and the same goes for function, method, as well as for delegate, callback, functor, etc.