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 Darckret · Mar 18 at 02:29 PM · collider2d gamecrouchingcolission

How do I make sure that my character does not collide with flying obstacles when crouching?

Hello everyone, I have a problem with a box collider, when my character crouches he keeps colliding with the object above him, how can I make this not happen? I saw different options out there but none worked for me, I appreciate your help

This is my code

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Audio;

public class Jugador : MonoBehaviour { public float fuerzaSalto; public GameManager gameManager; public Score ScoreScript; public Timer Timer;

 public Transform groundCheck; // se sabe cuando el jugador estatocando el suelo

 public LayerMask ground;
 public float groundCheckRadius;

 AudioSource jumpsound;
 public AudioSource Death;

 private Rigidbody2D rigidbody2D;
 private Animator animator;
 private bool isGrounded;

 public int playerJumps;
 private int tempPlayerJumps;

 // Start is called before the first frame update
 void Start()
 {
     animator = GetComponent<Animator>();
     rigidbody2D = GetComponent<Rigidbody2D>();
     jumpsound = GetComponent<AudioSource>();
 }

 // Update is called once per frame
 private void FixedUpdate()
 {
     isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, ground);
 }

 void Update()
 {
     if (Input.GetKeyDown(KeyCode.DownArrow))
     {
         animator.SetBool("EstaAgachandose", true);

     }
     if (Input.GetKeyUp(KeyCode.DownArrow)) //This is where you call it instead
     {
         animator.SetBool("EstaAgachandose", false);
     }

         if (isGrounded)
     {
         tempPlayerJumps = playerJumps;
     }

     if (Input.GetKeyDown(KeyCode.Space) && tempPlayerJumps > 0)
     {
         rigidbody2D.velocity = Vector2.up * fuerzaSalto;
         tempPlayerJumps--;
     }
     if (Input.GetKeyDown(KeyCode.Space))
     {
         jumpsound.Play();
         animator.SetBool("estaSaltando", true);
         rigidbody2D.AddForce(new Vector2(0, fuerzaSalto));
     }
 }

 private void OnCollisionEnter2D(Collision2D collision)
 {
     if (collision.gameObject.tag == "Suelo")
     {
         animator.SetBool("estaSaltando", false);
     }

     if (collision.gameObject.tag == "Obstaculo")
     {
         gameManager.gameOver = true;
         ScoreScript.StopScore();
         Death.Play();
     }
     if (collision.gameObject.tag == "Obstaculo")
     {
         gameManager.gameOver = true;
         Debug.Log("gameOver");
         Timer.enabled = false;

     }
 }

}

Comment
Add comment · Show 4
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 Caeser_21 · Mar 18 at 04:49 PM 0
Share

You can use BoxCollider2D.size like :

  public Transform groundCheck; // se sabe cuando el jugador estatocando el suelo
  public LayerMask ground;
  public float groundCheckRadius;
  AudioSource jumpsound;
  public AudioSource Death;
  private Rigidbody2D rigidbody2D;
  private Animator animator;
  private bool isGrounded;
  public int playerJumps;
  private int tempPlayerJumps;
  private BoxCollider2D collider;
  public float BoxColMin; //This can be any value between 1 and the original 'y' value of the BoxCollider2D
  
  void Start()
  {
      animator = GetComponent<Animator>();
      rigidbody2D = GetComponent<Rigidbody2D>();
      jumpsound = GetComponent<AudioSource>();
      collider = GetComponent<BoxCollider2D>();
  }
  
  private void FixedUpdate()
  {
      isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, ground);
  }
  void Update()
  {
      if (Input.GetKeyDown(KeyCode.DownArrow))
      {
          animator.SetBool("EstaAgachandose", true);
          collider.size = new Vector2(collider.size.x, collider.size.y - BoxColMin)
      }
 
      if (Input.GetKeyUp(KeyCode.DownArrow)) //This is where you call it instead
      {
          animator.SetBool("EstaAgachandose", false);
          collider.size = new Vector2(collider.size.x, collider.size.y + BoxColMin)
      }
          if (isGrounded)
      {
          tempPlayerJumps = playerJumps;
      }
      if (Input.GetKeyDown(KeyCode.Space) && tempPlayerJumps > 0)
      {
          rigidbody2D.velocity = Vector2.up * fuerzaSalto;
          tempPlayerJumps--;
      }
      if (Input.GetKeyDown(KeyCode.Space))
      {
          jumpsound.Play();
          animator.SetBool("estaSaltando", true);
          rigidbody2D.AddForce(new Vector2(0, fuerzaSalto));
      }
  }
  private void OnCollisionEnter2D(Collision2D collision)
  {
      if (collision.gameObject.tag == "Suelo")
      {
          animator.SetBool("estaSaltando", false);
      }
      if (collision.gameObject.tag == "Obstaculo")
      {
          gameManager.gameOver = true;
          ScoreScript.StopScore();
          Death.Play();
      }
      if (collision.gameObject.tag == "Obstaculo")
      {
          gameManager.gameOver = true;
          Debug.Log("gameOver");
          Timer.enabled = false;
      }
  }
 }

