- Home /
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;
}
}
}
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;
}
}
}
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
Oh right, that might happen... give me some time I shall change your script to work :)
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
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();
Oh right, that was my mistake...
I changed the code so it should work now.
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.
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 :)