using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody2D rb;
private bool pressed;
private bool movingLeft;
void Start()
{
rb = GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(-3, 5);
movingLeft = true;
}
void Update()
{
KeyboardInput();
PlayerVelocity();
}
private void PlayerVelocity()
{
if (pressed && movingLeft)
{
rb.velocity = new Vector2(3, 5);
movingLeft = false;
}
if (pressed && !movingLeft)
{
rb.velocity = new Vector2(-3, 5);
movingLeft = true;
}
}
private void KeyboardInput()
{
if (Input.GetKeyDown("space"))
{
pressed = true;
pressed = false;
}
}
}
I want the player to start moving up left and then change moving up left or up right when pressing space. With my script, the rigidbody moves with the start velocity but doesn’t change its direction when pressing space. Why?