Stuttering movement in Unity 2D












2















So basically I am trying to make player movement system like one in RPG Maker with 8 directions. Somehow I succeded, but only partially. When I am trying to suddenly change direction for ex. from up to left, character stutter and do not want to move without releasing all keys first.



Gravity scale is disabled or more like set to 0, body type is dynamic.



Here is a code:



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveChar : MonoBehaviour {

Rigidbody2D rigid;

public float Speed;

// Use this for initialization
void Start () {

rigid = GetComponent<Rigidbody2D>();

}

// Update is called once per frame
void Update () {

// float horiz = Input.GetAxis("Horizontal");

// float vert = Input.GetAxis("Vertical");

if(Input.GetKeyDown(KeyCode.W)) //________________________________________MOVING UP

{
rigid.velocity = new Vector2(rigid.velocity.x, 1 * Speed);
}
else if(Input.GetKeyUp(KeyCode.W))
{
rigid.velocity = new Vector2(0, 0);
}

if (Input.GetKeyDown(KeyCode.S)) //_______________________________________MOVING DOWN
{
rigid.velocity = new Vector2(rigid.velocity.x, -1 * Speed);
}
else if (Input.GetKeyUp(KeyCode.S))
{
rigid.velocity = new Vector2(0, 0);
}


if (Input.GetKeyDown(KeyCode.A)) //_______________________________________MOVING LEFT
{
rigid.velocity = new Vector2(-1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.A))
{
rigid.velocity = new Vector2(0, 0);
}

if (Input.GetKeyDown(KeyCode.D)) //_______________________________________MOVING RIGHT
{
rigid.velocity = new Vector2(1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.D))
{
rigid.velocity = new Vector2(0, 0);
}
}









share|improve this question




















  • 1





    You need to stop setting your velocity to zero every time a key is lifted up. You only want to modify one component of the vector in that case, and also you need to consider the state of the key for going in the opposite direction. Also, is there a reason you aren't using Input.GetAxis? It does a lot of the stuff you're trying to do here.

    – Ruzihm
    Nov 14 '18 at 18:22











  • As Ruzihm says the first thing I would try is instead of setting (0,0) only change x or y the respective one you change in the other case of if.

    – derHugo
    Nov 14 '18 at 18:25













  • There is no reason for not using Axis, I simply don't know hot to use it. I tried using it before, but I could not implement it as negative value (even if I tried putting '-' before it, it did not work for some reasons). When i will not set value to 0, 0 after lifting key up, character will fly at set direction, I do not know any other way to stop it.

    – lubeszz
    Nov 14 '18 at 18:29











  • @lubeszz I edited my answer to include how to use Input.GetKeyUp/GetKeyDown if you must.

    – Ruzihm
    Nov 14 '18 at 18:42


















2















So basically I am trying to make player movement system like one in RPG Maker with 8 directions. Somehow I succeded, but only partially. When I am trying to suddenly change direction for ex. from up to left, character stutter and do not want to move without releasing all keys first.



Gravity scale is disabled or more like set to 0, body type is dynamic.



Here is a code:



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveChar : MonoBehaviour {

Rigidbody2D rigid;

public float Speed;

// Use this for initialization
void Start () {

rigid = GetComponent<Rigidbody2D>();

}

// Update is called once per frame
void Update () {

// float horiz = Input.GetAxis("Horizontal");

// float vert = Input.GetAxis("Vertical");

if(Input.GetKeyDown(KeyCode.W)) //________________________________________MOVING UP

{
rigid.velocity = new Vector2(rigid.velocity.x, 1 * Speed);
}
else if(Input.GetKeyUp(KeyCode.W))
{
rigid.velocity = new Vector2(0, 0);
}

if (Input.GetKeyDown(KeyCode.S)) //_______________________________________MOVING DOWN
{
rigid.velocity = new Vector2(rigid.velocity.x, -1 * Speed);
}
else if (Input.GetKeyUp(KeyCode.S))
{
rigid.velocity = new Vector2(0, 0);
}


if (Input.GetKeyDown(KeyCode.A)) //_______________________________________MOVING LEFT
{
rigid.velocity = new Vector2(-1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.A))
{
rigid.velocity = new Vector2(0, 0);
}

if (Input.GetKeyDown(KeyCode.D)) //_______________________________________MOVING RIGHT
{
rigid.velocity = new Vector2(1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.D))
{
rigid.velocity = new Vector2(0, 0);
}
}









share|improve this question




















  • 1





    You need to stop setting your velocity to zero every time a key is lifted up. You only want to modify one component of the vector in that case, and also you need to consider the state of the key for going in the opposite direction. Also, is there a reason you aren't using Input.GetAxis? It does a lot of the stuff you're trying to do here.

    – Ruzihm
    Nov 14 '18 at 18:22











  • As Ruzihm says the first thing I would try is instead of setting (0,0) only change x or y the respective one you change in the other case of if.

    – derHugo
    Nov 14 '18 at 18:25













  • There is no reason for not using Axis, I simply don't know hot to use it. I tried using it before, but I could not implement it as negative value (even if I tried putting '-' before it, it did not work for some reasons). When i will not set value to 0, 0 after lifting key up, character will fly at set direction, I do not know any other way to stop it.

    – lubeszz
    Nov 14 '18 at 18:29











  • @lubeszz I edited my answer to include how to use Input.GetKeyUp/GetKeyDown if you must.

    – Ruzihm
    Nov 14 '18 at 18:42
















2












2








2








So basically I am trying to make player movement system like one in RPG Maker with 8 directions. Somehow I succeded, but only partially. When I am trying to suddenly change direction for ex. from up to left, character stutter and do not want to move without releasing all keys first.



Gravity scale is disabled or more like set to 0, body type is dynamic.



Here is a code:



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveChar : MonoBehaviour {

Rigidbody2D rigid;

public float Speed;

// Use this for initialization
void Start () {

rigid = GetComponent<Rigidbody2D>();

}

// Update is called once per frame
void Update () {

// float horiz = Input.GetAxis("Horizontal");

// float vert = Input.GetAxis("Vertical");

if(Input.GetKeyDown(KeyCode.W)) //________________________________________MOVING UP

{
rigid.velocity = new Vector2(rigid.velocity.x, 1 * Speed);
}
else if(Input.GetKeyUp(KeyCode.W))
{
rigid.velocity = new Vector2(0, 0);
}

if (Input.GetKeyDown(KeyCode.S)) //_______________________________________MOVING DOWN
{
rigid.velocity = new Vector2(rigid.velocity.x, -1 * Speed);
}
else if (Input.GetKeyUp(KeyCode.S))
{
rigid.velocity = new Vector2(0, 0);
}


if (Input.GetKeyDown(KeyCode.A)) //_______________________________________MOVING LEFT
{
rigid.velocity = new Vector2(-1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.A))
{
rigid.velocity = new Vector2(0, 0);
}

if (Input.GetKeyDown(KeyCode.D)) //_______________________________________MOVING RIGHT
{
rigid.velocity = new Vector2(1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.D))
{
rigid.velocity = new Vector2(0, 0);
}
}









share|improve this question
















So basically I am trying to make player movement system like one in RPG Maker with 8 directions. Somehow I succeded, but only partially. When I am trying to suddenly change direction for ex. from up to left, character stutter and do not want to move without releasing all keys first.



Gravity scale is disabled or more like set to 0, body type is dynamic.



Here is a code:



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveChar : MonoBehaviour {

Rigidbody2D rigid;

public float Speed;

// Use this for initialization
void Start () {

rigid = GetComponent<Rigidbody2D>();

}

// Update is called once per frame
void Update () {

// float horiz = Input.GetAxis("Horizontal");

// float vert = Input.GetAxis("Vertical");

if(Input.GetKeyDown(KeyCode.W)) //________________________________________MOVING UP

{
rigid.velocity = new Vector2(rigid.velocity.x, 1 * Speed);
}
else if(Input.GetKeyUp(KeyCode.W))
{
rigid.velocity = new Vector2(0, 0);
}

if (Input.GetKeyDown(KeyCode.S)) //_______________________________________MOVING DOWN
{
rigid.velocity = new Vector2(rigid.velocity.x, -1 * Speed);
}
else if (Input.GetKeyUp(KeyCode.S))
{
rigid.velocity = new Vector2(0, 0);
}


if (Input.GetKeyDown(KeyCode.A)) //_______________________________________MOVING LEFT
{
rigid.velocity = new Vector2(-1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.A))
{
rigid.velocity = new Vector2(0, 0);
}

if (Input.GetKeyDown(KeyCode.D)) //_______________________________________MOVING RIGHT
{
rigid.velocity = new Vector2(1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.D))
{
rigid.velocity = new Vector2(0, 0);
}
}






c# unity3d 2d






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 18:56









Ruzihm

3,71611627




3,71611627










asked Nov 14 '18 at 18:08









lubeszzlubeszz

202




202








  • 1





    You need to stop setting your velocity to zero every time a key is lifted up. You only want to modify one component of the vector in that case, and also you need to consider the state of the key for going in the opposite direction. Also, is there a reason you aren't using Input.GetAxis? It does a lot of the stuff you're trying to do here.

    – Ruzihm
    Nov 14 '18 at 18:22











  • As Ruzihm says the first thing I would try is instead of setting (0,0) only change x or y the respective one you change in the other case of if.

    – derHugo
    Nov 14 '18 at 18:25













  • There is no reason for not using Axis, I simply don't know hot to use it. I tried using it before, but I could not implement it as negative value (even if I tried putting '-' before it, it did not work for some reasons). When i will not set value to 0, 0 after lifting key up, character will fly at set direction, I do not know any other way to stop it.

    – lubeszz
    Nov 14 '18 at 18:29











  • @lubeszz I edited my answer to include how to use Input.GetKeyUp/GetKeyDown if you must.

    – Ruzihm
    Nov 14 '18 at 18:42
















  • 1





    You need to stop setting your velocity to zero every time a key is lifted up. You only want to modify one component of the vector in that case, and also you need to consider the state of the key for going in the opposite direction. Also, is there a reason you aren't using Input.GetAxis? It does a lot of the stuff you're trying to do here.

    – Ruzihm
    Nov 14 '18 at 18:22











  • As Ruzihm says the first thing I would try is instead of setting (0,0) only change x or y the respective one you change in the other case of if.

    – derHugo
    Nov 14 '18 at 18:25













  • There is no reason for not using Axis, I simply don't know hot to use it. I tried using it before, but I could not implement it as negative value (even if I tried putting '-' before it, it did not work for some reasons). When i will not set value to 0, 0 after lifting key up, character will fly at set direction, I do not know any other way to stop it.

    – lubeszz
    Nov 14 '18 at 18:29











  • @lubeszz I edited my answer to include how to use Input.GetKeyUp/GetKeyDown if you must.

    – Ruzihm
    Nov 14 '18 at 18:42










1




1





You need to stop setting your velocity to zero every time a key is lifted up. You only want to modify one component of the vector in that case, and also you need to consider the state of the key for going in the opposite direction. Also, is there a reason you aren't using Input.GetAxis? It does a lot of the stuff you're trying to do here.

– Ruzihm
Nov 14 '18 at 18:22





You need to stop setting your velocity to zero every time a key is lifted up. You only want to modify one component of the vector in that case, and also you need to consider the state of the key for going in the opposite direction. Also, is there a reason you aren't using Input.GetAxis? It does a lot of the stuff you're trying to do here.

– Ruzihm
Nov 14 '18 at 18:22













As Ruzihm says the first thing I would try is instead of setting (0,0) only change x or y the respective one you change in the other case of if.

– derHugo
Nov 14 '18 at 18:25







As Ruzihm says the first thing I would try is instead of setting (0,0) only change x or y the respective one you change in the other case of if.

– derHugo
Nov 14 '18 at 18:25















There is no reason for not using Axis, I simply don't know hot to use it. I tried using it before, but I could not implement it as negative value (even if I tried putting '-' before it, it did not work for some reasons). When i will not set value to 0, 0 after lifting key up, character will fly at set direction, I do not know any other way to stop it.

– lubeszz
Nov 14 '18 at 18:29





There is no reason for not using Axis, I simply don't know hot to use it. I tried using it before, but I could not implement it as negative value (even if I tried putting '-' before it, it did not work for some reasons). When i will not set value to 0, 0 after lifting key up, character will fly at set direction, I do not know any other way to stop it.

– lubeszz
Nov 14 '18 at 18:29













@lubeszz I edited my answer to include how to use Input.GetKeyUp/GetKeyDown if you must.

– Ruzihm
Nov 14 '18 at 18:42







@lubeszz I edited my answer to include how to use Input.GetKeyUp/GetKeyDown if you must.

– Ruzihm
Nov 14 '18 at 18:42














2 Answers
2






active

oldest

votes


















2














Use Input.GetAxis(axisName) to avoid conflicting cases in your input code. Also, use AddForce to play nicely with other rigidbodies.



Vector2 oldV = rigid.velocity;
float horiz = Input.GetAxis("Horizontal");
float vert = Input.GetAxis("Vertical");

Vector2 newV = new Vector2(horiz * Speed, vert * Speed);
rigid.AddForce(newV-oldV, ForceMode2D.Impulse);


Alternatively, keep track of your own axes when keys are lifted/pressed down



public float horiz;
public float vert;

void Start() {
horiz = 0f;
vert = 0f;
if (Input.GetKey(KeyCode.A)) horiz -= 1f;
if (Input.GetKey(KeyCode.D)) horiz += 1f;
if (Input.GetKey(KeyCode.S)) vert -= 1f;
if (Input.GetKey(KeyCode.W)) vert += 1f;
}

void Update () {
Vector2 oldV = rigid.velocity;

if(Input.GetKeyDown(KeyCode.W)) vert += 1f;
else if(Input.GetKeyUp(KeyCode.W)) vert -= 1f;

if (Input.GetKeyDown(KeyCode.S)) vert -= 1f;
else if (Input.GetKeyUp(KeyCode.S)) vert += 1f;

if (Input.GetKeyDown(KeyCode.A)) horiz -= 1f;
else if (Input.GetKeyUp(KeyCode.A)) horiz += 1f;

if (Input.GetKeyDown(KeyCode.D)) horiz += 1f;
else if (Input.GetKeyUp(KeyCode.D)) horiz -= 1f;

Vector2 newV = new Vector2(horiz * Speed, vert * Speed);
rigid.AddForce(newV-oldV, ForceMode2D.Impulse);
}





share|improve this answer


























  • Thanks a lot, works fine now!

    – lubeszz
    Nov 14 '18 at 19:06











  • If this most helped answer your question, please mark it as the selected answer by clicking on the grey check mark under the voting buttons. This will help people with similar problems find this answer in the future. It will also give me some Stack Overflow points ;)

    – Ruzihm
    Nov 14 '18 at 19:23



















0














There is only a small problem with releasing the button. Add the original force in the direction that is not affected by the key. The whole script should look like this:



void Update()
{
if (Input.GetKeyDown(KeyCode.W)) //________________________________________MOVING UP
{
rigid.velocity = new Vector2(rigid.velocity.x, 1 * Speed);
}
else if (Input.GetKeyUp(KeyCode.W))
{
rigid.velocity = new Vector2(rigid.velocity.x, 0);
}

if (Input.GetKeyDown(KeyCode.S)) //_______________________________________MOVING DOWN
{
rigid.velocity = new Vector2(rigid.velocity.x, -1 * Speed);
}
else if (Input.GetKeyUp(KeyCode.S))
{
rigid.velocity = new Vector2(rigid.velocity.x, 0);
}


if (Input.GetKeyDown(KeyCode.A)) //_______________________________________MOVING LEFT
{
rigid.velocity = new Vector2(-1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.A))
{
rigid.velocity = new Vector2(0, rigid.velocity.y);
}

if (Input.GetKeyDown(KeyCode.D)) //_______________________________________MOVING RIGHT
{
rigid.velocity = new Vector2(1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.D))
{
rigid.velocity = new Vector2(0, rigid.velocity.y);
}
}


I hope this helps you.






share|improve this answer
























  • It helped a bit, but there still are some stutters

    – lubeszz
    Nov 14 '18 at 18:39











  • Additional: Change Interpolate parameter of rigidbody component to "Interpolate"

    – Raph_Wa
    Nov 14 '18 at 18:43











  • If you press both A and D then only release D, this code won't move the body horizontally at all.

    – Ruzihm
    Nov 14 '18 at 18:44













  • I was not sure if that was wanted @Ruzihm

    – Raph_Wa
    Nov 14 '18 at 18:46











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306366%2fstuttering-movement-in-unity-2d%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Use Input.GetAxis(axisName) to avoid conflicting cases in your input code. Also, use AddForce to play nicely with other rigidbodies.



Vector2 oldV = rigid.velocity;
float horiz = Input.GetAxis("Horizontal");
float vert = Input.GetAxis("Vertical");

Vector2 newV = new Vector2(horiz * Speed, vert * Speed);
rigid.AddForce(newV-oldV, ForceMode2D.Impulse);


Alternatively, keep track of your own axes when keys are lifted/pressed down



public float horiz;
public float vert;

void Start() {
horiz = 0f;
vert = 0f;
if (Input.GetKey(KeyCode.A)) horiz -= 1f;
if (Input.GetKey(KeyCode.D)) horiz += 1f;
if (Input.GetKey(KeyCode.S)) vert -= 1f;
if (Input.GetKey(KeyCode.W)) vert += 1f;
}

void Update () {
Vector2 oldV = rigid.velocity;

if(Input.GetKeyDown(KeyCode.W)) vert += 1f;
else if(Input.GetKeyUp(KeyCode.W)) vert -= 1f;

if (Input.GetKeyDown(KeyCode.S)) vert -= 1f;
else if (Input.GetKeyUp(KeyCode.S)) vert += 1f;

if (Input.GetKeyDown(KeyCode.A)) horiz -= 1f;
else if (Input.GetKeyUp(KeyCode.A)) horiz += 1f;

if (Input.GetKeyDown(KeyCode.D)) horiz += 1f;
else if (Input.GetKeyUp(KeyCode.D)) horiz -= 1f;

Vector2 newV = new Vector2(horiz * Speed, vert * Speed);
rigid.AddForce(newV-oldV, ForceMode2D.Impulse);
}





share|improve this answer


























  • Thanks a lot, works fine now!

    – lubeszz
    Nov 14 '18 at 19:06











  • If this most helped answer your question, please mark it as the selected answer by clicking on the grey check mark under the voting buttons. This will help people with similar problems find this answer in the future. It will also give me some Stack Overflow points ;)

    – Ruzihm
    Nov 14 '18 at 19:23
















2














Use Input.GetAxis(axisName) to avoid conflicting cases in your input code. Also, use AddForce to play nicely with other rigidbodies.



Vector2 oldV = rigid.velocity;
float horiz = Input.GetAxis("Horizontal");
float vert = Input.GetAxis("Vertical");

Vector2 newV = new Vector2(horiz * Speed, vert * Speed);
rigid.AddForce(newV-oldV, ForceMode2D.Impulse);


Alternatively, keep track of your own axes when keys are lifted/pressed down



public float horiz;
public float vert;

void Start() {
horiz = 0f;
vert = 0f;
if (Input.GetKey(KeyCode.A)) horiz -= 1f;
if (Input.GetKey(KeyCode.D)) horiz += 1f;
if (Input.GetKey(KeyCode.S)) vert -= 1f;
if (Input.GetKey(KeyCode.W)) vert += 1f;
}

void Update () {
Vector2 oldV = rigid.velocity;

if(Input.GetKeyDown(KeyCode.W)) vert += 1f;
else if(Input.GetKeyUp(KeyCode.W)) vert -= 1f;

if (Input.GetKeyDown(KeyCode.S)) vert -= 1f;
else if (Input.GetKeyUp(KeyCode.S)) vert += 1f;

if (Input.GetKeyDown(KeyCode.A)) horiz -= 1f;
else if (Input.GetKeyUp(KeyCode.A)) horiz += 1f;

if (Input.GetKeyDown(KeyCode.D)) horiz += 1f;
else if (Input.GetKeyUp(KeyCode.D)) horiz -= 1f;

Vector2 newV = new Vector2(horiz * Speed, vert * Speed);
rigid.AddForce(newV-oldV, ForceMode2D.Impulse);
}





share|improve this answer


























  • Thanks a lot, works fine now!

    – lubeszz
    Nov 14 '18 at 19:06











  • If this most helped answer your question, please mark it as the selected answer by clicking on the grey check mark under the voting buttons. This will help people with similar problems find this answer in the future. It will also give me some Stack Overflow points ;)

    – Ruzihm
    Nov 14 '18 at 19:23














2












2








2







Use Input.GetAxis(axisName) to avoid conflicting cases in your input code. Also, use AddForce to play nicely with other rigidbodies.



Vector2 oldV = rigid.velocity;
float horiz = Input.GetAxis("Horizontal");
float vert = Input.GetAxis("Vertical");

Vector2 newV = new Vector2(horiz * Speed, vert * Speed);
rigid.AddForce(newV-oldV, ForceMode2D.Impulse);


Alternatively, keep track of your own axes when keys are lifted/pressed down



public float horiz;
public float vert;

void Start() {
horiz = 0f;
vert = 0f;
if (Input.GetKey(KeyCode.A)) horiz -= 1f;
if (Input.GetKey(KeyCode.D)) horiz += 1f;
if (Input.GetKey(KeyCode.S)) vert -= 1f;
if (Input.GetKey(KeyCode.W)) vert += 1f;
}

void Update () {
Vector2 oldV = rigid.velocity;

if(Input.GetKeyDown(KeyCode.W)) vert += 1f;
else if(Input.GetKeyUp(KeyCode.W)) vert -= 1f;

if (Input.GetKeyDown(KeyCode.S)) vert -= 1f;
else if (Input.GetKeyUp(KeyCode.S)) vert += 1f;

if (Input.GetKeyDown(KeyCode.A)) horiz -= 1f;
else if (Input.GetKeyUp(KeyCode.A)) horiz += 1f;

if (Input.GetKeyDown(KeyCode.D)) horiz += 1f;
else if (Input.GetKeyUp(KeyCode.D)) horiz -= 1f;

Vector2 newV = new Vector2(horiz * Speed, vert * Speed);
rigid.AddForce(newV-oldV, ForceMode2D.Impulse);
}





share|improve this answer















Use Input.GetAxis(axisName) to avoid conflicting cases in your input code. Also, use AddForce to play nicely with other rigidbodies.



Vector2 oldV = rigid.velocity;
float horiz = Input.GetAxis("Horizontal");
float vert = Input.GetAxis("Vertical");

Vector2 newV = new Vector2(horiz * Speed, vert * Speed);
rigid.AddForce(newV-oldV, ForceMode2D.Impulse);


Alternatively, keep track of your own axes when keys are lifted/pressed down



public float horiz;
public float vert;

void Start() {
horiz = 0f;
vert = 0f;
if (Input.GetKey(KeyCode.A)) horiz -= 1f;
if (Input.GetKey(KeyCode.D)) horiz += 1f;
if (Input.GetKey(KeyCode.S)) vert -= 1f;
if (Input.GetKey(KeyCode.W)) vert += 1f;
}

void Update () {
Vector2 oldV = rigid.velocity;

if(Input.GetKeyDown(KeyCode.W)) vert += 1f;
else if(Input.GetKeyUp(KeyCode.W)) vert -= 1f;

if (Input.GetKeyDown(KeyCode.S)) vert -= 1f;
else if (Input.GetKeyUp(KeyCode.S)) vert += 1f;

if (Input.GetKeyDown(KeyCode.A)) horiz -= 1f;
else if (Input.GetKeyUp(KeyCode.A)) horiz += 1f;

if (Input.GetKeyDown(KeyCode.D)) horiz += 1f;
else if (Input.GetKeyUp(KeyCode.D)) horiz -= 1f;

Vector2 newV = new Vector2(horiz * Speed, vert * Speed);
rigid.AddForce(newV-oldV, ForceMode2D.Impulse);
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 18:48

























answered Nov 14 '18 at 18:29









RuzihmRuzihm

3,71611627




3,71611627













  • Thanks a lot, works fine now!

    – lubeszz
    Nov 14 '18 at 19:06











  • If this most helped answer your question, please mark it as the selected answer by clicking on the grey check mark under the voting buttons. This will help people with similar problems find this answer in the future. It will also give me some Stack Overflow points ;)

    – Ruzihm
    Nov 14 '18 at 19:23



















  • Thanks a lot, works fine now!

    – lubeszz
    Nov 14 '18 at 19:06











  • If this most helped answer your question, please mark it as the selected answer by clicking on the grey check mark under the voting buttons. This will help people with similar problems find this answer in the future. It will also give me some Stack Overflow points ;)

    – Ruzihm
    Nov 14 '18 at 19:23

















