- Home /
Problem with 2D movement
Good night,
Before I start, I'm sorry for my English.
I have problems with a movement script for a 2D game. In general the script works fine, but sometimes the player stops walking as if colliding with something in front of it (although he passed the place without problems before).
The scripts
Player:
void Update()
{
Vector2 directions = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
movement_control.movement(directions, isGrounded());
}
bool isGrounded()
{
Vector2 position = transform.position;
Vector2 direction = Vector2.down;
float distance = collider.size[1]/2 + 0.1f;
RaycastHit2D hit = Physics2D.Raycast(position, direction, distance, groundLayer);
return hit.collider != null;
}
Movement:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public bool bloq_movement;
public bool is_grounded;
public int current_direction_x;
public int current_direction_y;
public float direction_x;
public float direction_y;
public float jump_force;
public float vertical_speed;
public float horizontal_speed;
public float vertical_max_speed;
public float horizontal_max_speed;
public float vertical_acceleration;
public float horizontal_acceleration;
public float vertical_bonus;
public float horizontal_bonus;
public float gravity;
public float friction;
public bool movement(Vector2 direction, bool is_grounded)
{
Rigidbody2D rigidbody = GetComponent<Rigidbody2D>();
setDirectionX(direction[0]);
setDirectionY(direction[1]);
this.is_grounded = is_grounded;
if (rigidbody == null) {
return false;
}
Vector2 next_point = getMovement();
rigidbody.velocity = next_point;
return true;
}
public Vector2 getMovement()
{
if (direction_x == 0)
{
addHorizontalSpeed(friction * -current_direction_x);
} else
{
addHorizontalSpeed(horizontal_acceleration * current_direction_x);
}
if (!is_grounded)
{
addVerticalSpeed(-gravity);
}
else if (direction_y == 1 && is_grounded)
{
addVerticalSpeed(jump_force);
} else
{
vertical_speed = 0;
}
return new Vector2(horizontal_speed + horizontal_bonus, vertical_speed + vertical_bonus);
}
public void setDirectionX(float direction)
{
if (direction != 0) {
direction_x = direction > 0 ? 1 : -1;
current_direction_x = direction > 0 ? 1 : -1;
}
else
{
direction_x = 0;
}
}
public void setDirectionY(float direction)
{
if (direction != 0) {
direction_y = direction > 0 ? 1 : -1;
current_direction_y = direction > 0 ? 1 : -1;
}
else
{
direction_y = 0;
}
}
public void addHorizontalSpeed(float speed)
{
if ((Math.Abs(horizontal_speed) + Math.Abs(speed)) >= horizontal_acceleration)
{
horizontal_speed += speed;
}
else
{
horizontal_speed = 0;
}
horizontal_speed = horizontal_speed > 0 ? Math.Min(horizontal_speed, horizontal_max_speed) : Math.Max(horizontal_speed, -horizontal_max_speed);
}
public void addVerticalSpeed(float speed)
{
vertical_speed += speed;
vertical_speed = vertical_speed > 0 ? Math.Min(vertical_speed, vertical_max_speed) : Math.Max(vertical_speed, -vertical_max_speed);
}
}
Components:
An example of the error, the player (green square) sometimes stops in the middle of the platform:
Thanks!
I'm not sure what could cause this, but your $$anonymous$$/max lines could be simplified with the $$anonymous$$athf.Clamp function, or the Vector2 function Clamp$$anonymous$$agnitude
Your answer
Follow this Question
Related Questions
Diagonal collision script not working (Unity + Orthello, C#) 1 Answer
Glitchy collisions 0 Answers
My player goes straight through my colider 3 Answers
Platformer crates(boxes) collision bug 1 Answer
How to Apply Animations to Script 1 Answer