- Home /
Other
Move player position a bit
Hi!, i'm make a hole in my game, when the player has fallen for this hole, appears in the same position, i want my player appears a bit far of this last position so that it does not fall back through the hole again, how could I do this?, now i have this code, thanks.
Video to understand it better: https://www.youtube.com/watch?v=cq76T0ws47Q
//Falling Hole
private void OnTriggerEnter2D(Collider2D col)
{
if (col.CompareTag("hole"))
{
print("falling");
CallFallingCoCoroutine();
}
}
public void CallFallingCoCoroutine()
{
StartCoroutine(fallingHole());
}
//Falling hole coroutine
float fallSeconds = 0.5f;
float flashHoleDelay = 0.2f;
public IEnumerator fallingHole()
{
Health.Instance.health--;
anim.SetBool("falling", true);
canMove = false;
rb2d.constraints = RigidbodyConstraints2D.FreezePosition;
yield return new WaitForSeconds(fallSeconds);
anim.SetBool("falling", false);
canMove = true;
rb2d.constraints = RigidbodyConstraints2D.None;
yield return new WaitForSeconds(flashHoleDelay);
vulnerability = true;
CallFlashCoCoroutine();
}
Answer by Magso · Jun 04, 2019 at 01:20 PM
You can start the coroutine from OnTriggerEnter2D. I'm gathering that the animation is the fall itself rather than using the rigidbody, in this case you can use transform.Translate(Vector3.back*holeWidth)
just after anim.SetBool("falling", false)
Hi!, i use this but not work, the player not move back.
//Falling Hole
private void OnTriggerEnter2D(Collider2D col)
{
if (col.CompareTag("Player"))
{
print("falling");
CallFallingCoCoroutine();
}
}
//Call falling coroutine
public void CallFallingCoCoroutine()
{
StartCoroutine(fallingHole());
}
//Falling hole coroutine
float fallSeconds = 0.8f;
float flashHoleDelay = 0.2f;
public IEnumerator fallingHole()
{
anim.SetBool("falling", true);
can$$anonymous$$ove = false;
rb2d.constraints = RigidbodyConstraints2D.FreezePosition;
yield return new WaitForSeconds(fallSeconds);
anim.SetBool("falling", false);
transform.Translate(Vector3.back * 2.5f);
can$$anonymous$$ove = true;
rb2d.constraints = RigidbodyConstraints2D.None;
yield return new WaitForSeconds(flashHoleDelay);
if (vulnerability == false)
{
Health.Instance.health--;
CallFlashCoCoroutine();
}
}
Sorry I've just realised you're working in 2D, it should've been Vector2.left or Vector2.right.
Answer by RustyCrow · Jun 04, 2019 at 02:46 PM
Edit: after watching the video OP provided in the comments, the only way i know from the top of my head would be a respawn position. create a empty gameobject in the scene(This will be your respawn position) at the end of the falling code you set
player.transform.position = respawn.transform.position
.
For now you can just make a Public Gameobject RespawnPoint
in the Falling script and drag and drop the point in the inspector. This will snap the player to the empty gameobject position. However if you want to spawn him next to the Hole from the position he went in then i dont have a answer atm sorry.
Hi! Yes, it's a 2D game. I don't need instantiate the player because he not desapear, i have two colliders, the player collider and the hole collider, when the player touch the hole collider, the code above work, but when the code is finished, i want move the player a bit out of hole, I leave a video so you understand it better, thanks :)
https://www.youtube.com/watch?v=cq76T0ws47Q&feature=youtu.be
Thank you for video, i understand now. I will edit the answer but i dont think i can give you the answer you want. Its a little to complex for me to think about on the fly. Edit your question with the video link its helps alot.
Yes, It's a bit complicated, thank you anyway :)
Follow this Question
Related Questions
Player stops moving upon collision 0 Answers
Simple Waypoints Glitch 0 Answers
Player movement 1 Answer