The Tripleshot object will hit the player and stop when i uncheck isTrigger on the Box Collider2D. When I check it the Triple Shot will go right through it and not be destroyed? On the Player Spaceship
BoxCollider2d with Trigger
Player tagged as “Player”
On the Triple Shot
BoxCollider2D with trigger
Rigidbody2D with no gravity
Powerupscript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Powerup : MonoBehaviour
{
[SerializeField]
float speed = 3.01f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.down * speed * Time.deltaTime);
if (transform.position.y < -4.6f)
{
Destroy(this.gameObject);
}
}
private void OntriggerEnter2d(Collider2D other)
{
if (other.tag == "Player")
{
Destroy(this.gameObject);
}
}
}
It seems like it should work to me, but you can get rid of the “this,” since “gameObject” refers directly to the game object the script is attached to.
I had a similar problem which I posted in “physics” yesterday. I followed the collision chart to a tee. For what it’s worth, I restarted the engine, and now it works. Maybe you’ll be as lucky.
Just tried it (OnCollisionEnter2D) but did not work either
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Powerup : MonoBehaviour
{
[SerializeField]
float speed = 3.01f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.down * speed * Time.deltaTime);
if (transform.position.y < -4.6f)
{
Destroy(gameObject);
}
}
private void OnCollisionEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Destroy(gameObject);
}
}
}
So this script does have the enemy being destroyed when it hits the player, and the bullets destroying the enemy… And it is exactly like the Powerip script and the same player is hitting both with the exact same physics in it .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Destoyenemy1 : MonoBehaviour
{
public float speedDown = 4.1f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.down * speedDown * Time.deltaTime);
if (transform.position.y < -6.1f)
{
transform.position = new Vector3(Random.Range(-13f, 13f), 7f, 0);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
other.transform.GetComponent<Player>().Damage();
Destroy(this.gameObject);
}
if (other.tag == "Bullet")
{
Destroy(other.gameObject);
Destroy(this.gameObject);
}
}
}
So I was teaching class last night and had them do this project explaining there was a bug in the script, and when I helped one of the students I was shocked to see to it work for them and ti worked for all 20 students? Maybe it was the caps of just a error with the VS script I created?
This worked
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Powerup : MonoBehaviour
{
[SerializeField]
float speed = 3.041f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.down * speed * Time.deltaTime);
if (transform.position.y < -4.6f)
{
Destroy(this.gameObject);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Destroy(this.gameObject);
}
}
}