Thanks a lot, works fine now!

– lubeszz
Nov 14 '18 at 19:06





Thanks a lot, works fine now!

– lubeszz
Nov 14 '18 at 19:06













If this most helped answer your question, please mark it as the selected answer by clicking on the grey check mark under the voting buttons. This will help people with similar problems find this answer in the future. It will also give me some Stack Overflow points ;)

– Ruzihm
Nov 14 '18 at 19:23





If this most helped answer your question, please mark it as the selected answer by clicking on the grey check mark under the voting buttons. This will help people with similar problems find this answer in the future. It will also give me some Stack Overflow points ;)

– Ruzihm
Nov 14 '18 at 19:23













0














There is only a small problem with releasing the button. Add the original force in the direction that is not affected by the key. The whole script should look like this:



void Update()
{
if (Input.GetKeyDown(KeyCode.W)) //________________________________________MOVING UP
{
rigid.velocity = new Vector2(rigid.velocity.x, 1 * Speed);
}
else if (Input.GetKeyUp(KeyCode.W))
{
rigid.velocity = new Vector2(rigid.velocity.x, 0);
}

if (Input.GetKeyDown(KeyCode.S)) //_______________________________________MOVING DOWN
{
rigid.velocity = new Vector2(rigid.velocity.x, -1 * Speed);
}
else if (Input.GetKeyUp(KeyCode.S))
{
rigid.velocity = new Vector2(rigid.velocity.x, 0);
}


if (Input.GetKeyDown(KeyCode.A)) //_______________________________________MOVING LEFT
{
rigid.velocity = new Vector2(-1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.A))
{
rigid.velocity = new Vector2(0, rigid.velocity.y);
}

if (Input.GetKeyDown(KeyCode.D)) //_______________________________________MOVING RIGHT
{
rigid.velocity = new Vector2(1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.D))
{
rigid.velocity = new Vector2(0, rigid.velocity.y);
}
}


