Charactercontroller weird behaivor

Hi everyone i have problem saving my game, it happen when i added the gravity code
isGrounded = controller.isGrounded;
if (velocity.y < 0f && isGrounded)
{
velocity.y = 0f;
}
velocity.y += gravityValue * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
when i load the saved game it spawn at the correct position that i saved at, then the playerobject snaps to the orginal start position like in starting a new game
if i remove the controller.Move(velocity * Time.deltaTime);
everything works perfect, thanks in advance!

here is the complete code

using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;

public class PlayerController : MonoBehaviour
{
    [SerializeField]
    GameObject head;
    [SerializeField]
    private float playerSpeed = 0.5f;
    private float posx = 0f, posy = 0f, posz = 0f;
    private int str = 0;
    Vector3 lastPos;
    private bool isGrounded;
    private Input input;
    private CharacterController controller;
    private Transform camTrans;
    Vector2 asdf;
    Vector3 velocity;
    private bool escp = false;
    private bool leftClick = false;
    [SerializeField]
    private float gravityValue = -9.81f;
    void Awake()
    {
        Cursor.lockState = CursorLockMode.Locked;
        awakeActions();
        camTrans = Camera.main.transform;
    }
    void FixedUpdate()
    {
        updateActions();
        movementPhysics();
        pgravity();
    }
    private void pgravity()
    {
        isGrounded = controller.isGrounded;
         if (velocity.y < 0f && isGrounded)
        {
            velocity.y = 0f;
        }
        velocity.y += gravityValue * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
        Debug.Log("v.y= " + velocity  + " grounded=" + isGrounded);
    }
    private void movementPhysics()
    {
        //if moving forward/backward
        if (asdf.y != 0f)
        {
            //Rotate the player
            transform.rotation = Quaternion.Euler(0f, Camera.main.transform.eulerAngles.y, 0f);

            Vector3 directi = new Vector3(asdf.x, 0f, asdf.y);
            directi = camTrans.forward * directi.z + camTrans.right * directi.x;
            directi.y = 0f;
            controller.Move(directi * playerSpeed * Time.fixedDeltaTime);
        }
        //if moving right/left
        else if (asdf.y == 0f && asdf.x != 0f)
        {
            //Rotate the player
            transform.rotation = Quaternion.Euler(0f, Camera.main.transform.eulerAngles.y, 0f);

            Vector3 directi = new Vector3(asdf.x, 0f, asdf.y);
            directi = camTrans.forward * directi.z + camTrans.right * directi.x;
            directi.y = 0f;
            controller.Move(directi * playerSpeed * Time.fixedDeltaTime);

        }
        //if not moving ( move head with mouse)
        else if (asdf == Vector2.zero)
        {
            head.transform.rotation = Quaternion.Euler(Camera.main.transform.eulerAngles.x, Camera.main.transform.eulerAngles.y, 0f);
        }
     /*   if (transform.position.y > 26.57f)
        {
            transform.Translate(Vector3.down * 3f * Time.deltaTime, Space.World);
        }*/

    }
    private void updateActions()
    {     
        //#GameManagerData
        GameManager.instance.activeScene = SceneManager.GetActiveScene().buildIndex;
        GameManager.instance.posx = transform.position.x;
        GameManager.instance.posy = transform.position.y;
        GameManager.instance.posz = transform.position.z;

        //if ESCP pressed call menu scene
        if (escp)
        {
            Time.timeScale = 0f;
            Cursor.lockState = CursorLockMode.Confined;
            SceneManager.LoadScene("Menu");
        }
    }

    private void awakeActions()
    {
        lastPos = new Vector3(GameManager.instance.posx, GameManager.instance.posy, GameManager.instance.posz);
        transform.position = lastPos;
        input = new Input();
        // cam = Camera.main.transform;
        controller = GetComponent<CharacterController>();
        input.pInput.move.performed += cntx => asdf = cntx.ReadValue<Vector2>();
        input.pInput.move.canceled += cntx => asdf = Vector2.zero;
        input.pInput.escp.performed += cntx => escp = true;
        input.pInput.escp.canceled += cntx => escp = false;
        input.pInput.leftClick.performed += cntx => leftClick = true;
        input.pInput.leftClick.canceled += cntx => leftClick = false;
        //spawn player from saved data
       /* posx = GameManager.instance.posx;
        posy = GameManager.instance.posy;
        posz = GameManager.instance.posz;*/
        //transform.position = new Vector3(posx, posy, posz);
    }
    public void OnEnable()
    {
        input.Enable();
    }

    public void OnDisable()
    {
        input.Disable();
    }
}

sofar i figured out something with the fixedupdate method, putting the pgravity(); function in a update function instead of fixedupdate seems to work but it the falling is much slower

It’s your character controller. It has ots own memory of your characters position. So it will overwrite your teleportation when you load the position.

Workaround is:

  • Disable Character controller
  • Set position
  • Re-enable CC
1 Like

ill try that tomorow , thanks for the advice!!

it worked thanks again