Hi everybody !
I have a problem with my character controller and colliders :
My player move perfectly but when i try to pass throught a wall (with a collider on my player and on the wall) my player go on the wall and go on a (random ?) tile around the wall.
This is my script :
Direction currentDirection;
Vector2 input;
bool isMoving = false;
Vector3 startPos;
Vector3 endPos;
float t;
public bool isAllowedToMove;
public float walkspeed = 3f;
public Sprite northSprite;
public Sprite eastSprite;
public Sprite southSprite;
public Sprite westSprite;
void Start()
{
isAllowedToMove = true;
}
void Update()
{
if (!isMoving && isAllowedToMove)
{
input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
input.y = 0;
else
input.x = 0;
if (input != Vector2.zero)
{
if (input.x < 0)
{
currentDirection = Direction.Ouest;
}
if (input.x > 0)
{
currentDirection = Direction.Est;
}
if (input.y < 0)
{
currentDirection = Direction.Sud;
}
if (input.y > 0)
{
currentDirection = Direction.Nord;
}
switch (currentDirection)
{
case Direction.Nord:
gameObject.GetComponent<SpriteRenderer>().sprite = northSprite;
break;
case Direction.Est:
gameObject.GetComponent<SpriteRenderer>().sprite = eastSprite;
break;
case Direction.Sud:
gameObject.GetComponent<SpriteRenderer>().sprite = southSprite;
break;
case Direction.Ouest:
gameObject.GetComponent<SpriteRenderer>().sprite = westSprite;
break;
}
StartCoroutine(Move(transform));
}
}
}
public IEnumerator Move(Transform entity)
{
isMoving = true;
startPos = entity.position;
t = 0;
endPos = new Vector3(startPos.x + System.Math.Sign(input.x), startPos.y + System.Math.Sign(input.y), startPos.z);
while (t < 1f)
{
t += Time.deltaTime * walkspeed;
entity.position = Vector3.Lerp(startPos, endPos, t);
yield return null;
}
isMoving = false;
yield return 0;
}
enum Direction
{
Nord,
Est,
Sud,
Ouest
}
Thank you,
Bye, xyHeat