Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Player does not remain at the Respawnpoint position.

Discussion in 'Getting Started' started by Game_Architect, May 6, 2023.

  1. Game_Architect

    Game_Architect

    Joined:
    Feb 13, 2023
    Posts:
    11
    Hello I'm new to unity and my native language is not english. (Portuguese). So forgive me if I misspelled something or didn't understand an answer right away.

    I'm creating a system that should be simple for most developers. The system basically is a simple Player Respawn system. In this system I want that when the Player enters a "saferoom" (a cube or another object with "is Trigger") it saves the Player's position in this room. Throughout the game there will be several "saferooms" each of which subscribes to the Player's position.
    When any monster touches the Player the Player must respawn to the position of the Last "saferoom" he touched/entered.

    take into account that my Player has no HP, any monster that touches him, he "dies" and goes back to respawn.

    I have a system that does that, but when my player respawns at the Respawnpoint which is the "saferoom", if I move a little in the Saferoom it instantly returns to the monster object it touched. Worse is when the monster is active and keeps coming on top of the Player and then the effect is to go to the respawn and return to the position next to the monster multiple consecutive times.

    if anyone wants to analyze my scripts i post them here, there are 3 in all:
    1- Monster
    2- Respawnpoint
    3- RespawnManager
     
  2. TSRajesh

    TSRajesh

    Joined:
    Jun 19, 2013
    Posts:
    68
    Apparently you are moving it to old position in the code somewhere..
    Point me to your scripts.. I'll see if there is anything obvious.
     
  3. Game_Architect

    Game_Architect

    Joined:
    Feb 13, 2023
    Posts:
    11
    Sorry for the delay in replying, I'm new to this forum.
     
  4. Game_Architect

    Game_Architect

    Joined:
    Feb 13, 2023
    Posts:
    11

    All these scripts were made with the help of ChatGPT:
    this is the RespawnManager script that makes the whole system work:


    using UnityEngine;

    public class RespawnManager : MonoBehaviour
    {
    private static RespawnManager _instance;
    private Vector3 _respawnPoint;

    public static RespawnManager Instance
    {
    get { return _instance; }
    }

    private void Awake()
    {
    if (_instance == null)
    {
    _instance = this;
    }
    }

    public void SetRespawnPoint(Vector3 respawnPoint)
    {
    _respawnPoint = respawnPoint;
    }

    public void Respawn(Transform transform)
    {
    transform.position = _respawnPoint;
    }
    }
     
  5. Game_Architect

    Game_Architect

    Joined:
    Feb 13, 2023
    Posts:
    11
    This is the Respawnpoint script (Save Rooms) it is in this script that when the "Player" enters the object it saves its position, and when it enters another save room its position is updated.


    using UnityEngine;

    [RequireComponent(typeof(Collider))]
    public class Respawnpoint : MonoBehaviour
    {
    private enum CoordinateSource
    {
    TransformPosition,
    ColliderPosition,
    Vector3Variable
    }

    [SerializeField] private CoordinateSource _coordinateSource;
    [SerializeField] private Vector3 _respawnCoordinates;

    private void OnTriggerEnter(Collider other)
    {
    if (!other.CompareTag("Player"))
    {
    return;
    }

    switch (_coordinateSource)
    {
    case CoordinateSource.TransformPosition:
    RespawnManager.Instance.SetRespawnPoint(transform.position);
    break;
    case CoordinateSource.ColliderPosition:
    RespawnManager.Instance.SetRespawnPoint(transform.position + GetComponent<Collider>().bounds.center);
    break;
    case CoordinateSource.Vector3Variable:
    RespawnManager.Instance.SetRespawnPoint(_respawnCoordinates);
    break;
    default:
    Debug.LogError("Invalid CoordinateSource selected for Respawnpoint");
    break;
    }
    }
    }
     
  6. Game_Architect

    Game_Architect

    Joined:
    Feb 13, 2023
    Posts:
    11
    And finally: this is the script for any monster in the game. When the monster touches the player it goes back to the last (save Room) that touched/entered.

    using UnityEngine;

    public class Monster : MonoBehaviour
    {
    private void OnTriggerStay(Collider other)
    {
    if (other.gameObject.CompareTag("Player"))
    {
    RespawnManager.Instance.Respawn(other.gameObject.transform);
    }
    }
    }
     
  7. Game_Architect

    Game_Architect

    Joined:
    Feb 13, 2023
    Posts:
    11
    I'm having problems with all kinds of scripts that transform my Player's location instantly, I have a simple teleport script that should work without any problems, but it has the exact same problem with this Respawnpoint system, i.e. the Player teleports to the place it should, but if I move a little bit, it goes back to the buried position. I can't make the A.I. understand what's wrong with the script and she sends me strange solutions that don't solve the problem, so I decided to get human help.
     
  8. TSRajesh

    TSRajesh

    Joined:
    Jun 19, 2013
    Posts:
    68
    Please use "insert code" or "inline code" whenever you post a codelet. It is easier to look at.
    - Are you sure that the _respawnPoint is showing the correct value at the time of your respawn?
    - If I understand, You said that it does teleport properly, but goes back when you "move" it. Just means you are saving / maintaining your position somewhere..
    Do you use a Rigidbody component? Or a "Character Controller" ?
    In Character controller, Do you use "SimpleMove" ? Or just "Move()" ?
    Are you sure you are not calling the move() in more than one place? Like... To move, Jump, gravity etc..
    - Do you use Root motion? Or use code to move your character? While doing so, do you use any variable?

    There definitely is issue in your code if it doesn't work as expected.. Just have to look hard enough to find the bug.
    If this were my code, I know where exactly to look...

    (BTW, Artificial Intelligence is no match for Natural Stupidity).
     
  9. Game_Architect

    Game_Architect

    Joined:
    Feb 13, 2023
    Posts:
    11
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityStandardAssets.CrossPlatformInput;
    4. using UnityStandardAssets.Utility;
    5. using Random = UnityEngine.Random;
    6.  
    7. namespace UnityStandardAssets.Characters.FirstPerson
    8. {
    9.     [RequireComponent(typeof (CharacterController))]
    10.     [RequireComponent(typeof (AudioSource))]
    11.     public class FirstPersonController : MonoBehaviour
    12.     {
    13.         [SerializeField] private bool m_IsWalking;
    14.         [SerializeField] private float m_WalkSpeed;
    15.         [SerializeField] private float m_RunSpeed;
    16.         [SerializeField] [Range(0f, 1f)] private float m_RunstepLenghten;
    17.         [SerializeField] private float m_JumpSpeed;
    18.         [SerializeField] private float m_StickToGroundForce;
    19.         [SerializeField] private float m_GravityMultiplier;
    20.         [SerializeField] private MouseLook m_MouseLook;
    21.         [SerializeField] private bool m_UseFovKick;
    22.         [SerializeField] private FOVKick m_FovKick = new FOVKick();
    23.         [SerializeField] private bool m_UseHeadBob;
    24.         [SerializeField] private CurveControlledBob m_HeadBob = new CurveControlledBob();
    25.         [SerializeField] private LerpControlledBob m_JumpBob = new LerpControlledBob();
    26.         [SerializeField] private float m_StepInterval;
    27.         [SerializeField] private AudioClip[] m_FootstepSounds;    // an array of footstep sounds that will be randomly selected from.
    28.         [SerializeField] private AudioClip m_JumpSound;           // the sound played when character leaves the ground.
    29.         [SerializeField] private AudioClip m_LandSound;           // the sound played when character touches back on ground.
    30.  
    31.         private Camera m_Camera;
    32.         private bool m_Jump;
    33.         private float m_YRotation;
    34.         private Vector2 m_Input;
    35.         private Vector3 m_MoveDir = Vector3.zero;
    36.         private CharacterController m_CharacterController;
    37.         private CollisionFlags m_CollisionFlags;
    38.         private bool m_PreviouslyGrounded;
    39.         private Vector3 m_OriginalCameraPosition;
    40.         private float m_StepCycle;
    41.         private float m_NextStep;
    42.         private bool m_Jumping;
    43.         private AudioSource m_AudioSource;
    44.  
    45.         // Use this for initialization
    46.         private void Start()
    47.         {
    48.             m_CharacterController = GetComponent<CharacterController>();
    49.             m_Camera = Camera.main;
    50.             m_OriginalCameraPosition = m_Camera.transform.localPosition;
    51.             m_FovKick.Setup(m_Camera);
    52.             m_HeadBob.Setup(m_Camera, m_StepInterval);
    53.             m_StepCycle = 0f;
    54.             m_NextStep = m_StepCycle/2f;
    55.             m_Jumping = false;
    56.             m_AudioSource = GetComponent<AudioSource>();
    57.             m_MouseLook.Init(transform , m_Camera.transform);
    58.         }
    59.  
    60.  
    61.         // Update is called once per frame
    62.         private void Update()
    63.         {
    64.             RotateView();
    65.             // the jump state needs to read here to make sure it is not missed
    66.             if (!m_Jump)
    67.             {
    68.                 m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
    69.             }
    70.  
    71.             if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
    72.             {
    73.                 StartCoroutine(m_JumpBob.DoBobCycle());
    74.                 PlayLandingSound();
    75.                 m_MoveDir.y = 0f;
    76.                 m_Jumping = false;
    77.             }
    78.             if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
    79.             {
    80.                 m_MoveDir.y = 0f;
    81.             }
    82.  
    83.             m_PreviouslyGrounded = m_CharacterController.isGrounded;
    84.         }
    85.  
    86.  
    87.         private void PlayLandingSound()
    88.         {
    89.             m_AudioSource.clip = m_LandSound;
    90.             m_AudioSource.Play();
    91.             m_NextStep = m_StepCycle + .5f;
    92.         }
    93.  
    94.  
    95.         private void FixedUpdate()
    96.         {
    97.             float speed;
    98.             GetInput(out speed);
    99.             // always move along the camera forward as it is the direction that it being aimed at
    100.             Vector3 desiredMove = transform.forward*m_Input.y + transform.right*m_Input.x;
    101.  
    102.             // get a normal for the surface that is being touched to move along it
    103.             RaycastHit hitInfo;
    104.             Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo,
    105.                                m_CharacterController.height/2f, Physics.AllLayers, QueryTriggerInteraction.Ignore);
    106.             desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;
    107.  
    108.             m_MoveDir.x = desiredMove.x*speed;
    109.             m_MoveDir.z = desiredMove.z*speed;
    110.  
    111.  
    112.             if (m_CharacterController.isGrounded)
    113.             {
    114.                 m_MoveDir.y = -m_StickToGroundForce;
    115.  
    116.                 if (m_Jump)
    117.                 {
    118.                     m_MoveDir.y = m_JumpSpeed;
    119.                     PlayJumpSound();
    120.                     m_Jump = false;
    121.                     m_Jumping = true;
    122.                 }
    123.             }
    124.             else
    125.             {
    126.                 m_MoveDir += Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime;
    127.             }
    128.             m_CollisionFlags = m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime);
    129.  
    130.             ProgressStepCycle(speed);
    131.             UpdateCameraPosition(speed);
    132.  
    133.             m_MouseLook.UpdateCursorLock();
    134.         }
    135.  
    136.  
    137.         private void PlayJumpSound()
    138.         {
    139.             m_AudioSource.clip = m_JumpSound;
    140.             m_AudioSource.Play();
    141.         }
    142.  
    143.  
    144.         private void ProgressStepCycle(float speed)
    145.         {
    146.             if (m_CharacterController.velocity.sqrMagnitude > 0 && (m_Input.x != 0 || m_Input.y != 0))
    147.             {
    148.                 m_StepCycle += (m_CharacterController.velocity.magnitude + (speed*(m_IsWalking ? 1f : m_RunstepLenghten)))*
    149.                              Time.fixedDeltaTime;
    150.             }
    151.  
    152.             if (!(m_StepCycle > m_NextStep))
    153.             {
    154.                 return;
    155.             }
    156.  
    157.             m_NextStep = m_StepCycle + m_StepInterval;
    158.  
    159.             PlayFootStepAudio();
    160.         }
    161.  
    162.  
    163.         private void PlayFootStepAudio()
    164.         {
    165.             if (!m_CharacterController.isGrounded)
    166.             {
    167.                 return;
    168.             }
    169.             // pick & play a random footstep sound from the array,
    170.             // excluding sound at index 0
    171.             int n = Random.Range(1, m_FootstepSounds.Length);
    172.             m_AudioSource.clip = m_FootstepSounds[n];
    173.             m_AudioSource.PlayOneShot(m_AudioSource.clip);
    174.             // move picked sound to index 0 so it's not picked next time
    175.             m_FootstepSounds[n] = m_FootstepSounds[0];
    176.             m_FootstepSounds[0] = m_AudioSource.clip;
    177.         }
    178.  
    179.  
    180.         private void UpdateCameraPosition(float speed)
    181.         {
    182.             Vector3 newCameraPosition;
    183.             if (!m_UseHeadBob)
    184.             {
    185.                 return;
    186.             }
    187.             if (m_CharacterController.velocity.magnitude > 0 && m_CharacterController.isGrounded)
    188.             {
    189.                 m_Camera.transform.localPosition =
    190.                     m_HeadBob.DoHeadBob(m_CharacterController.velocity.magnitude +
    191.                                       (speed*(m_IsWalking ? 1f : m_RunstepLenghten)));
    192.                 newCameraPosition = m_Camera.transform.localPosition;
    193.                 newCameraPosition.y = m_Camera.transform.localPosition.y - m_JumpBob.Offset();
    194.             }
    195.             else
    196.             {
    197.                 newCameraPosition = m_Camera.transform.localPosition;
    198.                 newCameraPosition.y = m_OriginalCameraPosition.y - m_JumpBob.Offset();
    199.             }
    200.             m_Camera.transform.localPosition = newCameraPosition;
    201.         }
    202.  
    203.  
    204.         private void GetInput(out float speed)
    205.         {
    206.             // Read input
    207.             float horizontal = CrossPlatformInputManager.GetAxis("Horizontal");
    208.             float vertical = CrossPlatformInputManager.GetAxis("Vertical");
    209.  
    210.             bool waswalking = m_IsWalking;
    211.  
    212. #if !MOBILE_INPUT
    213.             // On standalone builds, walk/run speed is modified by a key press.
    214.             // keep track of whether or not the character is walking or running
    215.             m_IsWalking = !Input.GetKey(KeyCode.LeftShift);
    216. #endif
    217.             // set the desired speed to be walking or running
    218.             speed = m_IsWalking ? m_WalkSpeed : m_RunSpeed;
    219.             m_Input = new Vector2(horizontal, vertical);
    220.  
    221.             // normalize input if it exceeds 1 in combined length:
    222.             if (m_Input.sqrMagnitude > 1)
    223.             {
    224.                 m_Input.Normalize();
    225.             }
    226.  
    227.             // handle speed change to give an fov kick
    228.             // only if the player is going to a run, is running and the fovkick is to be used
    229.             if (m_IsWalking != waswalking && m_UseFovKick && m_CharacterController.velocity.sqrMagnitude > 0)
    230.             {
    231.                 StopAllCoroutines();
    232.                 StartCoroutine(!m_IsWalking ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown());
    233.             }
    234.         }
    235.  
    236.  
    237.         private void RotateView()
    238.         {
    239.             m_MouseLook.LookRotation (transform, m_Camera.transform);
    240.         }
    241.  
    242.  
    243.         private void OnControllerColliderHit(ControllerColliderHit hit)
    244.         {
    245.             Rigidbody body = hit.collider.attachedRigidbody;
    246.             //dont move the rigidbody if the character is on top of it
    247.             if (m_CollisionFlags == CollisionFlags.Below)
    248.             {
    249.                 return;
    250.             }
    251.  
    252.             if (body == null || body.isKinematic)
    253.             {
    254.                 return;
    255.             }
    256.             body.AddForceAtPosition(m_CharacterController.velocity*0.1f, hit.point, ForceMode.Impulse);
    257.         }
    258.     }
    259. }
    260.  
     
  10. Game_Architect

    Game_Architect

    Joined:
    Feb 13, 2023
    Posts:
    11
    This is my FirstPersonController script
     
  11. Game_Architect

    Game_Architect

    Joined:
    Feb 13, 2023
    Posts:
    11
    Another A.I. did two simpler codes for me however it has the same problem. Disregard the initial scripts and can you help me with these new ones? I think it will be easier for you to help me. Sorry for the initial confusion, now I understand how to post the codes correctly.
     
  12. Game_Architect

    Game_Architect

    Joined:
    Feb 13, 2023
    Posts:
    11
    This is the updated code for "enemy" this code is attached to any monster that should collide with the Player and send the Player to the last "Save Room"


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Enemy : MonoBehaviour
    4. {
    5.     private void OnTriggerEnter(Collider other)
    6.     {
    7.         if (other.CompareTag("Player"))
    8.         {
    9.             float playerX = PlayerPrefs.GetFloat("PlayerX");
    10.             float playerY = PlayerPrefs.GetFloat("PlayerY");
    11.             float playerZ = PlayerPrefs.GetFloat("PlayerZ");
    12.             other.transform.position = new Vector3(playerX, playerY, playerZ);
    13.         }
    14.     }
    15. }
     
  13. Game_Architect

    Game_Architect

    Joined:
    Feb 13, 2023
    Posts:
    11
    This is the "Save Room" code. If any monster touches the Player (with the Player tag) the Player must go back to the last "Save Room" he entered.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class SaveRoom : MonoBehaviour
    4. {
    5.     private void OnTriggerEnter(Collider other)
    6.     {
    7.         if (other.CompareTag("Player"))
    8.         {
    9.             PlayerPrefs.SetFloat("PlayerX", other.transform.position.x);
    10.             PlayerPrefs.SetFloat("PlayerY", other.transform.position.y);
    11.             PlayerPrefs.SetFloat("PlayerZ", other.transform.position.z);
    12.         }
    13.     }
    14. }
    I did the test and after touching the Monster "enemy script" it goes back to the last "Save Room". However it goes back for milliseconds and returns the position for the object with the "Enemy" script.