so, following “this and that” i achieved something close to what i want. But there’s one thing happening which i don’t understand why.
My character is moving around as it should, but when it collides with a wall (empty object with default sprite render and 2dcollider, no changes at all) he “jumps” to “get off” off the wall. If i’m moving close to the wall, for example to the right side, and then change to the left, my character “jumps” away from the wall before really move to the right
*the gravity scale is 0. It’s not falling and it happens with the walls in any side.
public class PlayerController : MonoBehaviour
{
private Vector2 moveDirection;
public MovableCreature player;
private void Start()
{
moveDirection = new Vector2();
}
public void SetPlayer(MovableCreature player)
{
this.player = player;
}
void FixedUpdate()
{
moveDirection.x = Input.GetAxis("Horizontal");
moveDirection.y = Input.GetAxis("Vertical");
if (moveDirection.x != 0 || moveDirection.y != 0)
{
if (moveDirection.x < 0.3f && moveDirection.x > -0.3f) moveDirection.x = 0;
if (moveDirection.y < 0.3f && moveDirection.y > -0.3f) moveDirection.y = 0;
player.MoveTo(moveDirection);
}
}
}
using UnityEngine;
using System.Collections;
public class Movable : MonoBehaviour
{
private float speed = 2000f;
private float runningSpeedMultiplier = 2f;
private float runningSpeed = 1;
protected Vector2 moveDirection = new Vector2();
protected Rigidbody2D creatureRigidbody;
protected bool movementLocked;
protected void Start()
{
creatureRigidbody = GetComponent<Rigidbody2D>();
creatureRigidbody.gravityScale = 0;
//creatureRigidbody.mass = 0;
creatureRigidbody.drag = MovementConfigs.FRICTION;
creatureRigidbody.angularDrag = MovementConfigs.FRICTION;
}
public void setSpeed(float speed)
{
this.speed = speed;
}
public void setrunningSpeedMultiplier(float speed)
{
this.speed = speed;
}
public void SetRunning(bool isRunning)
{
if (isRunning)
{
runningSpeed = runningSpeedMultiplier;
}
else
{
runningSpeed = 1;
}
}
public void MoveTo(Vector2 moveDirection)
{
this.moveDirection = moveDirection;
}
protected void FixedUpdate()
{
if (!movementLocked)
{
moveDirection = moveDirection.normalized;
moveDirection *= speed * runningSpeed;
if (moveDirection.magnitude != 0)
{
creatureRigidbody.AddForce(moveDirection * Time.deltaTime);
creatureRigidbody.MoveRotation(Operations.AngleDegrees(moveDirection));
}
else
{
creatureRigidbody.velocity = Vector2.zero;
}
}
}
}