#Collider
1 messages Β· Page 1 of 1 (latest)
Can you show me the code?
@viscid raft
Opened a thread, so we have the stuff here, just fyi
Okie dokie thanks
So that script is on the portal?
Yes
And I'd need the code of it to help you, not sure why you reference the colliders
public class PortalTeleport : MonoBehaviour
{
public GameObject objectCollider;
public GameObject anotherCollider;
private IEnumerator Teleportation()
{
SceneManager.LoadScene("Portal");
yield return new WaitForSeconds(2);
SceneManager.LoadScene("Level2");
}
private void OnTriggerEnter(Collider other)
{
if (other.transform.tag == "Player")
{
StartCoroutine(Teleportation());
}
}
}
Is the players gameobject's tag set to "Player"?
As in the portal?
Can you screenshot both colliders on player and portal?
This is portal
Still nothing
And the other collider screenshot?
Player
first one is portal, second one is player?
Yes
Ok, can you show me the updated code?
public class PortalTeleport : MonoBehaviour
{
public GameObject objectCollider;
public GameObject anotherCollider;
private IEnumerator Teleportation()
{
SceneManager.LoadScene("Portal");
yield return new WaitForSeconds(2);
SceneManager.LoadScene("Level2");
}
private void OnTriggerEnter2D(Collider other)
{
if (other.transform.tag == "Player")
{
StartCoroutine(Teleportation());
}
}
}
private void OnTriggerEnter2D(Collider other)
to
private void OnTriggerEnter2D(Collider2D other)
Okay so it loads my portal scene ... but doesnt load the next one
I guess because you destroy the portal by loading the new scene
So should I add a void Start() on the portal scene to load next scene after 2 seconds?
You can do DontDestroyOnLoad(gameObject); on the portal in the Start function, and then do Destroy(gameobject) after loading the second scene
This will prevent a scene load from destroying the portal
Might not be the most elegant solution but it should work
public class PortalTeleport : MonoBehaviour
{
public GameObject objectCollider;
public GameObject anotherCollider;
private IEnumerator Teleportation()
{
SceneManager.LoadScene("Portal");
yield return new WaitForSeconds(2);
SceneManager.LoadScene("Level2");
Destroy(gameObject);
}
private void OnTriggerEnter2D(Collider other)
{
if (other.transform.tag == "Player")
{
StartCoroutine(Teleportation());
}
}
private void Start()
{
DontDestroyOnLoad(gameObject);
}
}
Like this?
Yes
But your solution is also a nice idea, having a script run in the Portal scene to switch scenes again
What you can also do if it gets more complex than this (Having lots of worlds with lots of teleports), have a "Teleport Manager"
Basically an empty gameobject with a script on it that is set to "DontDestroyOnLoad", whenever you want to teleport you just tell that script to trigger the teleport
Sorry if thats a bit complicated, just want to nudge you into the right direction from the start π
Makes sense, thanks!
If you should follow that in the future your next problem will be finding that script from anywhere, you have two solutions (Prefacing this, because it WILL be a problem then):
- Singleton on the teleport manager
- GameObject.Find("NAME")
Ah this works perfectly. Now to find a good teleportation animation - the rick and morty one looks so out of place haha
Yeah thats up to you now, I am terrible when it comes to the creative part π
Though i suggest looking at the free assets in the asset store, some gems in there sometimes
Wow, that looks way better than anything I had in my earlier gamedev stages π
Just a suggestion, if you want to show a video, dont load a new scene for it, use UI that is hidden, and show it once you trigger a teleport, then switch scenes
Scene switches should be as low as possible, that will just set you up for problems π
Do you know of a bug that would cause my space bar to jump to only work like 1 fifth of the time @severe flame ??
Can you Show me the Code of it?
Where does the grounded come from? Check if its correctly Set whenever you want to jump
Im honestly not sure, I found the script on unity
Then output the grounded variable whenever you press the spacebar and check it with that
So use a Debug.Log and print the grounded when space is pressed?
yes
That could be the issue
It outputs
"False"
and then gives me an error saying that the parameter grounded does not exist
and velocityX does not exist
@severe flame ```cs
public class PlayerPlatformerController : PhysicsObject
{
public float moveSpeed;
public float jumpHeight;
Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
}
if (Input.GetKey(KeyCode.D))
{
rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
}
if (Input.GetKey(KeyCode.A))
{
rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
}
}
}
Works perfectly apart from when you jump, it doesnt STOP jumping
And doesnt stop walking either
I made one change to the code to make it so it does actually stop walking when you release
but it still doesnt stop jumping
@severe flame
If you just set the speed and nothing brakes it, it will never stop π
How do I make it stop jumping?
Do you have gravity enabled?