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;
}
}
}
The following shoul work :
-
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();
rigidbody2D = GetComponent();
jumpsound = GetComponent();
Collider = GetComponent();
}
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
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();