i dont know programming language, but i understand how the process works
character can jump/fall with animation, and the animations of moving left and right work when i press a/d, but no movement occurs.
I am getting no compiler errors, so i think a setting is just not working.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement : MonoBehaviour
{
public Rigidbody2D rb;
private Animator anim;
private enum State {idle, running, jumping, falling}
private State state = State.idle;
private Collider2D coll;
[SerializeField] private LayerMask ground;
[SerializeField] private float speed = 10;
[SerializeField] private float jumpForce = 10f;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
coll = GetComponent<Collider2D>();
}
private void Update()
{
float hDirection = Input.GetAxis("Horizontal");
if (hDirection < 0)
{
rb.velocity = new Vector2(-speed, rb.velocity.y);
gameObject.GetComponent<SpriteRenderer>().flipX = true;
}
else if (hDirection > 0)
{
rb.velocity = new Vector2(speed,rb.velocity.y);
gameObject.GetComponent<SpriteRenderer>().flipX = false;
}
else
{
}
if(Input.GetButtonDown("Jump") && coll.IsTouchingLayers(ground))
{
rb.velocity = new Vector2(rb.velocity.x,jumpForce);
state = State.jumping;
}
VelocityState();
anim.SetInteger("state", (int)state);
}
private void VelocityState()
{
if(state == State.jumping)
{
if(rb.velocity.y< .1f)
{
state = State.falling;
}
}
else if(state == State.falling)
{
if(coll.IsTouchingLayers(ground))
{
state = State.idle;
}
}
else if(Mathf.Abs(rb.velocity.x) > 9.5f)
{
state = State.running;
}
else
{
state = State.idle;
}
}
}
My first guess is the mass is too big (or the speed is too low) and the gravity itself is too big and the character cannot move because too much force pushing it down.
Move you character upper than the ground to see it fall (or even remove the ground). And see the character falling and press your A / D key and if I’m right you will see your character moving left / right slowly. So, it will mean the script is working and the settings are not right and too much force is pushing the character down.
You can also play with the speed for example and increase it to 1000.
What happens when you jump and move left / right at the same time?
Also if you do not want friction between the character and ground, you should check the PhysicsMaterial for the BoxCollider starting from here: Unity - Manual: Physics Material 2D. You can achieve a slippery ground with it or lower friction if you wish.
I have increased it to 1000 and nothing happened. i did it for a bit and position didnt change, even in the Transform section for player inspect. If I jump and go left/right, nothing happens
as you see in the image i attached, my mass, gravity, and linear drag are set quite low.
thanks for the help, but i dont know whats happening
Did you changed the Input Manager or any input settings?
Could you take a screenshot what are the Horizontal settings in the Input Manager? (Edit > Project Settings > Input Manager tab).
If the jump is working as you stated, you should try to change this line and following logic
float hDirection = Input.GetAxis("Horizontal");
to a specific character for example:
if (Input.GetKeyDown(KeyCode.D))
{
rb.velocity = new Vector2(-speed, rb.velocity.y);
gameObject.GetComponent<SpriteRenderer>().flipX = true;
}
It just for testing with a direct key instead of the Horizontal axes (using the Horizontal is a good solution, maybe it was misconfigured).