- Home /
Player does not teleport to the right location
Hello everyone, I'm doing a project for a course i'm taking and i've got into something I cannot resolve it myself. I have 4 GameObjects with the same script "HouseTP" but each have different transforms to teleport the player. When I test the game and go with the player inside the house which has a trigger it fires a coroutine, as the coroutine runs, everything works just fine except the teleportation which puts the player a -1 on the Y from where I tried to go into. I've tried Serializing it but I was not been able to since it has 4 gameobjects linked to it. I've tried an array for the different gameobjects but did not work, my last attempt was using a delegate event (don't know if that worked or if it is was made correctly). Does anyone have a different approach for my problem ? Thanks for your time, here are the scripts:
HouseTP:
using Cinemachine;
using UnityEngine;
namespace RPG.SceneManagement
{
public class HouseTP : MonoBehaviour
{
[SerializeField] private Transform _PosToGo;
[SerializeField] private GameObject _player;
[SerializeField] private CinemachineConfiner2D _CinemachineCameraConfiner;
[SerializeField] private PolygonCollider2D _polygonCollider2D;
[SerializeField] private SceneTransitionShader _sceneTransition;
private void OnEnable()
{
SceneTransitionShader.playerTeleportation += ChangePosition;
}
private void OnDisable()
{
SceneTransitionShader.playerTeleportation -= ChangePosition;
}
public void ChangePosition()
{
_player.transform.position = _PosToGo.position;
_CinemachineCameraConfiner.InvalidateCache();
_CinemachineCameraConfiner.m_BoundingShape2D = _polygonCollider2D;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Player"))
{
StartCoroutine(_sceneTransition.TransitionCoroutine());
}
}
}
}
And the other one:
using System.Collections;
using UnityEngine;
using RPG.SceneManagement;
public class SceneTransitionShader : MonoBehaviour
{
public delegate void PlayerTeleportation();
public static event PlayerTeleportation playerTeleportation;
[SerializeField] private Material _screenTransitionMaterial;
[SerializeField] private PlayerController _playerController;
[SerializeField] private float _transitionTime = 1f;
private float _startValue = 1f;
private float _endValue = 0f;
private const string _propertyName = "_Progress";
public IEnumerator TransitionCoroutine()
{
_playerController.stopPlayer = true;
float currentTime = 0f;
while (currentTime < _transitionTime)
{
currentTime += Time.deltaTime * 2;
float totalTime = currentTime / _transitionTime;
float value = Mathf.Lerp(_startValue, _endValue, totalTime);
_screenTransitionMaterial.SetFloat(_propertyName, value);
yield return null;
}
if (playerTeleportation != null)
playerTeleportation();
yield return new WaitForSecondsRealtime(1f);
StartCoroutine(TransitioningOver());
}
private IEnumerator TransitioningOver()
{
float currentTime = 0f;
while (currentTime < _transitionTime)
{
currentTime += Time.deltaTime * 2;
float totalTime = currentTime / _transitionTime;
float value = Mathf.Lerp(_endValue, _startValue, totalTime);
_screenTransitionMaterial.SetFloat(_propertyName, value);
yield return null;
}
_playerController.stopPlayer = false;
yield return null;
}
}
Your answer
Follow this Question
Related Questions
[2D] Moving the player 1 tile at a time using rigidbody movement 0 Answers
teleport destination differs when player hits trigger from different positions 0 Answers
2D player keeps getting dragged to the left for some reason. 0 Answers
Player movement script for a stickman 1 Answer
Collision for Amount of Time 1 Answer