For some reason my player cannot jump up and move left and right at the same time. Its wierd. Here is the controller script. This is a 2D Platformer.
public class PlayerController : MonoBehaviour
{
// Movement Settings
[Header(“Movement Settings:”)]
[Space]
[Range(1, 10)]
public float movementSpeed;
[Range(1, 10)]
public float jumpHeight;
private bool canJump;
// Components
private Rigidbody2D rigid;
void Start()
{
// Set Components to their Corresponding Variables
rigid = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
// Jumping
if(canJump && Input.GetKeyDown(KeyCode.W))
{
rigid.AddForce(Vector2.up * jumpHeight, ForceMode2D.Impulse);
}
}
void Update()
{
// Movement Left or Right
Vector3 moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0, 0);
transform.Translate(moveDirection * movementSpeed * Time.deltaTime);
// Check if Player is Grounded
Vector2 groundCheckStart = new Vector2(transform.position.x, transform.position.y - 0.55f);
Vector2 groundCheckEnd = new Vector2(transform.position.x, transform.position.y - 0.6f);
Debug.DrawLine(groundCheckStart, groundCheckEnd, Color.red);
RaycastHit2D groundCheck = Physics2D.Linecast(groundCheckStart, groundCheckEnd);
if (groundCheck.collider != null)
{
canJump = true;
}
else
{
canJump = false;
}
}
}