0
\$\begingroup\$

So, I am making a 2d board game with unity. Basically, two characters are moving on the board when clicking a spot on the board. Since it's a static one frame 2d game, I put all of my game objects children of the canvas. All the positions seems to be aligned, in the canvas itself everything is in place, also when I am debugging I notice that both of the positions, the local position and the target position are correct, so when I'm not using lerp and just setting the character position to the target position, it works perfect.

Lerp/Smooth damp are working perfect outside the canvas I think it's something related to the update function, as it's updating false positions, but that's all I can guess... This is the Update function of the Player class:

        private void FixedUpdate()
{
    if (moveTowards == true)
    {
        velocity = Vector3.zero;
        this.transform.position = Vector3.SmoothDamp(this.transform.localPosition, targetSpotPosition, ref velocity, .08f);
    }

    if (this.transform.position == targetSpotPosition)
    {
        moveTowards = false;
    }
}

moveTowards set to true when clicking a spot.

    public void movePlayerTo(Spots moveHere)
{
    moveTowards = true;
    moveHere.transform.position = Camera.main.ScreenToWorldPoint(moveHere.transform.position);
    targetSpotPosition = moveHere.transform.position;
    //targetSpotPosition = Camera.main.ScreenToWorldPoint(targetSpotPosition);
}

moveHere is the spot's position, there's no problem there.

I set my canvas to camera space render mode, and of course scale with screen size. Before putting everything in the canvas, it worked perfectley.

HELP PLEASE. Thanks in advance,

Omer

\$\endgroup\$
8
  • \$\begingroup\$ It sounds like you have a bug in your code. If you'd like help debugging your code, you should edit your question to include a Minimal Complete Verifiable Example of the problem (all code and scene setup instructions we'd need to reproduce a sample of the problem in a new, empty project). It would also help to include screenshots so we can see how the behaviour you're getting now differs from what you expect. \$\endgroup\$
    – DMGregory
    Sep 27, 2020 at 20:56
  • \$\begingroup\$ Sorry, I edited the question and added the code. Regarding screenshots, I'm not sure it will be very helpful since this behavior is more of an update issue and changes every frame, maybe a video could have been beneficial. Anyway, do you see something in the code that may cause that? I am mentioning again, that before I put the game objects children of the canvas, it worked great. Thanks so much. \$\endgroup\$ Sep 27, 2020 at 21:15
  • \$\begingroup\$ Your code makes reference to world space, which is completely inappropriate for objects in a screen space camera canvas. I'd recommend either dropping the canvas and working in world space, or changing to use UI-based positioning with RectTransforms, anchoredPositions, RectTransformUtility.ScreenPointToLocalPointInRectangle, etc. We have some existing Q&A about this. \$\endgroup\$
    – DMGregory
    Sep 27, 2020 at 21:19
  • \$\begingroup\$ Thank you so much. If I'm dropping the canvas, how shall I make my game resolution dynamic to fit different screen sizes?.. \$\endgroup\$ Sep 27, 2020 at 21:24
  • \$\begingroup\$ That sounds like a different question. You can edit your post to ask that instead or post a new question, showing a screenshot of what your game looks like on your primary target resolution, and a mock-up or detailed descriptions of what should change on different resolutions. \$\endgroup\$
    – DMGregory
    Sep 27, 2020 at 21:29

0

You must log in to answer this question.

Browse other questions tagged .