I hope this helps you.






share|improve this answer
























  • It helped a bit, but there still are some stutters

    – lubeszz
    Nov 14 '18 at 18:39











  • Additional: Change Interpolate parameter of rigidbody component to "Interpolate"

    – Raph_Wa
    Nov 14 '18 at 18:43











  • If you press both A and D then only release D, this code won't move the body horizontally at all.

    – Ruzihm
    Nov 14 '18 at 18:44













  • I was not sure if that was wanted @Ruzihm

    – Raph_Wa
    Nov 14 '18 at 18:46
















0














There is only a small problem with releasing the button. Add the original force in the direction that is not affected by the key. The whole script should look like this:



void Update()
{
if (Input.GetKeyDown(KeyCode.W)) //________________________________________MOVING UP
{
rigid.velocity = new Vector2(rigid.velocity.x, 1 * Speed);
}
else if (Input.GetKeyUp(KeyCode.W))
{
rigid.velocity = new Vector2(rigid.velocity.x, 0);
}

if (Input.GetKeyDown(KeyCode.S)) //_______________________________________MOVING DOWN
{
rigid.velocity = new Vector2(rigid.velocity.x, -1 * Speed);
}
else if (Input.GetKeyUp(KeyCode.S))
{
rigid.velocity = new Vector2(rigid.velocity.x, 0);
}


if (Input.GetKeyDown(KeyCode.A)) //_______________________________________MOVING LEFT
{
rigid.velocity = new Vector2(-1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.A))
{
rigid.velocity = new Vector2(0, rigid.velocity.y);
}

if (Input.GetKeyDown(KeyCode.D)) //_______________________________________MOVING RIGHT
{
rigid.velocity = new Vector2(1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.D))
{
rigid.velocity = new Vector2(0, rigid.velocity.y);
}
}


