- Home /
How do I make a bridge in game over an existing collision?
I apologize in advance I'm new to this. I'll try my best to explain this well.
I'm making a basic 2D Zelda like game and basically in this case I want to make a projectile fly over water and freeze it as it passes over it making it into a bridge to walk across. I have a Polygon 2D collider set as a trigger for the water. then as the arrow passes over it the arrow detects the trigger's tag as water and places an ice block prefab that is just a sprite and a Box Collider 2D set as a trigger. The idea was that as the player entered the water he would "Drown" but if it detects that there is an ice block he wont and be able to walk just fine. For the most part it worked except randomly it will act like the player drowned.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerMovement : MonoBehaviour
{
public float spd;
private Rigidbody2D playerRigidbody;
private Vector3 change;
private Animator anim;
public Text Debug;
public bool drown;
public bool overWater;
public bool onIce;
public string walkState;
void Start()
{
playerRigidbody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
Debug.text = "Grass";
drown = false;
walkState = "grass";
overWater = false;
}
void Update()
{
change = Vector3.zero;
change.x = Input.GetAxisRaw("Horizontal");
change.y = Input.GetAxisRaw("Vertical");
if (change != Vector3.zero)
{
MoveCharacter();
anim.SetFloat("moveX", change.x);
anim.SetFloat("moveY", change.y);
anim.SetBool("isWalking", true);
}
else
{
anim.SetBool("isWalking", false);
}
Debug.text = "Drown = " + drown + " OverWater = " + overWater;
}
void MoveCharacter()
{
if (!drown)
{
playerRigidbody.MovePosition(transform.position + change.normalized * spd * Time.fixedDeltaTime);
}
}
public void OnTriggerStay2D(Collider2D collision)
{
if (collision.CompareTag("Ice"))
{
walkState = "ice";
}
else if (collision.CompareTag("Water"))
{
if (walkState != "ice")
{
overWater = true;
Debug.text += walkState;
drown = true;
}
}
}
public void OnTriggerExit2D(Collider2D collision)
{
if (collision.CompareTag("Water"))
{
walkState = "Grass";
}
if (collision.CompareTag("Ice"))
{
walkState = "grass";
if (!onIce)
{
walkState = "water";
drown = true;
}
}
}
}
Any suggestions would be appreciated. Cant really think of any other way to do it Thanks.
Does the water still exist "under" the ice? Is it possible that the water and ice colliders may be overlapping, so that the player may sometimes collide with the water through the ice?
Yea the trigger for the water still is under the ice. unfortunately I cant think of a way to not "see" the water and still have it there if you walk off and fall into the water with out having to tediously place a lot of "tiles" per lake.
It works about 90% of the time just for some reason it decides that it doesnt see the ice over the water.
Well the first thing I would try is reorganizing your OnTriggerX logic, it looks a little fishy... try something like this
int touchingIce = 0;
void OnTriggerEnter2D(Collision2D collision){
if(collision.CompareTag("Ice")){
touchingIce++;
}
}
void OnTriggerExit2D(Collision2D collision){
if(collision.CompareTag("Ice")){
touchingIce--;
}
}
void OnTriggerStay2D(Collision2D collision){
if(collision.CompareTag("Water")){
if(touchingIce <= 0) drown = true;
}
}