2 minor gameplay problems
so first problem is i want my player to stop going up when you press uparrow. like when 1.5 seconds have gone, getkey goes to getkeyup. other problem is my character falls slower when you move in x axis left or right. there might be gravity but im not seeing any.
// animator parameters
private int Speed = 100;
private bool Jumping;
private float JumpSpeed = 150;
private bool jeps;
private bool fall;
private bool DownB;
// script own data
private float timer;
private float sTime;
private float limit = 1.5f;
private SpriteRenderer sR;
private Rigidbody2D rb2d;
private Animator anim;
void Start ()
{
sR = GetComponent<SpriteRenderer>();
rb2d = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
void Update ()
{
anim.SetBool("moving", jeps);
anim.SetBool("Jumping", Jumping);
anim.SetBool("falling", fall);
anim.SetBool("DownB", DownB);
// Keyboard settings
if (Input.GetKey(KeyCode.LeftArrow))
{
jeps = true;
rb2d.velocity = new Vector2(-Speed, 0);
if (sR != null)
{
// flip the sprite
sR.flipX = true;
}
}
if (Input.GetKeyUp(KeyCode.LeftArrow))
{
jeps = false;
}
if (Input.GetKey(KeyCode.RightArrow))
{
jeps = true;
rb2d.velocity = new Vector2(Speed, 0);
if (sR != null)
{
// flip the sprite
sR.flipX = false;
}
}
if (Input.GetKeyUp(KeyCode.RightArrow))
{
jeps = false;
}
if (Input.GetKey(KeyCode.UpArrow))
{
timer = Time.time;
sTime = Time.time - timer;
if (sTime >= limit)
{
Input.GetKeyUp(KeyCode.UpArrow);
}
Jumping = true;
rb2d.velocity = new Vector2(0, JumpSpeed);
}
if (Input.GetKeyUp(KeyCode.UpArrow))
{
jeps = false;
Jumping = false;
}
if (Input.GetKey(KeyCode.DownArrow))
{
DownB = true;
fall = true;
rb2d.velocity = new Vector2(0, -JumpSpeed);
}
if (Input.GetKeyUp(KeyCode.DownArrow))
{
fall = false;
}
}
Hello.
I dont understand your question... And you did 2 questions. I recommend you to make 1 post for each question, with a correct title ( title must contain the question issue, or people will not enter the post)
Another thing is, if you are detecting key press/keyup, (As a good practice) you should use if and elseif (not only if). This way will increase performance. Its clear that Get$$anonymous$$ey and Get$$anonymous$$eyUp the same key will never happens simultaneously, so you can do if Get$$anonymous$$ey and else if Get$$anonymous$$EyDown.
Bye!
Your answer
![](https://koobas.hobune.stream/wayback/20220612201422im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Script preventing gravity from working. 1 Answer
Unity3d gravity not working in movement script 0 Answers
Script preventing Gravity from working 1 Answer
Making an object drop when screen is touched 1 Answer
No Gravity Jump 0 Answers