I hope this helps you.






share|improve this answer
























  • It helped a bit, but there still are some stutters

    – lubeszz
    Nov 14 '18 at 18:39











  • Additional: Change Interpolate parameter of rigidbody component to "Interpolate"

    – Raph_Wa
    Nov 14 '18 at 18:43











  • If you press both A and D then only release D, this code won't move the body horizontally at all.

    – Ruzihm
    Nov 14 '18 at 18:44













  • I was not sure if that was wanted @Ruzihm

    – Raph_Wa
    Nov 14 '18 at 18:46














0












0








0







There is only a small problem with releasing the button. Add the original force in the direction that is not affected by the key. The whole script should look like this:



void Update()
{
if (Input.GetKeyDown(KeyCode.W)) //________________________________________MOVING UP
{
rigid.velocity = new Vector2(rigid.velocity.x, 1 * Speed);
}
else if (Input.GetKeyUp(KeyCode.W))
{
rigid.velocity = new Vector2(rigid.velocity.x, 0);
}

if (Input.GetKeyDown(KeyCode.S)) //_______________________________________MOVING DOWN
{
rigid.velocity = new Vector2(rigid.velocity.x, -1 * Speed);
}
else if (Input.GetKeyUp(KeyCode.S))
{
rigid.velocity = new Vector2(rigid.velocity.x, 0);
}


if (Input.GetKeyDown(KeyCode.A)) //_______________________________________MOVING LEFT
{
rigid.velocity = new Vector2(-1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.A))
{
rigid.velocity = new Vector2(0, rigid.velocity.y);
}

if (Input.GetKeyDown(KeyCode.D)) //_______________________________________MOVING RIGHT
{
rigid.velocity = new Vector2(1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.D))
{
rigid.velocity = new Vector2(0, rigid.velocity.y);
}
}


