Help Explain Code Differences in my Update Paddle Position

I am just hoping to get some clarification on the differences between Rick’s code and my own.

I paused the video while setting up the Update Paddle Position section so I could have a go at it on my own before he explained it further, and I ended up with something different, that works exactly the same way.

Rick’s Code:

void Update () 
    {
        float mousePosInUnits = Input.mousePosition.x / Screen.width * screenWidthInUnits
        Vector2 paddlePos = new Vector2(mousePosInUnits, transform.position.y);
        transform.position = paddlePos;
    }

My Code:

	void Update ()
    {
        float xPos = Input.mousePosition.x / Screen.width * screenWidthInUnits;
        transform.position = new Vector2(xPos, 0.3f);
    }

What are the differences functionally?
Why didn’t I need to create a Vector2 type variable?
What is the purpose of ‘new’ and what does it do?
Is one of these better than the other, why?
Any obvious advantages/disadvantages of either?

I am new to programming and barely understand what I’m looking at, so any information helps.

1 Like

Hi Darquizard,
In a nutshell, they’re pretty much the same.

Looking at your code, your xPos is the same as Rick’s mousePosInUnits.
The next to lines of Ricks code are roughly the same as your single line, but has just been expanded for readability. This is most likely to aid with the teaching process and isn’t strictly necessary.

Note that:

Vector2 paddlePos = new Vector2(mousePosInUnits, transform.position.y);
transform.position = paddlePos;

is exactly the same as:

transform.position = new Vector2(mousePosInUnits, transform.position.y);

If you’re new to coding (or are writing something particularly complex) it can be useful to take the long-form approach to make the code a little more self-documenting and easy to follow.
This is especially helpful for when you inevitably go back to some old code and need to work out what it’s doing.

In your code, you have a magic number (0.3f), which you should try to avoid. This sort of thing should always be stored in a variable, so that it’s easier to change at a later date. In Rick’s case, he’s used the current y-position of the paddle so this essentially locks that axis and prevents you moving up and down.

Both are valid options but I’d say Rick’s is slightly more elegant, as it allows you to move the paddle in the editor and the code will always work. In your case, you’d need to modify a variable (or that magic number) if you want to change the y-position of the paddle.

I hope that clears things up for you.

1 Like

Hi Harrison,

Just to add to Gary’s most excellent reply, specifically on the new keyword you queried.

The new keyword is used to instantiate a new instance of an object.

To give you an analogy…

You could think of a class as a cookie cutter, when you use new it’s like pushing the cookie cutter into the dough, you create an object (the cookie), and it’s in the shape of the cookie cutter you used.

Each object (an instance the class) will have the same structure as the class, the same variables (fields), properties and methods, but the data that each object holds may be different. Using the cookie analogy, the shape of the cookie would be the same, but perhaps the cookie has a frowny face, or a smilie face, blue eyes, or white eyes, two gummy buttons, or one gummy button…Here’s a non cookie example;

public class Person
{
    // member variables (fields)
    private string _name;
    private int _age;

    // constructor
    public Person()
    {
        // ...
    }

    // properties
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public int Age
    {
        get { return _age; }
        set { _age = value; }
    }

    // methods
    public void Walk()
    {
        // ...
    }

    public void Run()
    {
        // ...
    }
 
    public void Jump()
    {
        // ...
    }
    
}

The above may introduce some things you are not familiar with yet, specifically the constructor and the properties, but the above is a template, our cookie cutter. We’ve called it Person, each time I use it I can create a new object of type Person, the data that I store in each Person can be different.

Person securityGuard = new Person();
Person bankManager = new Person();

In the above, I have created two objects, two instances of the Person type. Using new made a call to the class’s default constructor, now lets make them more unique;

securityGuard.Name = "Harrison";
securityGuard.Age = 21;

In the above I use the properties to set both the name and the age, in doing so it uses the set block of code to store the value I have passed it into the appropriate member variable, so in the case of “name”, the Name property sets the _name member variable, and in the case of “age”, the Age property sets the _age member variable.

Finally, we have defined some behaviour in our class, methods Walk, Run and Jump, so we could then use these behaviours by making a call to them via our object;

bankManager.Jump();
securityGuard.Run();

Note, I have deliberately left our any code from the “middle” of these methods, what they do would be up to you :slight_smile:

I’ve linked some further reading below for you, don’t worry too much if it doesn’t all make sense right away, but hopefully some of it will be familiar from concepts you’ve already seen and used in the course.

Hope this helps :slight_smile:


See also;

1 Like

Thank you both very much. ‘new’ makes a lot more sense now, and I understand how both of these code blocks were essentially doing the exact same thing, Rick’s more explicitly. Consequently he covered the ‘magic number’ situation and the yPos of the paddle, so I ended up removing that .03f I had in there.

1 Like

You’re very welcome Harrison and, of course, welcome to the community :slight_smile:

Thanks for that addition Rob, I completely missed that part of the question!

1 Like

My pleasure :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms