Bugged Player Tp from position one to another
I try to make to make a rhythm game with a platform switch mechanic So I made a code to teleport the player from one position to another and rotate him and the camera And now I get a weird bug that just doesn't allow me to switch positions for like 5 sec each time until I am in a free fall.
If somebody has a clue why is that happening or how can I improve my code I will be thankful :D
Player Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerPlatformSwitch : MonoBehaviour
{
public Vector3 playerRot;
public Vector3 forward;
public Rigidbody rb;
public CameraFollow cf;
public float xPositionTp;
float y = 2;
bool rightChecker;
bool leftChecker;
bool yCheck = false;
void Update()
{
if(Input.GetKeyDown("d") && rb.position.y < 55)
{
rightChecker = true;
}
if (Input.GetKeyDown("a") && rb.position.y < 55)
{
leftChecker = true;
}
}
void FixedUpdate()
{
rb.AddForce(forward * Time.deltaTime);
if (rightChecker)
{
if (yCheck)
{
y = -2;
yCheck = false;
}
transform.Rotate(playerRot);
transform.position = new Vector3(transform.position.x + xPositionTp, transform.position.y + y, transform.position.z + 0);
rightChecker = false;
yCheck = true;
}
if (leftChecker)
{
if (yCheck)
{
y = -2;
yCheck = false;
}
transform.Rotate(-playerRot);
transform.position = new Vector3(transform.position.x + -xPositionTp, transform.position.y + 5, transform.position.z + 0);
leftChecker = false;
yCheck = true;
}
}
}
Camera Code:
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform player;
public Vector3 offset;
void Update()
{
transform.position = player.position + offset;
transform.rotation = player.rotation;
}
}
you said you were not able to teleport untill you were in free fall and in the key inputs you only let the input work if you are lower than 55 unitis height so probably you are higher than 55 units in your game.
Your answer
Follow this Question
Related Questions
transform.position.y updates very slowly 0 Answers
transform.position not applying to my camera. 1 Answer
RogueLike 2D (Tutorial) Rotation of Player 1 Answer
Replicate local movement between objects 1 Answer
How could I make an object move from left to right and it will come back from right? 0 Answers