Physics2D.IgnoreCollision is nor working properly - c#

i faced the problem that after some manipulations with code(Dont remember after what exactly) Physics2D.IgnoreCollision stopped working properly. I'm new to stack overflow, and dont know what else to attach here, if something else is needed to understand the situation, please tell me and i will add. Thanks in advance!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TurnOffGroundCollider : MonoBehaviour
{
[SerializeField] GameObject ground;
[SerializeField] GameObject ball;
private void OnTriggerEnter2D(Collider2D collision)
{
Physics2D.IgnoreCollision(ball.GetComponent<CircleCollider2D>(),
ground.GetComponent<BoxCollider2D>(), true);
}
private void OnTriggerExit2D(Collider2D collision)
{
Physics2D.IgnoreCollision(ball.GetComponent<CircleCollider2D>(),
ground.GetComponent<BoxCollider2D>(), false);
}
}

Related

Enemy instantiate agent not working properly

I have got a problem when im trying to instantiate 2 kind of enemies. i want each of them follow each other and attack. the attack working good but the agent.SetDestination seem a bit buggy. not gaining any errors but when they do spawn they go to some position in the sence.
(if im not instantiate them and they on scene they work good, trying to make it work when spawn as well).
this is the basic spawn script
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class EnemySpawner: MonoBehaviour {
[SerializeField] private GameObject enemy1;
// [SerializeField] private GameObject enemy2;
private float spawnDelay=10f;
private float nextSpawnTime=10f;
void Update() {
if (ShouldSpawn()) {
SpawnEnemy();
}
}
private void SpawnEnemy() {
nextSpawnTime=Time.time+spawnDelay;
Instantiate(enemy1, transform.position, transform.rotation);
//Instantiate(enemy2, transform.position, transform.rotation);
}
private bool ShouldSpawn() {
return Time.time>=nextSpawnTime;
}
}
and this is the base move to the other Target aka other enemy.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyController: MonoBehaviour {
public Transform Target;
private float _speed=0.1f;
private NavMeshAgent _agent;
private void Awake() {
_agent=GetComponent<NavMeshAgent>();
}
void Start() {
StartCoroutine(FollowTarget());
}
private IEnumerator FollowTarget() {
WaitForSeconds wait=new WaitForSeconds(_speed);
while (enabled) {
_agent.SetDestination(Target.transform.position);
yield return wait;
}
}
}

Animation does not trigger

I came to an issue when I try to trigger the event that must turn on the block moving animation, that i set as "move" in animator, it doesn't go to this state, but when I manually trigger it it works completly fine. You can check a screenshot of an animator and ask me to screenshot anything else to solve the problem.
public class BlockScript : MonoBehaviour
{
[SerializeField] private Animator blockanim;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
blockanim.SetTrigger("move");
}
}
}
Well the problem was solved by myself here is what i changed in code and now it works completely fine I didn't notice that I used box collider 2D
using System.Collections.Generic;
using UnityEngine;
public class BlockScript : MonoBehaviour
{
[SerializeField] private Animator blockanim;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
blockanim.SetTrigger("move");
Debug.Log("MOVE");
}
}
}

Unity check if collider is type of spawned prefab

So i want to check if a spawned Block (prefab) collides with my trigger collider but what i wrote doesn't seem to work. Anyone knows how to correctly check if the colliding GameObject is a Block?
Thanks in Advance. :)
Check:
using System;
using GameOver;
using UnityEngine;
public class gameOver : MonoBehaviour
{
[SerializeField] public DeathScreen deathScreen;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.gameObject.GetType() == typeof(Block))
{
deathScreen.Setup();
}
}
}
Block:
using System;
using TMPro;
using UnityEngine;
public class Block : MonoBehaviour
{
private int _hitsRemaining = 5;
private SpriteRenderer _spriteRenderer;
private TextMeshPro text;
private void Awake()
{
_spriteRenderer = GetComponent<SpriteRenderer>();
text = GetComponentInChildren<TextMeshPro>();
UpdateVisualState();
}
private void UpdateVisualState()
{
text.SetText(_hitsRemaining.ToString());
_spriteRenderer.color = Color.Lerp(new Color(0.35f, 1f, 0.67f), new Color(0.04f, 1f, 0.96f), _hitsRemaining / 10f);
}
private void OnCollisionEnter2D(Collision2D collision)
{
_hitsRemaining--;
if (_hitsRemaining > 0)
UpdateVisualState();
else
Destroy(gameObject);
}
internal void SetHits(int hits)
{
_hitsRemaining = hits;
UpdateVisualState();
}
}
Block Prefab:
Collider:
To check if the collided object is an instantiated Block (or any Block really), you check for the existence of the Block component:
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.GetComponent<Block>() != null)
{
deathScreen.Setup();
}
}
Additionally, with one of your collides being a trigger, OnCollisionEnter2D will not be called. Either switch both to non-triggers if you can, or use OnTriggerEnter2D - but then one of your objects has to have a Rigidbody2D as well (source), so it is really up to what you are designing.

Why Update() doesnt set .text?

I have a Script Coin-Counter connected with a Text. Every time the Player and the Coin are colliding the coinScore decreases by 1. If I want to display the coinScore in Update() this doesn't work. Why?
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinScore : MonoBehaviour
{
[SerializeField] private Text coinScorer;
private int coinScore;
private int oldCoinScore;
void Update()
{
coinScorer.text = coinScore.ToString(); // This doesn't work.
oldCoinScore = coinScore;
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
coinScore += 1;
Destroy(gameObject);
//coinScorer.text = coinScore.ToString(); //This works.
}
}
Your collision method is destroying this game object.
There is no update to run after that.

Unity Unexpected Sound Behavior

Im taking a Unity C# course on Udemy, and we got to an challenge and i came up with a very simple "game". My problem is i can't figure out why no sound is playing? I looked at the instructors code and is the same as my (or did i just miss something?)
When i press on space the rocket starts flying (this works) and should play sound (it doesnt). Yes i got a SoundListener attached to the main camera.
Can somebody help and explain me where i missed something?
Here is my code that i wrote:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rocket : MonoBehaviour
{
Rigidbody rigidBody;
AudioSource audioSource;
// Use this for initialization
void Start()
{
rigidBody = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
ProcessInput();
}
private void ProcessInput()
{
if (Input.GetKey(KeyCode.Space)) // can thrust while rotating
{
rigidBody.AddRelativeForce(Vector3.up);
if (!audioSource.isPlaying) // so it doesn't layer
{
audioSource.Play();
}
}
else
{
audioSource.Stop();
}
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.forward);
}
else if (Input.GetKey(KeyCode.D))
{
transform.Rotate(-Vector3.forward);
}
}
}
Solved it after after declaring the audioSource to public

Categories

Resources