-1
\$\begingroup\$

I would like to move my player from one position to another position using key detection. I would like to know how I can make the player movement smooth.

Code:

if (Input.GetKeyDown(KeyCode.A))
    {
        xSpeed = -1f;
        ySpeed = 0f;
        X = X + xSpeed;
        Y = Y + ySpeed;
        Vector3 NewPos = new Vector3(X, Y, 0);
        gameObject.transform.position = NewPos;
    }
    else if (Input.GetKeyDown(KeyCode.D))
    {
        xSpeed = 1f;
        ySpeed = 0f;
        X = X + xSpeed;
        Y = Y + ySpeed;
        Vector3 NewPos = new Vector3(X, Y, 0);
        gameObject.transform.position = NewPos;
    }
    else if (Input.GetKeyDown(KeyCode.W))
    {
        xSpeed = 0f;
        ySpeed = 1f;
        X = X + xSpeed;
        Y = Y + ySpeed;
        Vector3 NewPos = new Vector3(X, Y, 0);
        gameObject.transform.position = NewPos;
    }

    else if (Input.GetKeyDown(KeyCode.S))
    {
        xSpeed = 0f;
        ySpeed = -1f;
        X = X + xSpeed;
        Y = Y + ySpeed;
        Vector3 NewPos = new Vector3(X, Y, 0);
        gameObject.transform.position = NewPos;
    }

enter image description here

\$\endgroup\$
1
  • 6
    \$\begingroup\$ -1 for lack of research. Unitys own website has a plethora of tutorials; several of which answer this question. We expect some degree of research effort, on your behalf, before posting a question to GameDev.SE. \$\endgroup\$
    – Gnemlock
    Dec 12, 2016 at 23:27

1 Answer 1

-1
\$\begingroup\$

The most basic approach is, as stated in the link in the comments, to multiply your movement with Time.deltaTime in the Update-method of your gameobject.

Time.deltaTime is the time passed between the last frame (update) and the current.

In your case:

if (Input.GetKeyDown(KeyCode.A))
{
    xSpeed = -1f;
    ySpeed = 0f;
    //Consider to increase your speed or add a new factor to multiply with, otherwise your movement will slow down significantly 
    X = X + xSpeed * Time.deltaTime;
    Y = Y + ySpeed * Time.deltaTime;
    Vector3 NewPos = new Vector3(X, Y, 0);
    gameObject.transform.position = NewPos;
}
\$\endgroup\$
5
  • \$\begingroup\$ It is not working it doesn't move 1x or 1y like if it is at 0,0 and i click A it goes to 0.4830474 instead of 1,0 \$\endgroup\$
    – Pedro
    Dec 13, 2016 at 18:14
  • \$\begingroup\$ Sorry, I forgot to say, that you have to increase your speed or mulitipy an additional factor. Of course the multiplying only the deltaTime will slow it down heavily \$\endgroup\$
    – Artery
    Dec 13, 2016 at 19:16
  • \$\begingroup\$ The comment you point to is actually notification that the question is marked for closure. If you agree with it, you should not copy an answer across. You should not be answering, at all. \$\endgroup\$
    – Gnemlock
    Dec 13, 2016 at 20:30
  • \$\begingroup\$ Thanks for this advise. But what is the reason behind not answering at all? I didn't know such a rule. I didn't just copy the answer from the link, I tried to include the problem. In my opinion its a bit rude, to prevent other to answer this question even though it's a duplicate or whatever, especially down voting my answer, which is technically correct! \$\endgroup\$
    – Artery
    Dec 13, 2016 at 20:33
  • \$\begingroup\$ In my opinion it's not fair and a bit toxic to down-vote my answer, because I never heared about this rule and saw questions marked as duplicate were answered. Just tell me, that it is not common or not wanted to do that. Its not about losing some of the rep, but its about the behavior, which up sets me! \$\endgroup\$
    – Artery
    Dec 13, 2016 at 20:38

Not the answer you're looking for? Browse other questions tagged .