after colliding problems

hello everyone.

so i have a pretty basic 2d movement script, which works fairly good but there is a problem after i collide with something; for example:

if i go down and bump into something and stop pressing the button my plane goes up automatically it moves by itself.

so if i collide with something after colliding my rigidbody moves automatically to the opposite side.

heres my code

var keyboardSpeed = 10.0;


function FixedUpdate () {
   
    var keyboardX = Input.GetAxis("Horizontal") * keyboardSpeed * Time.deltaTime;
    var keyboardY = Input.GetAxis("Vertical") * keyboardSpeed * Time.deltaTime;
    
    var newPos = rigidbody.position + Vector3(keyboardX, keyboardY, 0.0);
   
    rigidbody.MovePosition(newPos);
}

my plane has the script a rigidbody and a boxcollider.

i tried to implement another script like

if(Input.GetButtonDown("w")) and changin the position and it is the same result

any ideas why?.

You shouldn’t move the rigidbody if it isn’t marked as kinematic otherwise you get this unwanted behavior.
Try to move the transform instead of the rigidbody like this:

var keyboardSpeed = 10.0;

function FixedUpdate () 
{

var keyboardX = Input.GetAxis("Horizontal") * keyboardSpeed * Time.deltaTime;
var keyboardY = Input.GetAxis("Vertical") * keyboardSpeed * Time.deltaTime;

var newPos = transform.position + Vector3(keyboardX, keyboardY, 0.0);

transform.Translate (newPos);

}

You should handle the collision stuff in the appropriate unity function like OnCollisionEnter() or OnCollisionStay() etc.

Here is unitys example how to move things wiht inputs - have a look at it…

i fixed it. so for anyone having the same problem heres my solution
i removed box collider instead im using the character controller script

plust this script

var speed : float = 6.0;

private var moveDirection : Vector3 = Vector3.zero;

function FixedUpdate() {
var controller : CharacterController = GetComponent(CharacterController);

    moveDirection = Vector3(Input.GetAxis("Horizontal"), 0 ,Input.GetAxis("Vertical") );
    moveDirection = transform.TransformDirection(moveDirection);
    moveDirection *= speed;
   
controller.Move(moveDirection * Time.deltaTime);

}