I have 2 Movement scripts - What is the difference between the 2?

I have 2 movement scripts with PlayerMove being the way I have been doing it so far and OtherPlayerMove being something that I saw somebody else do. They both “seem” like the end result is the same, but I’m curious to so his choices.

  • I see one immediate difference; his Vector 3 variable allows him to combine the X and Y Transfrom portion into line vs me having the code for each axis. I like and will be adopting.

  • What is the point of change = Vector.zero? I know it is shorthand for (0,0,0) but okay, if I’m not moving the player doesnt that mean I am at (0,0,0) as anyways? Then what is the point of the if statement? If change not = Vector3.zero then move but If I/m not moving that then it would be (0,0,0,) the I should be able to move but I can?

  • He adds a rigidbody component then turns off the gravity and moves the rigidbody by its transform and not force? why do that? I thought adding a rigidbody meant you wanted the object to interact with the physics engine, but if that is not the case the why just not do it the way I did it in the PlayerMove script…I guessing maybe the rigidbody will allow him to do something else later?

public class PlayerMove : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        Movement();
    }

    void Movement()
    {
        float speed = 5;
        float Xinput = Input.GetAxisRaw("Horizontal");
        float Yinput = Input.GetAxisRaw("Vertical");

        transform.position += Vector3.right * Xinput * speed * Time.deltaTime;
        transform.position += Vector3.up * Yinput * speed * Time.deltaTime;
    }
}
public class OtherPlayerMove : MonoBehaviour
{
    float speed = 5;
    Rigidbody2D rb;
    Vector3 change;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }
   
    // Update is called once per frame
    void Update()
    {
        change = Vector3.zero;
        change.x = Input.GetAxisRaw("Horizontal");
        change.y = Input.GetAxisRaw("Vertical");
        if (change != Vector3.zero)
        {
            Movement();
        }
    }

    void Movement()
    {
        rb.MovePosition(transform.position + change.normalized * speed * Time.deltaTime);
    }
}

hmmmmm… what is the issue with my thread?

I believe the compiler will complain if you try to modify the individual x/y/z components without having assigned some value to the variable as a whole. You could just assign this once during class creation, but I’m guessing at some point this was a local variable rather than a class variable (I don’t see any reason for it to be a class variable currently).

Also hypothetically if something had changed the Z component since the last update, this would reset it back to zero.

For Unity Vectors, == actually means approximately equal, not exactly equal.

If you were planning to move by a very tiny amount, skipping the movement entirely make make the game appear smoother and/or improve efficiency.

However, the main reason to do it in this specific case is that the move function is normalizing the vector–that is, forcing it to a magnitude of 1, no matter how big or small it was before. So in this case, a movement of 0.000001 would be turned into a movement of 1.0.

The reason for normalizing the input vector is probably to make it so that diagonal movement is the same speed as orthogonal movement. A player input of (1, 1) is “bigger” than an input of (1, 0), so if you don’t correct it somehow, the player’s maximum diagonal speed will be larger by a factor of sqrt(2).

But this also has the effect that inputs smaller than 1 are increased to 1. This would cause problems if you wanted to have analog input where the player can move slowly by tilting the joystick only a little bit. (But since you’re using GetAxisRaw, that is presumably not the intent.)

I would guess that he wants objects that collide with the player to get pushed around by physics (and maybe also push the player around) even if the player itself isn’t using physics for normal movement. But I haven’t done much physics-based stuff.

1 Like

Thank you so much for responding.

I have been playing around with it and still cannot find any purpose for change = vector 3.zero:

  • I purposely changed the Z component before play and it did not reset its position back to zero which was surprising
  • I got rid of it thinking it might break something or flip out because there is no initial value assigned to it and it did not… nothing happened. Movement still worked.
  • The only “change” I got to happen is setting the if statement to == instead of != and that just stopped the gameobject from moving by inputs but not from non input movement.

The only thing I can think of is this is some kind of precaution/game element that will prevent movement if some other thing that is not being made clear happens

I am suspicious about the validity of your tests.