Help, my avatar keeps moving, gravity and sensitivity settings don't seem to help

After succesfully implementing grid based movement (1 continuous press=1step), I added a code for teleporting (colliding with teleport object & pressing Enter=teleport)

After teleporting (releasing enter & arrow keys), my character keeps moving continuously.

I have tried increasing the gravity and sensitivity to 1000 but it still happens.
I think it might be overloading transform.position but I’m not sure, help…

code for moving:

//using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Threading;
//using System.Collections.Specialized;
using UnityEngine;

public class Movement : MonoBehaviour {
    //so first we want to create directions in 3d and our current direction= up, down etc.
    Vector3 up = Vector3.zero,
        right = new Vector3(0,90,0),
        down = new Vector3(0,180,0),
        left = new Vector3(0,270,0),
        currentDirection = Vector3.zero;

    //we only talk to direction based on where we are currently going
    Vector3 nextPos, destination, direction;

    //Movement speed
    float speed = 5f;

    bool canMove;

    //raylenght
    float rayLength = 1f;

    // Start is called before the first frame update
    void Start() {
        //At the very start we initialize up
        currentDirection = up;
        //We're always moving over the forward axis
        nextPos = Vector3.forward;
        //Point where we are currently at: destination doesn't have any new input
        destination = transform.position;

    }

    // Update is called once per frame
    void Update()
    {
        //Start moving
        Move();
    }
  
    //how do we move
    void Move()
    {
        //current pos, where we want to go, how fast?
        transform.position = Vector3.MoveTowards(transform.position, destination, speed * Time.deltaTime);

        //take input and set next position
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            //face up from current direction
            nextPos = Vector3.forward;
            currentDirection = up;
            canMove = true;
        }

        if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            nextPos = Vector3.back;
            currentDirection = down;
            canMove = true;
        }

        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            nextPos = Vector3.right;
            currentDirection = right;
            canMove = true;
        }

        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            nextPos = Vector3.left;
            currentDirection = left;
            canMove = true;
        }

        //so, if the moving is triggered, rotate first
        if (Vector3.Distance(destination, transform.position) <= 0.00001f)
        {
            //rotate yourself: set local current direction to current direction
            transform.localEulerAngles = currentDirection;
        }
        //if can move, move
        if (canMove)
        {
            //valid turn? go to next destination
            if (Valid())
            {
                destination = transform.position + nextPos;
                direction = nextPos;
                //can only hop 1 field per press
                canMove = false;
            }
            destination = transform.position + nextPos;
        }
    }

    //check if next position is valid...
    bool Valid()
    {
        //shoot raycast in direction we are pressing and pudate cantmove function a it
        //the ray goes to position, a little upwards (so you don't get stuck on objects), and forwards
        Ray myRay = new Ray(transform.position + new Vector3(0, 0.25f, 0), transform.forward);
        RaycastHit hit;

        //for debugging purposes: see if the ray is present
        //Debug.DrawRay(myRay.origin, myRay.direction, Color.red);
      
        //physics check
        //shoot a ray, out and hit for a length
        if (Physics.Raycast(myRay,out hit, rayLength))
        {
            //don't move through walls
           if (hit.collider.tag == "Wall")
            {
                return false;
            }
        }
        //check if you hit a wall or not. return true bc you haven't hit anything yet
        return true;

    }
}

code for teleporting:

using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography;
using UnityEngine;

public class PortalTeleporter : MonoBehaviour
{
    public Transform teleportDestination1;
    public Transform teleportDestination2;
    public Transform teleportDestination3;
    public Transform teleportDestination4;

    bool enterPressed = false;

    void OnTriggerEnter(Collider a)
    {
      
        if (a.gameObject.tag == "teleport1")
        {  
            EnterPressed();
            if (enterPressed == true)
            {
                transform.position = teleportDestination1.position;
            }
            enterPressed = false;

        }

        if (a.gameObject.tag == "teleport2")
        {
            EnterPressed();
            if (enterPressed == true)
            {
                transform.position = teleportDestination2.position;

            }
        }

        if (a.gameObject.tag == "teleport3")
        {
            EnterPressed();
            if (enterPressed == true)
            {
                transform.position = teleportDestination3.position;
            }
        }

        if (a.gameObject.tag == "teleport4")
        {
                EnterPressed();
            if (enterPressed == true)
            {
                transform.position = teleportDestination4.position;
            }
        }
    }
    void EnterPressed()
    {
        if (Input.GetKey(KeyCode.Return))
        {
            enterPressed = true;
        }
    }

}

I’m a beginner with unity tbh, so it would help a lot if you could give an example ^-^

I’m gonna guess it is line 53 in the first script… this function is called Move() and it actually calls the Vector3.MoveTowards() function, moving the position towards the destination every frame.

Is that your problem? If so, stop calling that function when you don’t want the player to move towards the destination!

Ok, took me a while, but I think I found it. Your problem is in the if-statement starting in line 95. Any time you call the Move() function, which is every frame, since it is called in Update, this is called. The problem comes from this line:

destination = transform.position + nextPos;

Say you start at position 0. You then set the position to 1 and move by 0.1 in the next frame. Now you are at 0.1 and you reset your destination to 1.1, because canMove and Valid() both return true. You need to ask if the new target is valid when the new target is set, which is when the button is pressed.

I would suggest splitting this into 2 functions. In one function, you catch any input and set a new destination. In the other function, you handle anything that’s related to the movement. That way, these things can’t interfere with each other (as easily).