Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by McAvalnch · Oct 13, 2021 at 08:08 PM · collidercollider2dtriggerscollision2dontriggerstay

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bdubbert · Oct 13, 2021 at 09:10 PM 0
Share

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?

avatar image McAvalnch bdubbert · Oct 13, 2021 at 09:16 PM 0
Share

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.

avatar image bdubbert McAvalnch · Oct 13, 2021 at 09:34 PM 1
Share

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;
     }
 }


Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

173 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to set collision for an object with specific collider size? 3 Answers

Player jump only once 1 Answer

Weird ContactPoint2D on two BoxCollider2D collision 0 Answers

HOW TO CALLBACK COLLIDER WITH NAME? 2 Answers

OnCollisionEnter is not working for me!! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges