hi! so I’m new and using online tutorials for my work, I came across this one for opening doors and teleporting into a new room. Interacting With The World - Using Doors [Unity Tutorial] - YouTube . I’ve been trying to add a way to stop player movement during this transition time and re-enable it when it’s over. (i do apologise in advance, my code is quite long )
this is my player movement code ;
{
Animator anim;
//interact
private Vector2 boxSize = new Vector2(0.4f, 2f);
public GameObject interactIcon;
//movement
public float speed;
private float MoveInput;
//jump
public float jumpforce;
public float jump;
private bool isGrounded;
public Transform feetPos;
public float checkRadius;
public LayerMask whatIsGround;
//jump (timer)
private float jumptimecounter;
public float jumptime;
private bool isjumping;
//warping
public bool canMove;
private Rigidbody2D player;
//respawn
private Vector3 respawnPoint; //record position of where they start
public GameObject fallDetector; //link to fall detector in scene
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
respawnPoint = transform.position;
interactIcon.SetActive(false);
canMove = true;
}
// Update is called once per frame
void FixedUpdate()
{
// movement
MoveInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(speed * MoveInput, rb.velocity.y);
if(canMove == false)
{
speed = 0f;
jumpforce = 0f;
}
if(canMove == true)
{
speed = 10f;
jumpforce = 10f;
}
}
void Update()
{
if(Input.GetKeyDown(KeyCode.E))
CheckInteraction();
//check ground
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
if(MoveInput >0)
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
else if(MoveInput < 0)
{
transform.eulerAngles = new Vector3(0, 175, 0);
}
if( isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
isjumping = true;
jumptimecounter = jumptime;
rb.velocity = Vector2.up * jumpforce;
}
if(Input.GetKey(KeyCode.Space) && isjumping == true)
{
if(jumptimecounter > 0){
rb.velocity = Vector2.up * jumpforce;
jumptimecounter -= Time.deltaTime;
} else
{
isjumping = false;
}
}
if(Input.GetKeyUp(KeyCode.Space)) {
isjumping = false;
}
//if player is moving downwards increase velocity
//move fall detector
fallDetector.transform.position = new Vector2(transform.position.x, fallDetector.transform.position.y ); //doesnt move y axis//
}
//called whenever collision is detected
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "FallDetector")
{
transform.position = respawnPoint;
}
}
// INTERACTABLE
public void OpenInteractableIcon()
{
interactIcon.SetActive(true);
}
public void CloseInteractableIcon()
{
interactIcon.SetActive(false);
}
private void CheckInteraction()
{
RaycastHit2D[] hits = Physics2D.BoxCastAll(transform.position, boxSize, 0, Vector2.zero);
//make sure we got something in hits array
if(hits.Length >0)
{
foreach(RaycastHit2D rc in hits)
{
//check if interactable
if(rc.transform.GetComponent<Interactable>())
{
//if it has component, call interact method (opens and closed )
rc.transform.GetComponent<Interactable>().Interact();
//makes sure we dont interact with every object within range
return;
}
}
}
}
}
and here is my scene controller
public Image fader;
private static Scenecontrol instance;
private GameObject player;
private void Awake()
{
if (instance == null)
instance = this;
else
Destroy(gameObject);
}
private void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
}
//when we want to transition player, we cann call this
public static void TransitionPlayer(Vector3 pos)
{
instance.StartCoroutine(instance.Transition(pos));
}
//black screen toggles on and off. player position changes
private IEnumerator Transition(Vector3 pos)
{
fader.gameObject.SetActive(true);
//for is a loop. it repeats a code a fixed number of times.
//the first statement creates a local variavle used to cont through iteration of the loop
//seecond evaluates to a boolean. it is checkes befor each loop. if it is false, loop finishes
//third is what happens after each loop
for(float f = 0; f<1; f+= Time.deltaTime/0.25f)
{
//increase alpha val from 0 to 1
fader.color = new Color(0,0,0,Mathf.Lerp(0,1,f));
yield return null;
}
player.transform.position = pos;
//screen stays back until player moves
yield return new WaitForSeconds(1);
for(float f = 0; f<1; f+= Time.deltaTime/0.25f)
{
//decrease alpha val from 1 to 0
fader.color = new Color(0,0,0,Mathf.Lerp(1,0,f));
yield return null;
}
fader.gameObject.SetActive(false);
}
}
and finally my door script
public GameObject target;
public override void Interact()
{
//use transition player method
Scenecontrol.TransitionPlayer(target.transform.position);
}
I’ve got the “canMove” bool set up on my player, yet no matter where i put it in the scene controller script my game will not play once I click “e” on the door. does anyone have some advice/ help.