- Home /
Need help fixing 2D Top-Down controls and collisions
So, I am working on a 2D top-down game using the 2D Toolkit in Unity. Thus far everything has been working fine, but you can see in this video that I'm having some issues with controls/collisions. The build in the video for controls is bellow, though 1) it is a much older working build, and 2) I have made several attempts at a state-based system. Unfortunantly, I have come to realize that I was not actually doing it state based, or at least in the correct manor. While I would appreciate pointers on the bellow script (C#), any outlines on doing an actual state-based directional controller would be even more appreciated:
using UnityEngine;
using System.Collections;
public class Animation_Controler : MonoBehaviour
{
tk2dAnimatedSprite anim;
float speed = 0.25f;
int lastButtonPress = 0;
// Use this for initialization
void Start ()
{
anim = GetComponent<tk2dAnimatedSprite>();
}
// Update is called once per frame
void Update ()
{
//movement conditions
else
{
//Moving Animations
//Move Up
if (Input.GetKey(KeyCode.UpArrow))
{
transform.Translate(0,0,speed * Time.deltaTime,Space.World);
if (Input.GetKeyDown(KeyCode.UpArrow))
{
anim.Play("Walk_Up");
}
lastButtonPress = 0;
return;
}
//Move Left
else if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Translate(-speed * Time.deltaTime,0,0,Space.World);
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
anim.Play("Walk_Left");
}
lastButtonPress = 1;
return;
}
//Move Down
else if (Input.GetKey(KeyCode.DownArrow))
{
transform.Translate(0,0,-speed * Time.deltaTime,Space.World);
if (Input.GetKeyDown(KeyCode.DownArrow))
{
anim.Play("Walk_Forward");
}
lastButtonPress = 2;
return;
}
//Move Right
else if (Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(speed * Time.deltaTime,0,0,Space.World);
if (Input.GetKeyDown(KeyCode.RightArrow))
{
anim.Play("Walk_Right");
}
lastButtonPress = 3;
return;
}
//Idle Animations
else if (lastButtonPress == 0)
{
anim.Play("Up_Idle");
}
else if (lastButtonPress == 1)
{
anim.Play("Left_Idle");
}
else if (lastButtonPress == 2)
{
anim.Play("Forward_Idle");
}
else if (lastButtonPress == 3)
{
anim.Play("Right_Idle");
}
}
}
}
Finally, you can see the shaking collisions in the video. I do not know what is causing this, if it is inherent to unity, or if it actually is an issue in the 2D Toolkit. If this is something anyone knows how to fix/or what the cause is, I would appreciate an explanation, but its not my primary concern at this point in time.
Your answer
![](https://koobas.hobune.stream/wayback/20220613082825im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
How can I code an enemy attack? (Top Down Melee Attack) 1 Answer
Strange 2D sprite behavior 0 Answers
Character Arm Joints. 1 Answer
Collision on specific animation 1 Answer