I hope this helps you.






share|improve this answer













There is only a small problem with releasing the button. Add the original force in the direction that is not affected by the key. The whole script should look like this:



void Update()
{
if (Input.GetKeyDown(KeyCode.W)) //________________________________________MOVING UP
{
rigid.velocity = new Vector2(rigid.velocity.x, 1 * Speed);
}
else if (Input.GetKeyUp(KeyCode.W))
{
rigid.velocity = new Vector2(rigid.velocity.x, 0);
}

if (Input.GetKeyDown(KeyCode.S)) //_______________________________________MOVING DOWN
{
rigid.velocity = new Vector2(rigid.velocity.x, -1 * Speed);
}
else if (Input.GetKeyUp(KeyCode.S))
{
rigid.velocity = new Vector2(rigid.velocity.x, 0);
}


if (Input.GetKeyDown(KeyCode.A)) //_______________________________________MOVING LEFT
{
rigid.velocity = new Vector2(-1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.A))
{
rigid.velocity = new Vector2(0, rigid.velocity.y);
}

if (Input.GetKeyDown(KeyCode.D)) //_______________________________________MOVING RIGHT
{
rigid.velocity = new Vector2(1 * Speed, rigid.velocity.y);
}
else if (Input.GetKeyUp(KeyCode.D))
{
rigid.velocity = new Vector2(0, rigid.velocity.y);
}
}


