- Home /
Question by
drahsid · Apr 18, 2015 at 08:35 PM ·
c#movementcontroller
Character is sliding?
I made a controller for a 2D character, but it slides, and seems to continuously add force in certain directions?
Help?
Reading input: private bool kJmp; private bool kUp; private bool kDwn; private bool Atk; private playerController iput; private float LR;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
iput = GetComponent<playerController> ();
if (!kJmp) {
kJmp = Input.GetKey (KeyCode.Space);
} else {
if (!kUp) {
kUp = Input.GetKeyDown (KeyCode.W);
}
else {
if (!kDwn) {
kDwn = Input.GetKeyDown (KeyCode.S);
}
else {
if (!Atk) {
Atk = Input.GetKeyDown(KeyCode.X);
}
}
}
}
}
private void FixedUpdate () {
LR = Input.GetAxis("Horizontal");
iput.Move(kJmp, Atk, kUp, kDwn, LR);
kJmp = false;
Atk = false;
kUp = false;
kDwn = false;
LR = 0.0f;
Applying the motion:
public void Move(bool Jmp, bool Atk, bool Up, bool Down,float LR) {
if (Jmp == true && jumping == false && Grounded == true) {
jumping = true;
rb.AddForce (new Vector2 (0, Jump_Force));
jumping = false;
}
if (Atk == true && Grounded == true) {
Instantiate(Slash, Player.transform.position, Quaternion.identity);
}
if (Up == true && Grounded == true) {
print ("Looking up");
anim.SetInteger("Look", 1);
}
else {
if (Down == true && Grounded == true) {
anim.SetInteger("Look",-1);
}
}
if (LR != 0){
rb.velocity = new Vector2 (Speed * LR, rb.velocity.y);
if (LR < 0){
Facing = 1;
}
else {
if (LR > 0){
Facing = -1;
}
else {
if (LR == 0){
rb.velocity = new Vector2 (0,rb.velocity.y);
}
else {
if (LR != 0 && jumping == false && Grounded == true){
anim.SetBool("Moving",true);
}
}
}
}
}
Flip ();
Jmp = false;
Atk = false;
Up = false;
Down = false;
LR = 0.0f;
}
Comment
Answer by Cherno · Apr 18, 2015 at 08:38 PM
You could set the velocity to 0 once no movement button is pressed / an opposing direction button is pressed. Many people say that setting the velocity directly is bad, I personally never had a problem, it's just a matter of keeping control over the rigidbody's behaviour.
I tried that and it still slid around, what are the other methods?