Problem with Boundaries

Hi,

i have done exact all what the tutor did. But my player don’t move anymore and jump in the middle of the screen. (picture 1)

The console is still working.

Had someone a idea?

Thanks

Hi Hamster,

Did you clamp the position of the player? If so, check the clamp values. Maybe they do not match the coordinates of the game screen.

Hi Nina,

thank you for the fast feedback. :slight_smile:

if have done all what the tutor (Gary) did.

Sorry my english is not so good.
Any Ideas ?

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? If the spaceship does not move, and since the movement depends on delta, log the value of delta into your console. Maybe it is 0. Also log other values into your console to see if they are the expected values.

Hope this helps :slight_smile:


See also;

sorry for the format.
The following is my Player.cs code(Without the usings)
public class Player : MonoBehaviour
{
[SerializeField] float moveSpeed = 5f;

Vector2 rawInput;
Vector2 minBounds;
Vector2 maxBounds;

void Stat()
{
    InitBounds();
}

void Update()
{
    Move();
}

void InitBounds()
{
    Camera mainCamera = Camera.main;
    minBounds = mainCamera.ViewportToWorldPoint(new Vector2(0,0));
    maxBounds = mainCamera.ViewportToWorldPoint(new Vector2(1,1));
}

void Move()
{
    Vector2 delta = rawInput * moveSpeed * Time.deltaTime;
    Vector2 newPos = new Vector2();
    newPos.x = Mathf.Clamp(transform.position.x + delta.x, minBounds.x, maxBounds.x);
    newPos.y = Mathf.Clamp(transform.position.y + delta.y, minBounds.y, maxBounds.y);
    Debug.Log(delta);
    Debug.Log(newPos);
    transform.position = newPos;
}

void OnMove(InputValue value)
{
    rawInput = value.Get<Vector2>();
    Debug.Log(rawInput);
}

}

The log values are over 0.

Which value is which one in your console? Could you log the values along with a self-explanatory message? That would help a lot. If none of the rawInput values is 0 but delta is 0, the problem is very likely moveSpeed.

the first is delta the scond is newPos the onthers are the raw Input logs

i have no idea why it will not work i have done this section many times exact like the tutorial but it will not work!

If i make the last line transform.position = delta; the ship fly
but out of the box

It is really frustrated. i sit since 3 hours on this problem and it dosen’t work.
I have done all exact like the tutorial (more than one time watched and done).
My Next Steps/Ideas:

  1. Hope that sombody can help me
  2. i will build the player.cs that i can move → i must life with no border → maybe as feature
  3. i try the older version of this Laser defender and hope that it will work → last way but really pity
  4. i must maybe add collider for the border → that is cheating and take more time but i have no other ideas more
  5. If Points 1 to 4 dosen’t work i can’t finish this course :frowning:

The output in the console is helpful.

We know that the user input and rawInput are obviously working because we are getting values that are not 0. And you figured out that the car does move if you assign delta to transform.position. This is important information because it narrows the issue down significantly. Good job so far. :slight_smile:


delta and newPos are apparently not working because their values remain 0. Since newPos depends on delta, we check delta first.

This is where we calculate the value for delta:
Vector2 delta = rawInput * moveSpeed * Time.deltaTime;

We also know: If we multiply by 0, the result is 0.

I would check the value of the single factors like this:

Debug.Log("MOVE VALUES");
Debug.Log("rawInput: " + rawInput);
Debug.Log("moveSpeed: " + moveSpeed);
Debug.Log("Time.deltaTime: " + Time.deltaTime);

Vector2 delta = rawInput * moveSpeed * Time.deltaTime;

If moveSpeed is 0 even though the assigned value in the Inspector is not 0, I would suggest to remove the Player component from the Player game object. Then readd it. In rare cases, the components in Unity are buggy.

And if nothing helps, replace moveSpeed with a hard-coded value like this:
Vector2 delta = rawInput * 10f * Time.deltaTime;

If the car moves when you press the keys, the problem is moveSpeed. In that case, remove the [SerializeField] attribute in front of the variable and execute Vector2 delta = rawInput * moveSpeed * Time.deltaTime;. If the issue persists, create a new float variable and use that one instead of moveSpeed. Do not rename moveSpeed because you want Unity to use your new variable, not the broken(?) one.

Also try to close Unity and your script editor. Then go to your project folder and delete the Library folder. Start Unity again. This might take a few seconds as Unity recreates the Library folder. Open your scene and test it.


And if nothing helps, could you please upload your project to GitHub without the Temp and Library folder and share a link to the public repository here? I’d like to take a look into this.

Here is a tutorial video by Brackeys:

https://www.youtube.com/watch?v=qpXxcvS-g3g

Alternatively, your zipped project folder without the Library and the Temp folder will be fine too.

Hi Nina,

thank you very much but your ideas will not work for me. i have done everything, sorry for the bad news.

I have put the Code in a zip and upladed to git(i hope you you open it):
https://github.com/HamsterGer/Laserdefender.git

Sorry that i make you so much trouble in front of the holiday.

In case we don’t write to each other before Christmas. I wish you happy holidays and my problem can still wait (I have 20 days until it is closed, so have a nice time with your family (work is not all)). :slight_smile:

Greeting hamster

Thank you for the zip. I’ve just downloaded it and imported it into Unity 2021.1.26f1. I’m describing the steps how I am debugging your project so you’ll be able to do it yourself if similar problems occurs again in your project. Debugging is all about gathering and interpreting information.

I usually start with a question to figure out what does work. In this case, my question is: “Does the player move if we do not clamp the position?”

The answer is “yes, it does” because this code is working:

        float newSpeed = 10f;

        Vector2 delta = rawInput * newSpeed * Time.deltaTime;  //10f for moveSpeed dosen't work
        transform.position += (Vector3)delta;

        return; // to terminate the method immediately

My next assumption: No movement means that the newPos values must be constant. I briefly verify this assumption.

Debug.Log("newPos.x: " + newPos.x);
Debug.Log("newPos.y: " + newPos.y);

image

Only 0s. My assumption was correct.

Next assumption: The bounds might be wrong. I moved the ship manually in the scene window to see what the expected clamp values are.

Debug.Log("minBounds: " + minBounds);
Debug.Log("maxBounds: " + maxBounds);

image

And this is indeed the problem. The min and max values are 0 each, so the Mathf.Clamp method returns 0 only.

And the problem is … I should have checked the spelling of your methods a bit more carefully. :grimacing:

Can you spot the issue as well?

If not, here is the solution: The Stat method must be spelt Start.


I wish you Happy Holidays. :slight_smile:

1 Like

Hi Nina,

thank you for the solution.

I’ve checked everything but that. What a shame (I’m very ashamed that I stole your time).
I will check something like that first before I turn to you. :slight_smile:

bad hamster :slight_smile: didn’t write the start method correctly, now there are no presents for him. :slight_smile:

Sorry and thank you again for your Feedback(now it is clear).
Have nice Holidays. :slight_smile:

No worries. These are very typical mistakes. Even the best programmers make all the time. It’s not uncommon to spend hours looking for a bug just to discover some random semicolon or spelling error. Nobody expects that you spot all mistakes yourself all the time.

If you simply share what you figured out before you got stuck (for example, code and console output and/or a video, screenshots, and so on), that’s usually very helpful for other people who would like to assist you. :slight_smile:

Ok for the next time i make it better for the people who would like to help me. :slight_smile:

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

Privacy & Terms