I hope this helps you.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 18:34









Raph_WaRaph_Wa

293




293













  • It helped a bit, but there still are some stutters

    – lubeszz
    Nov 14 '18 at 18:39











  • Additional: Change Interpolate parameter of rigidbody component to "Interpolate"

    – Raph_Wa
    Nov 14 '18 at 18:43











  • If you press both A and D then only release D, this code won't move the body horizontally at all.

    – Ruzihm
    Nov 14 '18 at 18:44













  • I was not sure if that was wanted @Ruzihm

    – Raph_Wa
    Nov 14 '18 at 18:46



















  • It helped a bit, but there still are some stutters

    – lubeszz
    Nov 14 '18 at 18:39











  • Additional: Change Interpolate parameter of rigidbody component to "Interpolate"

    – Raph_Wa
    Nov 14 '18 at 18:43











  • If you press both A and D then only release D, this code won't move the body horizontally at all.

    – Ruzihm
    Nov 14 '18 at 18:44













  • I was not sure if that was wanted @Ruzihm

    – Raph_Wa
    Nov 14 '18 at 18:46

















It helped a bit, but there still are some stutters

– lubeszz
Nov 14 '18 at 18:39





It helped a bit, but there still are some stutters

– lubeszz
Nov 14 '18 at 18:39













Additional: Change Interpolate parameter of rigidbody component to "Interpolate"

– Raph_Wa
Nov 14 '18 at 18:43





Additional: Change Interpolate parameter of rigidbody component to "Interpolate"

– Raph_Wa
Nov 14 '18 at 18:43













If you press both A and D then only release D, this code won't move the body horizontally at all.

– Ruzihm
Nov 14 '18 at 18:44







If you press both A and D then only release D, this code won't move the body horizontally at all.

– Ruzihm
Nov 14 '18 at 18:44















I was not sure if that was wanted @Ruzihm

– Raph_Wa
Nov 14 '18 at 18:46





I was not sure if that was wanted @Ruzihm

– Raph_Wa
Nov 14 '18 at 18:46


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306366%2fstuttering-movement-in-unity-2d%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values