- Home /
 
               Question by 
               alirezarazavi1386 · Jan 17 at 06:12 PM · 
                unity 2dcollision2dprediction  
              
 
              How do I predict an objects position that might collide with something??
I have this code that predicts the players position. It's not perfect but I'm ok with it, the only problem with it is that the predicted position might go through collisions and I have no idea how to fix that. I tried using Raycasts but since I have multiple objects that the player can collide with it didn't work.
 void ManagePrediction()
 {
     Rigidbody2D playerRB = player.GetComponent<Rigidbody2D>();
     predictedPosition.x = (playerRB.velocity.x * howFarInFuture) + playerRB.position.x;
     
     if(playerRB.velocity.y == 0){
         predictedPosition.y = 0 + player.transform.position.y;
     } else{
         predictedPosition.y = playerRB.velocity.y * howFarInFuture - (playerRB.gravityScale + howFarInFuture) + playerRB.position.y;
     }
     
     GameObject.FindWithTag(predictionMarker.tag).transform.position = predictedPosition;
 }
               Comment
              
 
               
              Your answer
 
 
             Follow this Question
Related Questions
Elastic collisions between cubes and walls 0 Answers
How do I make a projectile go in a straight line after colliding with another projectile? 0 Answers
How can I allow the rope to correctly interact with Box and Polygon colliders? 0 Answers
If gameobject on collision with gameobject then destroy 0 Answers
Unity2D: How to make my player not walk through walls? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                