Help/ advice in Game clashing

Alright will try to put it. In the script that responsible for moving the player .

I followed it , exactly. Added it in the script controlling the movement but still it failed to save the transform or reload it. Where exactly am I going wrong .

Should call the load function, or the loadlastscene function.

It’s saving to the file and in the location. But when I click load. The player isn’t being moved only a blue dot is showing up, where I last saved and then it disappears

I opened the save file, and it’s showing like it did for sam. When he saved , the binary is the same


The blue dot between the legs, is the only one moving to the save point when loading the save state

This is challenging, since I don’t fully know what your character’s setup is…
Can you post the script that contains your movement code (and should contain the CaptureState/RestoreState?

I will send it

sing System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using GameDevTV.Saving;


public class RelativeMovement : MonoBehaviour,ISaveable
{
    public float walkSpeed = 2f;
    public float runSpeed = 6;
    [SerializeField] private Transform target;
    private Animator anim;
    private CharacterController CharacterController;
    private ControllerColliderHit contact;
    public float rotationSpeed = 15.0f;
    public float jumpSpeed = 15.0f;
    public float gravity = -12f;
    public float terminalVelocity = -13f;
    public float minFall = -1.5f;
   

    public float[] position;


    private float vertSpeed;
    private float speedSmoothVelocity;
    private float speedSmoothTime;
    private float currentSpeed;
    private float velocityY;
    private float jumpHieght = 1f;
   
    private Health health;
    private Transform player;
   

    void Start()
    {
        vertSpeed = minFall;
        CharacterController = GetComponent<CharacterController>();
        anim = GetComponent<Animator>();
        health = GetComponent<Health>();
        player = GameObject.FindWithTag("Player").transform;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        Vector3 movement = Vector3.zero;

        float horInput = Input.GetAxis("Horizontal");
        float vertInput = Input.GetAxis("Vertical");

        if (horInput != 0 || vertInput != 0)
        {
            movement.x = horInput;
            movement.z = vertInput;
            movement = Vector3.ClampMagnitude(movement, walkSpeed);


            Quaternion tmp = target.rotation;
            target.eulerAngles = new Vector3(0, target.eulerAngles.y, 0);
            movement = target.TransformDirection(movement);
            target.rotation = tmp;

            Quaternion direction = Quaternion.LookRotation(movement);
            transform.rotation = Quaternion.Lerp(transform.rotation, direction, rotationSpeed * Time.deltaTime);
            velocityY += Time.deltaTime * gravity;


        }
        bool running = Input.GetKey(KeyCode.LeftShift);
        float targetSpeed = ((running) ? runSpeed : walkSpeed) * movement.magnitude;
        currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);


        float animationSpeedPercent = ((running) ? 1 : .5f) * movement.magnitude;
        anim.SetFloat("Speed", animationSpeedPercent, speedSmoothTime, Time.deltaTime);

        movement = Grounded(movement);
        movement.y = vertSpeed;
        movement *= Time.deltaTime;
        CharacterController.Move(movement);


    }

    private Vector3 Grounded(Vector3 movement)
    {

        #region Raycast Hit commenst
        bool hitGround = false;
        RaycastHit hit;
        if (vertSpeed < 0 && Physics.Raycast(transform.position, Vector3.down, out hit))
        {
            float check = (CharacterController.height + CharacterController.radius) / 1.9f;
            hitGround = hit.distance <= check;
        }
        #endregion

        if (hitGround)
        {
            if (Input.GetButtonDown("Jump"))
            {
                vertSpeed = jumpSpeed;

            }
            else
            {
                vertSpeed = minFall;
                anim.SetBool("Jumping", false);
            }
        }
        else
        {
            vertSpeed += gravity * 5 * Time.deltaTime;
            if (vertSpeed < terminalVelocity)
            {
                vertSpeed = terminalVelocity;
            }

            #region Raycast comments and code
            if (contact != null)
            {
                anim.SetBool("Jumping", true);
            }

            if (CharacterController.isGrounded)
            {
                if (Vector3.Dot(movement, contact.normal) < 0)
                {
                    movement = contact.normal * walkSpeed;
                }
                else
                {
                    movement += contact.normal * walkSpeed;
                }
            }
            //}
            #endregion

        }

        return movement;
    }

    public object CaptureState()
    {
        return new SerializableVector3(player.position);
    }

    public void RestoreState(object state)
    {
        SerializableVector3 position = (SerializableVector3)state;
        player.position = position.ToVector();
    }
}

Is this script on the player’s GameObject? (I see you’re finding a separate player object, but if the script is on the player, then you should be able to use transform.position instead of player.position in capture/restore state). It seem, though, that you’re saying another GameObject is being translated to the new location. Try just using transform.position in Capture/RestoreState

Alright trying it right now

Yes it is

I wanted to say how I solved it, so that you can help someone who might have the same issue in the future .
I was using the character controller, and when I reloaded , it was refusing to take the new position. So I deactivated it and then reactivated it when the ToVector() was called.

Ok, that’s similar to an issue that arises in the course project, since we use the NavMesh. The Agent does not like the character to be moved by anything but it, so one solution is to turn it off and on like that.

1 Like

Yes I watched it and saw that the navmesh was mentioned .

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

Privacy & Terms