I have this character that, when the player presses an arrow key, I want to move in a singular direction until it hits a wall, after which the player can move again. It also leaves a path behind it. This is part of my script.
//Move Character and Stop at Blocks
//Up
IEnumerator MoveUp()
{
Debug.Log("Moved Upwards");
stop = false;
start = true;
collider.size = new Vector2(0.10f, 0.46f);
Instantiate(downPath, new Vector2(gameObject.transform.position.x, gameObject.transform.position.y), Quaternion.identity, storage.transform);
while (stop == false)
{
transform.position = new Vector2(transform.position.x, transform.position.y + 0.32f);
if (start == true)
{
Instantiate(upPath, new Vector2(gameObject.transform.position.x, gameObject.transform.position.y), Quaternion.identity, storage.transform);
}
if (start == false)
{
Instantiate(verticalPath, new Vector2(gameObject.transform.position.x, gameObject.transform.position.y - 0.32f), Quaternion.identity, storage.transform);
}
start = false;
yield return null;
}
moving = false;
collider.size = new Vector2(0.32f, 0.32f);
Instantiate(upPath, new Vector2(gameObject.transform.position.x, gameObject.transform.position.y), Quaternion.identity, storage.transform);
StartCoroutine(checkBlocks());
}
//Down
IEnumerator MoveDown()
{
Debug.Log("Moved Downwards");
stop = false;
start = true;
collider.size = new Vector2(0.10f, 0.46f);
Instantiate(upPath, new Vector2(gameObject.transform.position.x, gameObject.transform.position.y), Quaternion.identity, storage.transform);
while (stop == false)
{
transform.position = new Vector2(transform.position.x, transform.position.y - 0.32f);
if (start == true)
{
Instantiate(downPath, new Vector2(gameObject.transform.position.x, gameObject.transform.position.y), Quaternion.identity, storage.transform);
}
if (start == false)
{
Instantiate(verticalPath, new Vector2(gameObject.transform.position.x, gameObject.transform.position.y + 0.32f), Quaternion.identity, storage.transform);
}
start = false;
yield return null;
}
moving = false;
collider.size = new Vector2(0.32f, 0.32f);
Instantiate(downPath, new Vector2(gameObject.transform.position.x, gameObject.transform.position.y), Quaternion.identity, storage.transform);
StartCoroutine(checkBlocks());
}
//Left
IEnumerator MoveLeft()
{
Debug.Log("Moved Left");
stop = false;
start = true;
collider.size = new Vector2(0.46f, 0.10f);
Instantiate(rightPath, new Vector2(gameObject.transform.position.x, gameObject.transform.position.y), Quaternion.identity, storage.transform);
while (stop == false)
{
transform.position = new Vector2(transform.position.x - 0.32f, transform.position.y);
if (start == true)
{
Instantiate(leftPath, new Vector2(gameObject.transform.position.x, gameObject.transform.position.y), Quaternion.identity, storage.transform);
}
if (start == false)
{
Instantiate(horizontalPath, new Vector2(gameObject.transform.position.x + 0.32f, gameObject.transform.position.y), Quaternion.identity, storage.transform);
}
start = false;
yield return null;
}
moving = false;
collider.size = new Vector2(0.32f, 0.32f);
Instantiate(leftPath, new Vector2(gameObject.transform.position.x, gameObject.transform.position.y), Quaternion.identity, storage.transform);
StartCoroutine(checkBlocks());
}
//Right
IEnumerator MoveRight()
{
Debug.Log("Moved Right");
stop = false;
start = true;
collider.size = new Vector2(0.46f, 0.10f);
Instantiate(leftPath, new Vector2(gameObject.transform.position.x, gameObject.transform.position.y), Quaternion.identity, storage.transform);
while (stop == false)
{
transform.position = new Vector2(transform.position.x + 0.32f, transform.position.y);
if (start == true)
{
Instantiate(rightPath, new Vector2(gameObject.transform.position.x, gameObject.transform.position.y), Quaternion.identity, storage.transform);
}
if (start == false)
{
Instantiate(horizontalPath, new Vector2(gameObject.transform.position.x - 0.32f, gameObject.transform.position.y), Quaternion.identity, storage.transform);
}
start = false;
yield return null;
}
moving = false;
collider.size = new Vector2(0.32f, 0.32f);
Instantiate(rightPath, new Vector2(gameObject.transform.position.x, gameObject.transform.position.y), Quaternion.identity, storage.transform);
StartCoroutine(checkBlocks());
}
//If Enter Block, Stop
private void OnTriggerEnter2D (Collider2D other)
{
stop = true;
}
The problem with this that I noticed is: usually my character moves normally and everything worked as intended. They moved in units of 0.32 and stopped at walls. The problem was that about every 1/10 times the character would phase into a block, and if the move button was pressed again pass through it. Now I am pretty sure this is because I am moving definitively instead of moving with Unity’s physics.
I can’t find any examples of moving in unity without the Input.GetAxis method, and that won’t work in my code because the player will only press the button once. Also, I need my character to always occupy spaces that are multiples of 0.32, and I detect this through colliders. So either I need code that will stop exactly at a block, or that can move my character to a multiple of 0.32 after the movement; all of this with physics based movement.
Can anyone help me? Feel free to ask any questions, and I will provide the full script if needed. I don’t need a fully coded answer (though that would be helpful), just someone to point me in the right direction.
TLDR: Need character to continuously move in one direction through physics, and then occupy a position that is a multiple of 0.32.