- Home /
i have an issue about getting to next level,i have a question about getting to new level
so i want my player sprite getting to next level by avoiding 5 colliders. so how i code it? im new here, pls help me :(
heres my code
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class player : MonoBehaviour { public float moveSpeed = 600f;
float movement = 0f;
// Update is called once per frame
void Update()
{
movement = Input.GetAxisRaw("Horizontal");
}
private void FixedUpdate()
{
transform.RotateAround(Vector3.zero, Vector3.forward, movement * Time.fixedDeltaTime * -moveSpeed); // untuk membuat hexagon mengerucut seiring berjalannya waktu
}
private void OnTriggerEnter2D(Collider2D collision)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
},so i have this player sprite, i want to make this player survive the collider 5 time so it can continue to next level(new scene) heres the code
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class player : MonoBehaviour { public float moveSpeed = 600f;
float movement = 0f;
// Update is called once per frame
void Update()
{
movement = Input.GetAxisRaw("Horizontal");
}
private void FixedUpdate()
{
transform.RotateAround(Vector3.zero, Vector3.forward, movement * Time.fixedDeltaTime * -moveSpeed); // untuk membuat hexagon mengerucut seiring berjalannya waktu
}
private void OnTriggerEnter2D(Collider2D collision)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
Answer by MiB775 · Jun 22, 2020 at 08:47 PM
What do you mean by "avoiding 5 colliders"? Do they move towards the player? Let's assume you can check if the player avoided the collider with function "bool avoided()". What you do is:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class player : MonoBehaviour
{
int collidersAvoided = 0;
void Update()
{
if(avoided())
collidersAvoided++;
}
private void OnTriggerEnter2D(Collider2D collision)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
// If you just want to reset the score to zero, use:
// collidersAvoided = 0;
// It's faster than loading an entire scene
}
}
But then, I don't know what do you mean by "avoiding colliders", so I don't know how to check it.
there will be lots of hexagons which works as the obstacle of this game, so the player should avoid the hexagon in order to survive the game. if the player collides with the hexagon, the game resets. so the hexagon is the collider.
can u help me more?
here is the link to the screenshot of my game, because i have an error while uploading the picture here
Answer by haizathaneefa · Jun 23, 2020 at 06:22 AM
have a script up for your colliders and have OnTriggerEnter
function in it that checks if the ball is colliding with the colliders or not. Put a collider on your hole and give a OnTriggerEnter
function that checks if the ball goes in, load the next level.
welll, make sense but i still didnt get it, can u translate it to code?
Your answer
Follow this Question
Related Questions
next level 1 Answer
How do I make the screen fade when it goes into the next level? 0 Answers
Load Level on Collision 1 Answer
How to script a death fall and respawn? 0 Answers
collision script to next scene 2 Answers