I jump and the character just goes straight up. the sprite still looks left and right when i press a and d
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player : MonoBehaviour
{
//Setting up engines, animation, speed, and whatnot
private Rigidbody2D rb;
private Animator anim;
public int crystals = 0;
//Animation
private enum State {idle, running, jumping, falling}
private State state = State.idle;
private Collider2D coll;
[SerializeField] private float speed = 200f;
[SerializeField] private float jumpForce = 25f;
[SerializeField] private Text crystalText;
[SerializeField] private LayerMask ground; //collider
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "collectable")
{
Destroy(collision.gameObject);
crystals+=1;
crystalText.text = crystals.ToString();
}
}
private void Start()
//Engines used
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
coll = GetComponent<Collider2D>();
}
private void Update()
//Movement
{
float hDirection = Input.GetAxis("Horizontal");
//Left
if (hDirection < 0)
{
rb.AddForce(new Vector2(-speed,0), ForceMode2D.Impulse);
gameObject.GetComponent<SpriteRenderer>().flipX = true;
}
//Right
else if (hDirection > 0)
{
rb.AddForce(new Vector2(speed,0), ForceMode2D.Impulse);
gameObject.GetComponent<SpriteRenderer>().flipX = false;
}
//Jumping
if(Input.GetButtonDown("Jump") && coll.IsTouchingLayers(ground))
{
rb.AddForce(new Vector2(0,jumpForce), ForceMode2D.Impulse);
state = State.jumping;
}
VelocityState();
anim.SetInteger("state", (int)state);
}
}