avatar image Darckret Caeser_21 · Mar 18 at 07:39 PM 0
Share

Hello friend, again thanks for the help but now something curious happens the character is as if he fell a few centimeters from the floor

avatar image Caeser_21 Darckret · Mar 19 at 05:14 AM 0
Share

Oh right, that might happen... give me some time I shall change your script to work :)

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Caeser_21 · Mar 20 at 08:03 AM

The following shoul work :
1. Change the tag of all the Flying Objects to "FlyingObj", Then add the following script

  public Transform groundCheck; // se sabe cuando el jugador estatocando el suelo
  public LayerMask ground;
  public float groundCheckRadius;
  AudioSource jumpsound;
  public AudioSource Death;
  private Rigidbody2D rigidbody2D;
  private Animator animator;
  private bool isGrounded;
  public int playerJumps;
  private int tempPlayerJumps;
  private BoxCollider2D Collider;
  private GameObject[] FlyingObjects;
  
  void Start()
  {
      animator = GetComponent<Animator>();
      rigidbody2D = GetComponent<Rigidbody2D>();
      jumpsound = GetComponent<AudioSource>();
      Collider = GetComponent<BoxCollider2D>();
  }
 
  private void FixedUpdate()
  {
      isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, ground);
  }
  void Update()
  {
      FlyingObjects = GameObject.FindGameObjectsWithTag("FlyingObj");
 
      if (Input.GetKeyDown(KeyCode.DownArrow))
      {
          animator.SetBool("EstaAgachandose", true);
          foreach (GameObject Obj in FlyingObjects)
          {
               Physics2D.IgnoreCollision(Obj.GetComponent<BoxCollider2D>() , Collider, true);
          }
      }
      if (Input.GetKeyUp(KeyCode.DownArrow)) //This is where you call it instead
      {
          animator.SetBool("EstaAgachandose", false);
          foreach (GameObject Obj in FlyingObjects)
          {
               Physics2D.IgnoreCollision(Obj.GetComponent<BoxCollider2D>() , Collider, true);
          }
      }
          if (isGrounded)
      {
          tempPlayerJumps = playerJumps;
      }
      if (Input.GetKeyDown(KeyCode.Space) && tempPlayerJumps > 0)
      {
          rigidbody2D.velocity = Vector2.up * fuerzaSalto;
          tempPlayerJumps--;
      }
      if (Input.GetKeyDown(KeyCode.Space))
      {
          jumpsound.Play();
          animator.SetBool("estaSaltando", true);
          rigidbody2D.AddForce(new Vector2(0, fuerzaSalto));
      }
  }
  private void OnCollisionEnter2D(Collision2D collision)
  {
      if (collision.gameObject.tag == "Suelo")
      {
          animator.SetBool("estaSaltando", false);
      }
      if (collision.gameObject.tag == "Obstaculo")
      {
          gameManager.gameOver = true;
          ScoreScript.StopScore();
          Death.Play();
      }
      if (collision.gameObject.tag == "Obstaculo")
      {
          gameManager.gameOver = true;
          Debug.Log("gameOver");
          Timer.enabled = false;
      }
      if (collision.gameObject.tag == "FlyingObj")
      {
          gameManager.gameOver = true;
          Debug.Log("gameOver");
          Timer.enabled = false;
      }
  }
 }

NOTE : This is not the most performance-efficient way, but it should work

Comment
Add comment · Share
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
0

Answer by Darckret · Mar 20 at 02:33 PM

Hello my friend, I have the following problem, this appears when I place the code, I already put the label and the code as you told me and this comes out

Severity Code Description Project File Line Suppression State Error CS1061 'GameObject[]' does not contain a definition for 'GetComponent' and no accessible extension method 'GetComponent' accepting a first argument of type 'GameObject[]' could be found (are you missing a using directive or an assembly reference?)

referencing this part of the code FlyingCollider = GameObject.FindGameObjectsWithTag("FlyingObj").GetComponent();

Comment
Add comment · Show 28 · Share
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 Caeser_21 · Mar 20 at 02:39 PM 0
Share

Oh right, that was my mistake...
I changed the code so it should work now.

avatar image Darckret Caeser_21 · Mar 20 at 03:06 PM 0
Share

Thanks again for your help my friend, it worked perfectly I would like to pay you for your help you are the only one who takes the trouble to answer and help me with such patience and I am very grateful if you could give me your email or a means to contact you I want to give you something for you help.

avatar image Caeser_21 Darckret · Mar 20 at 03:37 PM 0
Share

No problem mate... I think is is against the rules to share emails and private info (I'm not exactly sure), but I don't need anything in return. The point of this site is to get help from others when you're stuck, and that's what I am doing now and will do in the future :)

Show more comments

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

219 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 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

2D:I have a movementController script on my character but it doesn't move forward 0 Answers

Trigger2D not working 0 Answers

platform 2d about collider 3 Answers

How to get GameObject with its tag when ignoring Collision? 2 Answers

Creating 'redstone' like wires 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