1
\$\begingroup\$

I have a gameobject that moves tile based(It moves 1 unity unit continously when I press button) But when ıt hits a wall, ıt doesn't move anymore

enter image description here

As you see, When It hits a wall, ıt doesn't move any direction anymore. Actually I found the problem but I couldn't find how to solve. The main problem is when cube hits wall, the distance between nextPosition and transform.position never will be <0.0005f But Idk how to solve this.

Those are code that moves cube :

void MovePlayer()
{
    rb.MovePosition(Vector3.MoveTowards(transform.position, nextPosition, speed * Time.deltaTime));

    if (Input.GetKeyDown(KeyCode.W))
    {
        direction = Vector3.forward;
    }

    if (Input.GetKeyDown(KeyCode.S))
    {
        direction = Vector3.back;
    }

    if (Input.GetKeyDown(KeyCode.A))
    {
        direction = Vector3.left;
    }

    if (Input.GetKeyDown(KeyCode.D))
    {
        direction = Vector3.right;
    }

    if (Vector3.Distance(transform.position, nextPosition) < 0.0005f)
    {
        nextPosition = transform.position + direction;
    }
}

I tried to make with timer but failed

 float timer = 0.25f;
 float elapsed;
  private void Update()
{


    elapsed += Time.deltaTime;
}

 
    if(Vector3.Distance(transform.position, nextPosition) > 0.9f && elapsed > timer)
    {
        nextPosition = transform.position;
    }

I tried to make wtih collision. But this time when I try to make a lateral movement, ıt just moves 1 unit(1 tile) every time I pressed button.

float timer = 0.25f;
float elapsed;
bool elapsedBool;



 private void OnCollisionEnter(Collision collision)
{
    if(collision.gameObject.tag == "Wall")
    {

        elapsedBool = true;
        nextPosition = transform.position;
        direction = Vector3.zero;
       

    }
}


private void OnCollisionExit(Collision collision)
{
    if(collision.gameObject.tag == "Wall")
    {
        elapsedBool = false;
        elapsed = 0f;
    }
}


void ElapsedTime()
{
    if(elapsedBool == true)
    {
        elapsed += Time.deltaTime;
    }
   
}
\$\endgroup\$
5
  • \$\begingroup\$ how you detect collision. with wall. can you share callision detection code \$\endgroup\$ Jul 5, 2021 at 22:14
  • \$\begingroup\$ I didn't use collision in this game. Because it cause more problem and it really effects performance especially for mobile games @BarışCanYılmaz \$\endgroup\$ Jul 5, 2021 at 22:21
  • \$\begingroup\$ how you detect ball hit the wall ? \$\endgroup\$ Jul 5, 2021 at 22:35
  • \$\begingroup\$ Why should I detect if the player hits the wall or not ? Both have box collider and player have rigidbody. It's enough to make player stop when it hits wall \$\endgroup\$ Jul 5, 2021 at 22:50
  • \$\begingroup\$ I wrote new answers , previous one didn't work \$\endgroup\$
    – ShoulO
    Jul 7, 2021 at 15:05

2 Answers 2

1
\$\begingroup\$

Also you can avoid using walls altogether. Just keep track on which tile you are on , and if you reached outer tile, just don't move anymore. Lets say your starting point is bottom, right corner (0,0). so you could write something like this:

public enum MovementDirections{Right,Left,Up,Down};
MovementDirections currentDir;
void MovePlayer()
{
 ...
 //your code..
if (Input.GetKeyDown(KeyCode.A))
{
    direction = Vector3.forward;
    currentDir = MovementDirections.Left;
}
...

Then create and update int horizTile in your if statement:

int horizTile = 0;
if (Vector3.Distance(transform.position, nextPosition) < 0.0005f)
{
    nextPosition = transform.position + direction;
    if(currentDir == MovementDirections.Left){
       horizTile++;
      }
}

Now you can prevent going off-board by altering your keypress if statement:

if (Input.GetKeyDown(KeyCode.A) && horizTile < 11 )
{
    //your code..
}

Now left going button will do nothing if you on leftmost tiles..

Do similar for vertical tiles.

\$\endgroup\$
0
\$\begingroup\$

Seems that the problem that wall stops you arriving to nextPosition so distance to it is always slightly above 0.0005f. Am I correct?

If yes, your nextPosition is not reset even if you press the button, since if statement allows to reset it only if you have arrived. And you haven't. One solution would be to move wall little back.

Another solution would be to add another clause to your if statement - boolean justHitWall.

if (justHitWall || Vector3.Distance(transform.position, nextPosition) < 0.0005f)
{ 
... your code

now this if statement will allow you to reset nextPosition.

To set value to justHitWall you can use something like this:

bool justHitWall = false;
private void OnCollisionExit(Collision collision)
{
  if(collision.gameObject.tag == "Wall")
  {
    justHitWall = true;
  }
}

also you may want to reset it somewhere (to wait for another wall hit). Maybe in your if statement itself:

if (justHitWall || Vector3.Distance(transform.position, nextPosition) < 0.0005f)
{
    // your code..
    justHitWall = false;
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .