Object is not moving after collision

I have player and walls. When the player hits the wall, its not moving.

Those are codes for moving :thinking:

void MoveTileBased()
 {
     transform.position = Vector3.MoveTowards(transform.position, destination, Time.deltaTime * speed);
     
 
 
 
 
 
 
 
 
 
     if (Input.GetKeyDown(KeyCode.W))
     {
         nextPos = Vector3.forward;
     }
 
     if (Input.GetKeyDown(KeyCode.S))
     {
         nextPos = Vector3.back;
     }
 
     if (Input.GetKeyDown(KeyCode.A))
     {
         nextPos = Vector3.left;
     }
 
     if (Input.GetKeyDown(KeyCode.D))
     {
         nextPos = Vector3.right;
     }
 
 
     if(Vector3.Distance(destination, transform.position) < 0.01f)
     {
         destination = transform.position + nextPos;
     }
 }

This is collision script :

private void OnCollisionEnter(Collision collision)
{

    player.speed = 0f;

}

Player rigidbody and collider

1 Like

When the player collides the speed is set to 0 (OnCollisionEnter block of code), that’s what causing your issue, if you set the speed to 0 the cube won’t move at all because 0 times anything will be 0.

Not sure what you are trying to achieve here. In which part of the course are you?

Actually, I understood what the problem is but I couldn’t find a way to get around it. I finished Unity 2D and 3D courses and started to a new project on my own. I’m trying to make this collision event because when the cube hits wall, cube is starting to vibrate. Is there any other way to solve this ?

Congratulations on starting your own project, be sure to share it with the community.

Setting the position of an object in transform.position or transform.Translate will make objects jitter because these functions ignore physics, they are basically teleporting instead of moving.

To solve this you’ll have to modify the speed of the rigidbody instead, depending on how you want your object to react, you might want to use Addforce, MovePosition, or even modify the velocity directly.

Actually I tried to use Physics system to move player. But player should move grid to grid continously until hit any edge and never stop between grids even if player take command to move another direction. When I try to move player with Physics system I wasn’t be able to move player from grid to grid

I created a clone of your project, I’m using rigidbodies, and as you can see in the GIF below, there’s no jittering and it moves in a grid.

ezgif-7-3ff260a504a7

I did this to your code;

    Rigidbody rb;

    private void Awake()
    {
        rb = GetComponent<Rigidbody>();
    }

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

But I noticed a more obnoxious problem, the player cannot move after hitting a wall because of how your code sets the new destination. If you want the player to be able to move after hitting a wall then you’ll have to modify your condition.

2 Likes

Thanks. It was very useful

Did Yee’s solution fix the problem?


See also:

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms