Home > Mobile >  How to do vector addition in Unity
How to do vector addition in Unity

Time:06-08

This is my first from scratch game project. I'm trying to make a pinball game but I don't want to just "watch a video on how to make a pinball game". I want to run into the problems and learn how to tackle them as they come.

So far, attaching script to a sprite was issue #1 but I've kinda worked that out. Issue #2 was creating variables and having them translate to real object values. After multiple hours of trial and error I eventually just copied someone elses script that had the most basic setup possible, then broke it and rebuilt it to what I have below with the addition of void Update.

My question is mostly to gather a better understanding but also about a new problem of mine. Issue #3 is currently when I click play, it moves the object only once. I thought void update is supposed to call every frame? I would also like to know why when I do transform.position, why can't I do transform.position = (value 1, value 2)? From what I've come up with from experimenting, the only way to alter transform.position is to do = new Vector everytime which I don't fully understand... Another way of wording this part of the question would be: Is there a shorter way of writing a vector transformation or is this the only way the change can be written?

Below is the code. I appreciate any answers even if it's simply directing on the right path to find the information I want.

    public float width, height, xSpeed, ySpeed, xPosition, yPosition;
    public Vector2 position, scale;
    
    void Start() {
        // Initialise the variables
        width = 0.5f;
        height = 0.5f;
        xSpeed = 0;
        ySpeed = -1f;
        xPosition = 0;
        yPosition = 3.5f;

        // set the scaling
        Vector2 scale = new Vector2(width, height);
        transform.localScale = scale;
        
        // set the position
        transform.position = new Vector2(xPosition, yPosition);
    }

    void Update() {
        transform.position = new Vector2(xPosition   xSpeed, 
                                        yPosition   ySpeed);
    }

CodePudding user response:

First I would recommend you changing the title of the question to something that is a bit more on point, so that people have an idea what programming concept your question is about! :) I would recommend something like "How to do vector addition in Unity".

I will go through your questions one-by-one:

Yes, the Update-Function is called every frame! With each call of Update() you set your position to exactly the same value again and again. That is why it is not moving. Neither the xPosition/yPosition nor the xSpeed/ySpeed variables are changing after they have been defined in Start(), so your Update Function will always set your position to (0, 2.5, 0).

You can do Vector addition! But in order to do that, you need to properly write it in your code, by which I mean you need to make a vector out of the values you want to add and only then you can add them to the position vector! So if you wanted to add the xSpeed/ySpeed values ontop of your position, it would look like that:

transform.position  = new Vector2(xSpeed, ySpeed);

I hope that helps!

  • Related